Database.Middle.What is the Seek Method?

The Seek Method paginates by using a stable, indexed column value (like id or created_at) from the last row of the previous page, instead of counting and skipping rows like OFFSET does.

🧠 Core Idea

Instead of saying:

-- Offset method: skips rows
LIMIT 10 OFFSET 20;

You say:

-- Seek method: starts "after" a known value
WHERE created_at < '2024-01-01 10:00:00'
ORDER BY created_at DESC
LIMIT 10;

🧭 You’re saying: “Give me the next 10 posts older than this timestamp.”

✅ Example

Table: posts

idcontentcreated_at
105Hello world2024-06-10 10:00:00
104Post 22024-06-10 09:55:00
103Post 32024-06-10 09:45:00

First Page:

SELECT * FROM posts
ORDER BY created_at DESC
LIMIT 10;

You get the latest 10 posts. The last post’s created_at is 2024-06-10 09:00:00.

Next Page (Seek):

SELECT * FROM posts
WHERE created_at < '2024-06-10 09:00:00'
ORDER BY created_at DESC
LIMIT 10;

→ Returns the next 10 posts older than that date.

Repeat by tracking the last created_at on each page.

✅ Advantages

BenefitWhy it Matters
🔄 Consistent resultsNo shifting from inserts/deletes on other rows
⚡ Very fastEspecially for deep pages (no OFFSET scan)
💪 Works with indexesOptimized for indexed columns

⚠️ Limitations

DrawbackDescription
❌ No page numbersYou can’t jump directly to page 10
❌ Need stateThe client must remember the last seen value

🧰 Pro Tips

  • Use unique values in ORDER BY, like created_at, id, to avoid duplicates:
ORDER BY created_at DESC, id DESC
WHERE (created_at, id) < (?, ?)

ORDER BY created_at DESC, id DESC
WHERE (created_at, id) < (?, ?)

CREATE INDEX idx_posts_created_at ON posts(created_at DESC);

🔁 Summary

FeatureOFFSETSeek Method (Keyset)
SpeedSlower on deep pagesFast even for page 1000
Result StabilityCan shift on insertsStable and consistent
Page jumping✅ Yes❌ No (next/prev only)
Requires bookmark?❌ No✅ Yes (last seen value)
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.