I know you can ALTER the column order in MySQL with FIRST and AFTER, but why would you want to bother? Since good queries explicitly name columns when inserting data, is there really any reason to care what order your columns are in in the table?
Yes, column order does matter.
No, column order is not significant.
Based on what I have seen and read the ordering of columns in SQL Server makes no difference. The storage engine places columns on the row irrespective of how they are specified in the CREATE TABLE statement.
Column order had a big performance impact on some of the databases I've tuned, spanning Sql Server, Oracle, and MySQL. This post has good rules of thumb:
An example for difference in performance is an Index lookup. The database engine finds a row based on some conditions in the index, and gets back a row address. Now say you are looking for SomeValue, and it's in this table:
SomeId int, SomeString varchar(100), SomeValue int
The engine has to guess where SomeValue starts, because SomeString has an unknown length. However, if you change the order to:
SomeId int, SomeValue int, SomeString varchar(100)
Now the engine knows that SomeValue can be found 4 bytes after the start of the row. So column order can have a considerable performance impact.
EDIT: Sql Server 2005 stores fixed-length fields at the start of the row. And each row has a reference to the start of a varchar. This completely negates the effect I've listed above. So for recent databases, column order no longer has any impact.
Update:
In MySQL
, there may be a reason to do this.
Since variable datatypes (like VARCHAR
) are stored with variable lengths in InnoDB
, the database engine should traverse all previous columns in each row to find out the offset of the given one.
The impact may be as big as 17% for 20
columns.
See this entry in my blog for more detail:
In Oracle
, trailing NULL
columns consume no space, that's why you should always put them to the end of the table.
Also in Oracle
and in SQL Server
, in case of a large row, a ROW CHAINING
may occur.
ROW CHANING
is splitting a row that doesn't fit into one block and spanning it over the multiple blocks, connected with a linked list.
Reading trailing columns that didn't fit into the first block will require traversing the linked list, which will result in an extra I/O
operation.
See this page for illustration of ROW CHAINING
in Oracle
:
That's why you should put columns you often use to the beginning of the table, and columns you don't use often, or columns that tend to be NULL
, to the end of the table.
Important note:
If you like this answer and want to vote for it, please also vote for @Andomar
's answer.
He answered the same thing, but seems to be downvoted for no reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With