Does column ordering affects performance in Microsoft SQL Server 2012?

后端 未结 2 680
我寻月下人不归
我寻月下人不归 2020-12-20 15:43

I have read that the varchar fields should have placed as a column at the end of a database table - at least in MySQL. The reason is because the varchar fields have variable

2条回答
  •  我在风中等你
    2020-12-20 16:25

    When it comes to creating an index, column order does matter.

    An index key is sorted on the first column of the index and then subsorted on the next column within each value of the previous column. The first column in a compound index is frequently referred to as the leading edge of the index. For example, consider this table:

    c1  c2
    1   1
    2   1
    3   1
    1   2
    2   2
    3   2

    If a composite index is created on the columns (c1, c2), then the index will be ordered as shown in this table:

    c1  c2
    1   1
    1   2
    2   1
    2   2
    3   1
    3   2

    As shown in the above table, the data is sorted on the first column (c1) in the composite index. Within each value of the first column, the data is further sorted on the second column (c2).

    Therefore, the column order in a composite index is an important factor in the effectiveness of the index. You can see this by considering the following:

    • Column uniqueness
    • Column width
    • Column data type

    SELECT * FROM t1 WHERE c2 = 12

    SELECT * FROM t1 WHERE c2 = 12 AND c1 = 11

    An index on (c2, c1) will benefit both the queries. But an index on (c1, c2) will not be appropriate, because it will sort the data initially on c1, whereas the first SELECT statement needs the data to be sorted on c2.

    Source: SQL Server 2008 Query Performance Tuning Distilled

提交回复
热议问题