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?
4

6 回答 6

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.

于 2008-08-29T17:52:40.703 回答
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

于 2008-08-29T17:37:15.017 回答
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.

于 2008-08-29T17:40:47.207 回答
1

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

于 2008-08-29T17:38:42.423 回答
0

创建表时列顺序无关紧要。我们可以在从数据库中检索数据时排列列。主键可以设置为任何列或列组合。

于 2017-07-10T12:52:37.287 回答
-3

对于第一个项目符号:

的,列顺序确实很重要,至少如果您使用已弃用的 BLOB imagetextntext,并且使用 SQL Server <= 2005。

在这些情况下,您应该将这些列放在表的“末尾”,并且每次检索一个时都会对性能造成影响。

如果您使用 DAL 从此类表中检索数据,那么这是延迟加载模式的理想场所。

于 2010-04-28T20:18:29.423 回答