Does the sort order of clustered indexes

If the PK of the table is a standard int (Id) increment, and the extracted and updated records are almost always closer to the maximum Id, will there be any performance difference depending on whether the clustered PK index is sorted as ascending or descending?

When such a PK is created, SSMS by default sets the sort order of the index as ascending, and since the rows most accessible are always the ones closest to the current maximum identifier, I wonder if sorting will change the sort in descending order until the records are sorted from top to bottom, not bottom to top, and records closer to the top will be available most often.

+5
source share
2 answers

I don’t think there will be any kind of performance hit. Because it will do a binary search of the index key for access, and then a specific data block with that key. In any case, this binary search will reach O(log N) complexity. Thus, in general, O(log N) + 1 , and since this is a clustered index, it must be O(log N) time complexity; since table entries are physically ordered, and do not have a separate index page / block.

+2
source

Indexes use a B-tree structure, therefore None. But if you have an index based on several columns, you need the most different columns at the outer level and the least different at the inner levels. For example, if you had 2 columns (gender and age), you would like the age to be external and gender on the internal, because there are only 2 possible sexes, while there are many more ages. This will affect performance.

+2
source

Source: https://habr.com/ru/post/1204444/


All Articles