~learn.
0:00 / 1:11
W00 · Foundations

Page 2,000 costs 2,000 times page 1.

transcript

Page 1 and page 2,000 return the same 20 rows. One of them costs 2,000 times more. Here's why.

A café orders groceries every day. 40,000 orders so far. Your app shows 20 to a page. Limit 20, plus an offset. Easy. The owner opens page 2,000. Offset: 39,980.

But here's the catch. Skipped rows still get computed. Page 1 walks 20. Page 100 walks 2,000 index entries. Page 2,000 walks 40,000. Just to keep 20. Page n costs n times page 1.

Scroll through every page? About 40 million entries. All that, to show 40,000 orders. Most users stop by page 3, so averages look fine. Then one crawler outspends thousands of users.

Offset says: skip 39,980 rows, then give me 20. A cursor says: start after this order. The index jumps straight there. That's key set pagination.

So every page costs about 21 reads. Page 1, or page 2,000. Don't count your way there. Say where you stopped.

Full breakdown in the lecture.