Keyset pagination for performance in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When fetching data in pages, how fast the system responds matters a lot. We want to understand how the time to get each page changes as the data grows.
How does using keyset pagination affect the speed compared to other methods?
Analyze the time complexity of the following code snippet.
GET /items?limit=10&after=12345
// Server query example:
SELECT * FROM items
WHERE id > 12345
ORDER BY id ASC
LIMIT 10;
This code fetches the next 10 items after a given ID using keyset pagination.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Database scans starting from a specific key (id > 12345).
- How many times: The database reads only the next 10 rows after the key, no scanning of previous rows.
As the total number of items grows, the query still reads only the next 10 items after the last seen ID.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads about 10 rows |
| 100 | Reads about 10 rows |
| 1000 | Reads about 10 rows |
Pattern observation: The number of operations stays roughly the same regardless of total data size.
Time Complexity: O(1)
This means the time to fetch a page stays constant no matter how big the data grows.
[X] Wrong: "Fetching page 100 will take 100 times longer than page 1 because it has to skip all previous items."
[OK] Correct: Keyset pagination uses a key to jump directly to the right spot, so it does not scan or skip all previous items.
Understanding how keyset pagination keeps queries fast helps you design APIs that scale well. This skill shows you can think about performance as data grows.
"What if we changed the query to use offset-based pagination instead of keyset? How would the time complexity change?"
Practice
keyset pagination over traditional offset-based pagination in REST APIs?Solution
Step 1: Understand offset-based pagination issues
Offset pagination uses a page number and offset, which becomes slow on large datasets because the database must skip many rows.Step 2: Recognize keyset pagination benefits
Keyset pagination uses a fixed key (like an ID) to fetch the next set of rows, avoiding the costly skip operation and improving performance.Final Answer:
It improves performance by avoiding slow offset queries on large datasets. -> Option CQuick Check:
Keyset pagination = better performance [OK]
- Thinking keyset allows random page jumps
- Assuming keyset caches all data
- Believing keyset changes sort order automatically
users ordered by id?Solution
Step 1: Identify keyset pagination syntax
Keyset pagination uses a WHERE clause with a key (like id > last_seen_id) and a LIMIT to fetch the next page.Step 2: Analyze each option
SELECT * FROM users WHERE id > 20 ORDER BY id LIMIT 10; usesWHERE id > 20withORDER BY id LIMIT 10, which matches keyset pagination logic.Final Answer:
SELECT * FROM users WHERE id > 20 ORDER BY id LIMIT 10; -> Option BQuick Check:
Keyset uses WHERE key > last_key [OK]
- Using OFFSET instead of WHERE for pagination
- Using equality (=) instead of greater than (>)
- Not ordering results by the key column
GET /items?last_id=50&limit=5
And the database table
items with IDs: [45, 47, 50, 52, 55, 60, 65], what will be the IDs returned by this request?Solution
Step 1: Understand keyset pagination with last_id=50
The API returns items with IDs greater than 50, limited to 5 results.Step 2: Select IDs greater than 50 from the list
IDs greater than 50 are [52, 55, 60, 65]. There are only 4 such items, so all are returned.Final Answer:
[52, 55, 60, 65] -> Option AQuick Check:
IDs > 50 limited to 5 = [52, 55, 60, 65] [OK]
- Including items with ID equal to last_id
- Using offset instead of key comparison
- Assuming IDs are continuous numbers
SELECT * FROM orders WHERE order_date > '2024-01-01' ORDER BY order_date LIMIT 10;
But it returns duplicate rows when new orders are added. What is the likely cause?
Solution
Step 1: Identify ordering column uniqueness
Ordering byorder_datealone can cause duplicates if multiple rows share the same date.Step 2: Understand keyset pagination requirements
Keyset pagination requires ordering by a unique column or combination to avoid duplicates and missing rows.Final Answer:
Ordering by a non-unique column causing duplicates -> Option AQuick Check:
Order by unique key to avoid duplicates [OK]
- Thinking OFFSET fixes duplicates
- Using >= instead of > causes duplicates
- Assuming LIMIT controls duplicates
price ascending, then by id ascending to break ties.Which SQL WHERE clause correctly fetches the next page after last product with
price=100 and id=50?Solution
Step 1: Understand multi-column keyset pagination
When ordering by multiple columns, the WHERE clause must handle the first column and then the second to break ties.Step 2: Analyze the correct condition
The correct condition isprice > 100 OR (price = 100 AND id > 50)to get all rows with higher price or same price but higher id.Final Answer:
WHERE price > 100 OR (price = 100 AND id > 50) -> Option DQuick Check:
Multi-column keyset uses OR + AND for tie-break [OK]
- Using AND instead of OR for first column
- Ignoring tie-break column in WHERE clause
- Using >= instead of > causing duplicates
