I've got a procedure with a Sort operator of 92%. Here is the execution plan: here is the script:
CREATE PROCEDURE [Specification].[GetSpecificationsByPage]
(
@Next int,
@Limit int
)
AS
BEGIN
SELECT
ps.Id
psdc.Timestamp,
ps.CreationStatusId,
ps.ErrorReason
FROM
[Specification].[Specifications] ps WITH (NOLOCK)
OUTER APPLY (SELECT TOP 1 psd.Timestamp
FROM Specification.SpecificationDetails psd WITH (NOLOCK)
WHERE psd.SpecificationId = ps.Id
ORDER BY psd.Timestamp) psdc
ORDER BY ps.[Id] asc
offset @Next rows
fetch next @Limit rows only
END
I've added an index for column SpecificationId for table :Specification.SpecificationDetails that includes column: Timestamp even added index for Timestamp asc but it still have the cost of 92%.
The only way i've got is to replace to : ORDER BY ps.Id or remove the line in the select: psdc.Timestamp
what index do i need to continute?
Specification.SpecificationDetails(Id, Timestamp)
? Be aware thanNOLOCK
can cause rows to get missed or duplicated during scans, causing wrong results.Timestamp
column allow duplicate values? If so, you may need to addId
as a secondary ordering element to get deterministic results, though with all theNOLOCK
hints you seem to not care about result correctness anyway.