Is there a performance difference in using a GROUP BY with MAX() as the aggregate vs ROW_NUMBER over partition by?

前端 未结 3 1473
生来不讨喜
生来不讨喜 2021-01-18 00:15

Is there a performance difference between the following 2 queries, and if so, then which one is better?:

    select 
    q.id, 
    q.name 
    from(
                


        
3条回答
  •  失恋的感觉
    2021-01-18 00:37

    I'd use the group by name.

    Not much in it when the index is name, id DESC (Plan 1)

    but if the index is declared as name, id ASC (Plan 2) then in 2008 I see the ROW_NUMBER version is unable to use this index and gets a sort operation whereas the GROUP BY is able to use a backwards index scan to avoid this.

    You'd need to check the plans on your version of SQL Server and with your data and indexes to be sure.

提交回复
热议问题