Is there a performance difference between the following 2 queries, and if so, then which one is better?:
select
q.id,
q.name
from(
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.