←back to thread

428 points ahamez | 4 comments | | HN request time: 3.387s | source
1. Joker_vD ◴[] No.45015564[source]
> Instead of using OFFSET, the query becomes WHERE id > cursor ORDER BY id LIMIT 10

Wait. Surely "ORDER BY id OFFSET 20 LIMIT 10" works about the same as "WHERE id > cursor ORDER BY id LIMIT 10", if "id" is indexed?

replies(1): >>45019544 #
2. asddubs ◴[] No.45019544[source]
no, because it has to count the amount of matching rows preceding the offset rows to determine the offset, i.e. iterate over all preceding rows. The cursor provides a starting point for the offset so in this instance it's not necessary.

https://use-the-index-luke.com/sql/partial-results/fetch-nex...

replies(1): >>45021098 #
3. Joker_vD ◴[] No.45021098[source]
It only has to count if the index doesn't store the amounts of records in the "chunks" it manages, does it? I'm pretty sure B-trees do actually store the sizes of the subtrees.
replies(1): >>45101592 #
4. VGHN7XDuOXPAzol ◴[] No.45101592{3}[source]
Afaik Postgres doesn't. In my exposure it'd be quite uncommon for a b-tree to store the size of a subtree; would cause more churn/writes when updating trees.

Perhaps some of the page-level Copy-on-Write databases (LMDB?) might do this, since they have to rewrite ancestor pages anyway.