SQL INSERT order

When using SQL INSERT, does it matter if you ignore the order in which the columns are displayed in the db?

|-----------------------|
| id | derp | foo | bar |
|-----------------------|
| 1  |"herp"|"bar"|  88 |
|-----------------------|

      

So the order in SQL Server (especially 2008) is id, derp, foo, bar. (Assuming id is PK and autoincrement)

So, I would write SQL like this:

INSERT INTO table_name (foo, bar, derp) VALUES (42, "troll", "lolz");

      

I usually write it like this of course:

INSERT INTO table_name (derp, foo, bar) VALUES ("lolz","troll", 42);

      

Now it might be micro-optimized, but I'm curious if inserting from the column order will slow down my query at all?

+3


source to share


1 answer


A thing like this doesn't matter at all, since SQL is meant to be declarative in nature: you tell the database what you want and it is the best fit to achieve it.

Of course, there are exceptions to this, otherwise there would be no need for optimizer hints and the like. But the trend - generally speaking - is not to worry about the finer workings of the database to free you, the developer, for other tasks.

eg. self-serving tablespaces, auto-assigned partitions (both available in Oracle)

HOWEVER .... since we need to learn to deal with ever-expanding layers of abstraction, and since these abstractions often leak (when they don't work 100% as intended) from time to time, we need to understand what is going on behind the scenes. If you are able to do it, or know how to find out information on how to do it (almost as well!), Then it will be good for you.



For this reason, I can recommend this excellent essay by Joel Spolsky to you:

http://www.joelonsoftware.com/articles/LeakyAbstractions.html

The Law of Leaking Abstraction means that whenever someone comes up with a magical new code generation tool that should make us all very efficient, you hear a lot of people say "learn how to do it by hand first, then use the wizzy tool to save time" ... Code generation tools that pretend to abstract something, like all abstractions, leaks, and the only way to deal with leaks intelligently is to learn how abstractions work and what they abstract. Thus, abstractions save time to work, but they do not save us from teaching time.

And all of this means that, paradoxically, even if we have top and top tier programming tools with better and better abstractions, becoming an experienced programmer becomes more and more difficult.

+4


source







All Articles