Pagination patterns in Supabase - Time & Space Complexity
When fetching data in pages, the time it takes depends on how many pages and items we request.
We want to know how the number of data requests grows as we ask for more pages.
Analyze the time complexity of fetching paged data using Supabase.
const pageSize = 10;
let page = 1;
const { data, error } = await supabase
.from('items')
.select('*')
.range((page - 1) * pageSize, page * pageSize - 1);
// This fetches one page of items with a fixed size.
This code fetches one page of data with a fixed number of items per page.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: One API call to fetch a page of data.
- How many times: Once per page requested.
Each page requires one API call, so more pages mean more calls.
| Input Size (n pages) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The number of calls grows directly with the number of pages requested.
Time Complexity: O(n)
This means if you want to get more pages, the total work grows in a straight line with the number of pages.
[X] Wrong: "Fetching more pages is just one big call, so time stays the same."
[OK] Correct: Each page requires a separate call, so more pages mean more calls and more time.
Understanding how pagination scales helps you design efficient data fetching in real projects.
"What if we changed from page-based to cursor-based pagination? How would the time complexity change?"
Practice
.range(from, to) method do in Supabase pagination?Solution
Step 1: Understand the purpose of
This method is used to select a slice of rows by index, starting at.range(from, to)fromand ending atto, inclusive.Step 2: Compare options with this behavior
Only the option stating 'It fetches rows starting from indexfromup to indextoinclusive.' correctly describes the slicing behavior. The other options describe sorting, limiting, or skipping incorrectly.Final Answer:
It fetches rows starting from indexfromup to indextoinclusive. -> Option CQuick Check:
.range(from, to)= fetch rows from-to inclusive [OK]
- Thinking
tois exclusive - Confusing
.rangewith.limit - Assuming it skips rows instead of slicing
.range()?Solution
Step 1: Calculate start and end indexes for page 3
Page 3 means items 21 to 30 (1-based). Since.range()uses 0-based indexes, start = (3-1)*10 = 20, end = 20 + 9 = 29.Step 2: Match calculated range with options
.range(20, 29)matches the correct 0-based slice for page 3.Final Answer:
.range(20, 29) -> Option AQuick Check:
Page 3 with 10 items = indexes 20 to 29 [OK]
- Using 1-based indexes directly
- Off-by-one errors in start or end
- Confusing page number with item count
const page = 2;
const pageSize = 5;
const { data } = await supabase
.from('products')
.select('*')
.range((page - 1) * pageSize, page * pageSize - 1);
How many rows will
data contain?Solution
Step 1: Calculate the range indexes
For page 2 and pageSize 5, start = (2-1)*5 = 5, end = 2*5 - 1 = 9.Step 2: Determine number of rows fetched
Indexes 5 to 9 inclusive means 5 rows (5,6,7,8,9).Final Answer:
5 -> Option DQuick Check:
Range length = end - start + 1 = 5 [OK]
- Counting end index as exclusive
- Miscalculating start or end
- Assuming pageSize is ignored
const page = 1;
const pageSize = 10;
const { data } = await supabase
.from('orders')
.select('*')
.range(page * pageSize, (page + 1) * pageSize - 1);
Why does this return no rows?
Solution
Step 1: Analyze the range calculation
For page 1 and pageSize 10, start = 1*10 = 10, end = (1+1)*10 - 1 = 19. This skips rows 0 to 9.Step 2: Understand why no rows appear
If the table has fewer than 11 rows or you want the first page, you must start at 0. Starting at 10 skips the first 10 rows.Final Answer:
Because the range starts at 10 instead of 0, skipping the first page. -> Option BQuick Check:
Page 1 start index = 0, not 10 [OK]
- Starting range at page * pageSize instead of (page-1)*pageSize
- Misunderstanding zero-based indexing
- Incorrect order of method calls
Solution
Step 1: Define correct zero-based ranges for pages
For 15 items per page, first page is indexes 0 to 14 (15 items). Second page starts at 15 and ends at 29.Step 2: Match options with correct ranges
The option using.range(0, 14)for first page, then.range(15, 29)for second page is correct. It avoids off-by-one errors or overlaps present in the others.Final Answer:
Use.range(0, 14)for first page, then.range(15, 29)for second page. -> Option AQuick Check:
Pages must not overlap and cover all items [OK]
- Overlapping ranges causing duplicate items
- Off-by-one errors in start or end indexes
- Starting second page at wrong index
Ready to prove it?
Pass the test and this pattern moves unknown
