星期三, 3月 20, 2024

[SQL] 避免 Order By

Order By 在 MS SQL 內算是高成本 operator,看情況是否可以拉到 AP 端來處理,寫個簡易範例紀錄
use AdventureWorks2022
GO

DROP INDEX IF EXISTS IX_Person_LastName ON Person.Person
GO

CREATE INDEX IX_Person_LastName ON Person.Person (LastName)
GO

SELECT LastName , COUNT(*)
FROM Person.Person
WHERE LastName IN ('Diaz' , 'Hernandez')
GROUP BY LastName 

SELECT LastName , COUNT(*)
FROM Person.Person
WHERE LastName IN ('Diaz' , 'Hernandez')
GROUP BY LastName 
ORDER BY COUNT(*)


沒有留言:

張貼留言