Is there any reason to worry about the column order in a table?

前端 未结 14 1009
执念已碎
执念已碎 2020-11-28 17:49

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 the

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 18:16

    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:

    • Choosing column order

    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.

提交回复
热议问题