We are on 13.9.
SELECT /* ISNULL:pc_job.CloseDate:, KeyTable:pc_job; */ gRoot.ID col0, gRoot.Subtype col1, gRoot.CreateTime col2
I'm wondering why a sort is required for this query, as the index should be providing the required ordering to satisfy the ORDER BY clause. Does it have to do with the IS NULL predicate on the leading key column in the index?
There's an index, job_u_closedate_g9cdc6ghupib, on pc_job(CloseDate, Retired, Subtype, CreateTime, ID). All columns have ASC sort order and NULLs LAST.
- pc_job is the probe table in a hash join
- There are IS NULL and equality predicates on the 3 leading columns in the index and the last 2 key columns (CreateTime, ID) are the ordering columns in the query
- So, the Index Scan of job_u_closedate_g9cdc6ghupib is returning the rows in the sorted order
- NOTE: The sort is cheap, but I'm investigating this because "CloseDate IS NULL" is very selective and without forcing the index the optimizer is choosing a different sort avert index that does not include CloseDate and hence a lot of time is spent filtering out rows on that predicate against the heap.
FROM pc_job gRoot INNER JOIN pc_policy policy_0
ON policy_0.ID = gRoot.PolicyID
WHERE gRoot.Subtype = 7 AND gRoot.CloseDate IS NULL
ON policy_0.ID = gRoot.PolicyID
WHERE gRoot.Subtype = 7 AND gRoot.CloseDate IS NULL
AND gRoot.Retired = 0
AND policy_0.ProducerCodeOfServiceID IN
AND policy_0.ProducerCodeOfServiceID IN
(248,1092,1848,74101,103158,103159,117402,122618,129215,132420,135261,137719)
AND policy_0.Retired = 0
AND policy_0.Retired = 0
ORDER BY col2 ASC, col0 ASC LIMIT 10