I have a table that has more than 10.000.000 records, and I'm creating a query that returns about 4436 records.
It just so happens that it gives me the impression that the query cost to get to the last record is very high.
Index Scan using idx_name on task (cost=0.28..142102.57 rows=3470 width=34) (actual time=14.690..22.894 rows=4436 loops=1)
" Index Cond: ((situation = ANY ('{0,1,2,3,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}'::integer[])) AND (deadline < CURRENT_TIMESTAMP))"
Planning Time: 1.335 ms
JIT:
Functions: 5
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 1.654 ms, Inlining 0.000 ms, Optimization 1.214 ms, Emission 13.163 ms, Total 16.030 ms
Execution Time: 24.758 ms
Is this level of cost acceptable or does this index need improvement?
Index:
CREATE INDEX idx_name ON task (situation, deadline, approved)
WHERE
deadline IS NOT NULL AND
situation <> ALL ('{4,5}'::integer[]) AND
approved = 'N';
My Query:
SELECT
task.deadline,
task.id
FROM
task
WHERE
task.deadline IS NOT NULL
AND task.situation IN ('0', '1', '2', '3', '6' ,'7' ,'8','9','10','11','12','13','14','15','16','17','18','19','20')
AND task.situation NOT IN ('4', '5')
AND task.deadline < CURRENT_TIMESTAMP
AND task.approved = 'N';
situation <> ALL ('{4,5}'::integer[])
and not the more traditionalsituation NOT IN (4, 5)
?situation NOT IN (4, 5)
to besituation <> ALL ('{4,5}'::integer[])
so he might have just written the index WHERE to match what was showing up in the pre-index explain output.