For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC

后端 未结 7 1251
甜味超标
甜味超标 2020-12-06 10:20

I want to find the highest AutoIncremented value from a field. (its not being fetched after an insert where I can use @@SCOPE_IDENTITY etc) Which of these two q

7条回答
  •  执念已碎
    2020-12-06 10:51

    If there is a clustered index there is virtually no difference in performance between the two queries.

    This is becuase both will perform a Clustered Index Scan that will bear 100% of the query cost.

    Performing the two queries on a column that does not have an index results in 3 operators being used in both execution plans.

    The Top clause uses the Sort operator and the Max function uses a Stream Aggregate operator.

    When there is no index, the MAX() function provides better performance.

    Proof of concept can be found and full walkthrough of a test scenario can be found here:

    Performance Comparison Top 1 Verses MAX() Function

提交回复
热议问题