intermediateTop 30 Scenario-Based Questions
Your pagination query (OFFSET 1000000 LIMIT 20) is extremely slow. How do you fix it?
OFFSET N scans and discards N rows — O(n). For OFFSET 1,000,000: 1 million rows read and thrown away. Solution: Keyset pagination. Instead of OFFSET: WHERE id > last_seen_id ORDER BY id LIMIT 20. The query uses the primary key index — O(log n). In API: return last_id in response; next request uses it. For complex ORDER BY: use a tuple comparison: WHERE (salary, id) < (last_salary, last_id) ORDER B
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
Your pagination query (OFFSET 1000000 LIMIT 20) is extremely slow. How do you fix it?