SQL Server Clustered Index - Order of Index Question

后端 未结 9 1116
梦毁少年i
梦毁少年i 2020-12-05 12:34

I have a table like so:

keyA keyB data

keyA and keyB together are unique, are the primary key of my table and make up a clustered index.

9条回答
  •  既然无缘
    2020-12-05 12:55

    Just in case this isn't obvious: the sort order of your index does not promise much about the the sort order of the results in a query.

    In your queries, you must still add an

    ORDER BY KeyA, KeyB
    

    or

    ORDER BY KeyB, KeyA
    

    The optimizer may be pleased to find the data already physically ordered in the index as desired and save some time, but every query that is supposed to deliver data in a particular order must have an ORDER BY clause at the end of it. Without an order by, SQL Server makes no promises with respect to the order of a recordset, or even that it will come back in the same order from query to query.

提交回复
热议问题