27

In terms of performance and optimizations:

  • When constructing a table in SQL Server, does it matter what order I put the columns in?
  • Does it matter if my primary key is the first column?
  • When constructing a multi-field index, does it matter if the columns are adjacent?
  • Using ALTER TABLE syntax, is it possible to specify in what position I want to add a column?
    • If not, how can I move a column to a difference position?
Seibar
  • 66,138
  • 37
  • 86
  • 99
  • See http://stackoverflow.com/questions/6692021/performance-space-implications-when-ordering-sql-server-columns – gbn Oct 17 '11 at 06:07
  • Here's a similar question on DBA.SE: [Does the order of columns in a table's definition matter?](http://dba.stackexchange.com/q/18719/2660) – Nick Chammas Jun 05 '12 at 17:25

6 Answers6

9

In SQL Server 2005, placement of nullable variable length columns has a space impact - placing nullable variable size columns at the end of the definition can result in less space consumption.

SQL Server 2008 adds the "SPARSE" column feature which negates this difference.

See here.

Aidan Ryan
  • 11,139
  • 12
  • 50
  • 86
3

I would say the answer to all those questions is NO, altough my experience with MS-SQL goes as far as SQL2000. Might be a different story in SQL2005

Ricardo Reyes
  • 12,558
  • 4
  • 25
  • 19
3

For the fourth bullet: No you can't specify where you want to add the column. Here is the syntax for ALTER TABLE: https://msdn.microsoft.com/en-us/library/ms190273.aspx

In MySQL they offer an ALTER TABLE ADD ... AFTER ... but this doesn't appear in T-SQL.

If you want to reorder the columns you'll have to rebuild the table.

Edit: For your last last bullet point, you'll have to DROP the table and recreate it to reorder the columns. Some graphical tools that manipulate SQL databases will do this for you and make it look like you're reordering columns, so you might want to look into that.

Tim Abell
  • 10,156
  • 8
  • 75
  • 104
Daniel Jennings
  • 6,049
  • 3
  • 30
  • 42
1

No to the first 3 because the index will hold the data and no the last once also

SQLMenace
  • 128,762
  • 24
  • 200
  • 224
0

Column order does not matter while creating a table. We can arrange the columns while retrieving the data from the database. Primary key can be set to any column or combination of columns.

Nithin
  • 1,326
  • 11
  • 29
-3

For the first bullet:

Yes, column order does matter, at least if you are using the deprecated BLOBs image, text, or ntext, and using SQL Server <= 2005.

In those cases, you should have those columns at the 'end' of the table, and there is a performance hit every time you retrieve one.

If you're retrieving the data from such a table using a DAL, this is the perfect place for the Lazy Load pattern.

egrunin
  • 24,038
  • 8
  • 48
  • 92
  • I thought I'd commented. Complete rubbish http://stackoverflow.com/questions/6692021/performance-space-implications-when-ordering-sql-server-columns/6692107#6692107 – gbn Oct 17 '11 at 06:01