Databases · Senior · Question 56 of 75
Why can a LIMIT query sometimes be slower than the same query without LIMIT?
- 1
LIMITdisables indexes - 2
LIMITmakes PostgreSQL sort the whole table twice - ✓Correct answer: The planner may pick a plan that walks an index in order expecting to find matches quickly; if matches are rare, it scans far more rows
Explanation
This happens with skewed or correlated data. Better statistics, a composite index matching filter and order, or rewriting the query helps.