I have an index on car_id and an index on ended_at.
Is this query taking so long because I am ordering it by id and I have separate unique indexes?
Would it be better if I ordered it by ended_at and then made an index on both ended_at and car_id?
SELECT "trip_reports".*
FROM "trip_reports"
WHERE "trip_reports"."car_id" = $1 AND (ended_at < '2020-11-03 17:31:09')
ORDER BY "trip_reports"."id" DESC
LIMIT $2
Duration is 6.05 minutes.
The query plan:
Limit (cost=0.56..4512.17 rows=1 width=1156)
-> Index Scan Backward using trip_reports_pkey on trip_reports (cost=0.56..9830786.80 rows=2179 width=1156)
Filter: ((ended_at < '2020-11-03 20:55:57'::timestamp without time zone) AND (car_id = 103638))
EXPLAIN (ANALYZE, BUFFERS)
Limit (cost=0.56..4512.67 rows=1 width=1156) (actual time=976974.363..976974.363 rows=0 loops=1)
Buffers: shared hit=2071575 read=3222036
-> Index Scan Backward using trip_reports_pkey on trip_reports (cost=0.56..9831877.02 rows=2179 width=1156) (actual time=976974.361..976974.361 rows=0 loops=1)
Filter: ((ended_at < '2020-11-03 17:31:09'::timestamp without time zone) AND (car_id = 119780))
Rows Removed by Filter: 22862225
Buffers: shared hit=2071575 read=3222036
Planning time: 0.113 ms
Execution time: 976975.711 ms
EXPLAIN (ANALYZE, BUFFERS)for the query to give an answer.