What's the best way to increase the speed of a query in PostgreSQL that's performing a MAX(id) aggregation?
I have a modest number of records associated with an id, which I can COUNT() in a second e.g.
select count(id) as cnt from mytable where ref_id=2660
row cnt
1 2844
However, when I try and find the most recent record id using MAX(), the query takes nearly 5 minutes.
select max(id) as id from mytable where ref_id=2660
This is surprising, because I've otherwise found PG surprisingly fast with much more complicated queries. Why would there be such a difference in the query times, especially for such a relatively small number of records? What would be the best way to improve this performance?
EDIT: This is the query plan for the above MAX() select:
"Result (cost=219.84..219.85 rows=1 width=0)"
" InitPlan 1 (returns $0)"
" -> Limit (cost=0.00..219.84 rows=1 width=4)"
" -> Index Scan Backward using mytable_pkey on mytable (cost=0.00..773828.42 rows=3520 width=4)"
" Filter: ((id IS NOT NULL) AND (ref_id = 2660))"