Sphere
Log in Join
☆ 3

Postgres ignores my index on ORDER BY with LIMIT

Query selects recent posts WHERE status='approved' ORDER BY created_at DESC LIMIT 20. I have an index on created_at and one on status. Planner still does a sequential scan on a 2M row table.

⚑ Report

3 replies

☆ 0
K Kevin ·2026-10-30#1

Two single-column indexes rarely help a filter plus sort. Create a composite index on (status, created_at DESC), partial if approved is most of the table.

⚑ Report
☆ 0
R Rhea ·2026-10-30#2

Created the composite and it now uses it. Why did the plain created_at index not work?

⚑ Report
☆ 0
Z Zoe ·2026-10-30#3

Because it would have to scan all rows and filter status afterwards. The planner decided that was more expensive than a sequential scan.

⚑ Report
Log in to reply.