3

Are there any considerations that should be taken into account when designing a new table with regards to the order in which columns should be declared? I tend to put the primary key first, followed by any foreign keys (usually surrogate key integers), followed by other columns, but a discussion with a colleague had us wondering whether SQL Server will pad our data, possibly to make it faster.

Will SQL Server try and align our data on disk (with padding) to a specific byte alignment boundary for performance reasons (the way a C++ compiler would align a struct under default conditions) or will it just allocate as many bytes as our total row requires (possibly padding at a row level)? I.e. if we have a 3 byte char column and another column of type bit / tinyint, could we expect any change in behaviour (better or worse) from the server by making one of these follow another to align at 4 byte boundaries? Does SQL Server even care what order I declare the columns, or is it free to lay them out as it sees fit?

I understand that there are probably a million things I should look to first before trying to optimise the layout of my table columns, but for curiosity's sake I'm interested to know whether SQL Server cares about column ordering at all, and if so, where one would go to (DMV, etc??) see how it's physically laying the rows out on disk.

Terence Lewis
  • 904
  • 12
  • 22
  • Possible duplicate - http://stackoverflow.com/questions/34818/sql-server-does-column-order-matter – CResults Jul 14 '11 at 10:51
  • possible duplicate of [How to come to limits of 8060 bytes per row and 8000 per (varchar, nvarchar) value?](http://stackoverflow.com/questions/3793022/how-to-come-to-limits-of-8060-bytes-per-row-and-8000-per-varchar-nvarchar-valu) – gbn Jul 14 '11 at 10:57

1 Answers1

3

SQL Server stores the data on disk in set and fixed fashion.

The order in sys.columns and key columns has no relevance to this on-disk order.

See "Anatomy of a record" (Paul Randal) and my answer here: How do you get to limits of 8060 bytes per row and 8000 per (varchar, nvarchar) value?

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
  • Good day, The order in sys.columns is not relevant since it's only show the LOGICAL layer and does NOT show the real columns in the physical structure of the table. [Order of columns can significantly affect performance.Moreover, It can even impact errors!](http://ariely.info/Blog/tabid/83/EntryId/219/SQL-Server-internals-Is-Order-of-Column-in-the-table-important.aspx) – Ronen Ariely Apr 04 '19 at 19:46