Apologies for not including this in the original email. The other index, job_u_createtime_2cy0wgyqpani8, is on pc_job(CreateTime, Retired, Subtype, ID). The optimizer chooses Nested Loop when choosing that index, vs Hash Join when choosing the index in the first plan that I posted. It seems like the choice of the Hash Join in the 1st plan that I posted is collateral damage from the seemingly unnecessary need to do the sort.
Here's the plan without forcing the index:
Limit (cost=1.00..52692.73 rows=10 width=20) (actual time=55219.289..87704.704 rows=10 loops=1)
Buffers: shared hit=9579294 read=328583
I/O Timings: read=1157740.299
-> Nested Loop (cost=1.00..2007555.82 rows=381 width=20) (actual time=55219.288..87704.695 rows=10 loops=1)
Buffers: shared hit=9579294 read=328583
I/O Timings: read=1157740.299
-> Index Scan using job_u_createtime_2cy0wgyqpani8 on pc_job groot (cost=0.56..1800117.94 rows=153696 width=28) (actual time=102.075..79470.670 rows=5650 loops=1)
Index Cond: ((groot.retired = 0) AND (groot.subtype = 7))
Filter: (groot.closedate IS NULL)
Rows Removed by Filter: 14994857
Buffers: shared hit=9563981 read=321566
I/O Timings: read=1149579.949
-> Index Scan using pc_policy_pk on pc_policy policy_0 (cost=0.43..1.35 rows=1 width=8) (actual time=1.456..1.456 rows=0 loops=5650)
Index Cond: (policy_0.id = groot.policyid)
Filter: ((policy_0.retired = 0) AND (policy_0.producercodeofserviceid = ANY ('{248,1092,1848,74101,103158,103159,117402,122618,129215,132420,135261,137719}'::bigint[])))
Rows Removed by Filter: 1
Buffers: shared hit=15313 read=7017
I/O Timings: read=8160.350
Planning time: 2.209 ms
Execution time: 87705.116 ms
Buffers: shared hit=9579294 read=328583
I/O Timings: read=1157740.299
-> Nested Loop (cost=1.00..2007555.82 rows=381 width=20) (actual time=55219.288..87704.695 rows=10 loops=1)
Buffers: shared hit=9579294 read=328583
I/O Timings: read=1157740.299
-> Index Scan using job_u_createtime_2cy0wgyqpani8 on pc_job groot (cost=0.56..1800117.94 rows=153696 width=28) (actual time=102.075..79470.670 rows=5650 loops=1)
Index Cond: ((groot.retired = 0) AND (groot.subtype = 7))
Filter: (groot.closedate IS NULL)
Rows Removed by Filter: 14994857
Buffers: shared hit=9563981 read=321566
I/O Timings: read=1149579.949
-> Index Scan using pc_policy_pk on pc_policy policy_0 (cost=0.43..1.35 rows=1 width=8) (actual time=1.456..1.456 rows=0 loops=5650)
Index Cond: (policy_0.id = groot.policyid)
Filter: ((policy_0.retired = 0) AND (policy_0.producercodeofserviceid = ANY ('{248,1092,1848,74101,103158,103159,117402,122618,129215,132420,135261,137719}'::bigint[])))
Rows Removed by Filter: 1
Buffers: shared hit=15313 read=7017
I/O Timings: read=8160.350
Planning time: 2.209 ms
Execution time: 87705.116 ms
Thanks,
Jerry
On Wed, Jan 17, 2024 at 6:39 AM Jerry Brenner <jbrenner@xxxxxxxxxxxxx> wrote:
We are on 13.9.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.
SELECT /* ISNULL:pc_job.CloseDate:, KeyTable:pc_job; */ gRoot.ID col0, gRoot.Subtype col1, gRoot.CreateTime col2FROM pc_job gRoot INNER JOIN pc_policy policy_0
ON policy_0.ID = gRoot.PolicyID
WHERE gRoot.Subtype = 7 AND gRoot.CloseDate IS NULLAND gRoot.Retired = 0
AND policy_0.ProducerCodeOfServiceID IN(248,1092,1848,74101,103158,103159,117402,122618,129215,132420,135261,137719)
AND policy_0.Retired = 0ORDER BY col2 ASC, col0 ASC LIMIT 10