You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fast stateless pagination and efficient indexed count() #3949
This is a very common case which neither limit + skip neither slice can solve as of today. Anyone trying to write an API to get data out of RethinkDB will get disappointed with the performance.
Reacted by Juan Campa, Lucas Jans, Ante Sepic, Milan B, Cody Lundquist, Fabrizio, Steve Krenzel, jilen, DeepAnchor, Zach Peterson and 10 more
By stateless, do you mean being able to randomly access any given page by index?
If we stored subtree counts in something like the LBA we could do this (related to #152).
Unless we do some major re-architecturing of how LBA-like information is kept in memory, that would come at the cost of relatively hefty additional memory overhead though. I still don't really like this solution for that reason (and it's a lot of work).
Pagination by subtree counts only works for some types of queries. It speeds up table.slice(), table.orderBy().slice(), and table.between().slice(), but not e.g. table.filter().slice() or table.concatMap().slice(). Some types of joins would work and others would not. I'm not sure how bad this limitation is, though.
Yeah, I know we can do it fast using between + orderBy, as long as we always store the last key for later use, but this is not doable for the most common case: API to get data out of the database with a page index parameter. A lot of SaaS out there do it this way.
Here's a technical proposal of how we could solve the subtree count problem. It is kind of ugly because it breaks abstraction boundaries in the code, but so far it seems to be the most practical one:
Split the block ids into two ID ranges. One that we use for blob blocks (those that we use for storing large values), and one that we use for all other nodes.
Have two different data structures for the in-memory LBA. One for blob blocks that doesn't need a subtree count (and no recency, see below), and one for other blocks with an added subtree count. We currently have an array that's indexed by block ID and contains that sort of information. We would split that up into two arrays, one for each block ID range. The on-disk LBA format could always store all data, or we could have two different structs there as well.
The cache currently mirrors the recency for each block to avoid having to thread switch to the serializer when requesting a recency. We would implement a similar thing for subtree counts. Furthermore we would limit both of them to the non-blob block ID range to conserve memory.
The cache interface must be changed to distinguish between the two types of blocks.
Distinguishing blob and other blocks is important to keep memory consumption in check. As a very nice side-effect, this would also allow us to stop storing recencies for blob blocks, which would reduce our per-value memory overhead by close to 16 bytes (relevant for #1951).
Once that is done, we'll need to update the btree traversal and ReQL query language code to make use of the subtree counts.
An open question is how to upgrade existing data files. My suggestion would be to have a flag in the table that indicates whether the table has subtree counts or not. New tables will always have subtree counts, but old tables will never have them. Queries will only make use of subtree counts if they're available on the particular table.
Underneath, the in-memory LBA and cache could treat all blocks as non-blob blocks for old tables.
As far as the on-disk format of the LBA is concerned: In the worst-case we could completely rewrite it once during migration. That is relatively cheap.
Just FYI, on rethinkdb 2.1.0+1~0trusty (GCC 4.8.2):
Trying to get a count off a "between" was 21 seconds on my system (260k documents) as it seems to read all the data first.
r.db('freeswitch').table('logs_fs').between(r.minval,r.now().sub(12_60_60),{index:'timestamp'}).count()
I was expected sub-20ms..
Parts 2 and 4 (distinguishing between blob blogs and other blocks in the serializer) are implemented in the fix for #1951.
What's missing is adding subtree counts in the LBA, adding interfaces for retrieving and modifying them in the serializer and cache, and then integrating this with the btree modification and btree traversal code. That last part might be non-trivial.
Also backwards compatibility is something that we need to consider.
@avimar Indeed the current implementation reads all values in the range when performing the count on a secondary-index range. That would definitely be solved by the solution proposed in here.
I renamed the issue from "Fast stateless pagination" to "Fast stateless pagination and efficient indexed count()" because both issues will (probably) be solved by the same technical solution.
@v3ss0n This isn't required for changefeeds on counts. Changefeeds on counts will still be linear-time for the initial result, and then just update in constant time for each change.
Is this issue still being counted after the reboot of RethinkDB? I'm getting "mysterious" sustained 100% iowait at idle (with no jobs running), slow/frozen queries, and ofc a "slow" .count() operation time.
Activity
danielmewes commentedon Mar 19, 2015
By stateless, do you mean being able to randomly access any given page by index?
If we stored subtree counts in something like the LBA we could do this (related to #152).
Unless we do some major re-architecturing of how LBA-like information is kept in memory, that would come at the cost of relatively hefty additional memory overhead though. I still don't really like this solution for that reason (and it's a lot of work).
There might be some other way to achieve this.
timmaxw commentedon Mar 19, 2015
Pagination by subtree counts only works for some types of queries. It speeds up
table.slice(),table.orderBy().slice(), andtable.between().slice(), but not e.g.table.filter().slice()ortable.concatMap().slice(). Some types of joins would work and others would not. I'm not sure how bad this limitation is, though.thelinuxlich commentedon Mar 19, 2015
Yeah, I know we can do it fast using between + orderBy, as long as we always store the last key for later use, but this is not doable for the most common case: API to get data out of the database with a page index parameter. A lot of SaaS out there do it this way.
timmaxw commentedon Mar 19, 2015
I think you misread my comment. I'm not suggesting a workaround; I'm talking about the limitations of the solution that @danielmewes proposed.danielmewes commentedon Mar 19, 2015
I think @thelinuxlich was clarifying on my "By stateless, do you mean being able to randomly access any given page by index?" question.
thelinuxlich commentedon Mar 19, 2015
That's right :)
timmaxw commentedon Mar 19, 2015
Ah, sorry I misinterpreted you.
leandropincini commentedon Mar 19, 2015
+1 for this one...
I'm having the same kind of "performance" issue here.
Maybe with @danielmewes solution we're getting a start point.
danielmewes commentedon Mar 20, 2015
Here's a technical proposal of how we could solve the subtree count problem. It is kind of ugly because it breaks abstraction boundaries in the code, but so far it seems to be the most practical one:
Distinguishing blob and other blocks is important to keep memory consumption in check. As a very nice side-effect, this would also allow us to stop storing recencies for blob blocks, which would reduce our per-value memory overhead by close to 16 bytes (relevant for #1951).
Once that is done, we'll need to update the btree traversal and ReQL query language code to make use of the subtree counts.
An open question is how to upgrade existing data files. My suggestion would be to have a flag in the table that indicates whether the table has subtree counts or not. New tables will always have subtree counts, but old tables will never have them. Queries will only make use of subtree counts if they're available on the particular table.
Underneath, the in-memory LBA and cache could treat all blocks as non-blob blocks for old tables.
As far as the on-disk format of the LBA is concerned: In the worst-case we could completely rewrite it once during migration. That is relatively cheap.
avimar commentedon Aug 19, 2015
Just FYI, on rethinkdb 2.1.0+1~0trusty (GCC 4.8.2):
Trying to get a count off a "between" was 21 seconds on my system (260k documents) as it seems to read all the data first.
r.db('freeswitch').table('logs_fs').between(r.minval,r.now().sub(12_60_60),{index:'timestamp'}).count()
I was expected sub-20ms..
danielmewes commentedon Oct 14, 2015
Parts 2 and 4 (distinguishing between blob blogs and other blocks in the serializer) are implemented in the fix for #1951.
What's missing is adding subtree counts in the LBA, adding interfaces for retrieving and modifying them in the serializer and cache, and then integrating this with the btree modification and btree traversal code. That last part might be non-trivial.
Also backwards compatibility is something that we need to consider.
danielmewes commentedon Oct 14, 2015
@avimar Indeed the current implementation reads all values in the range when performing the count on a secondary-index range. That would definitely be solved by the solution proposed in here.
rlancer commentedon Dec 10, 2015
AppEngine has an interesting implementation which allows for queries to be converted to a next page token
https://cloud.google.com/appengine/docs/java/datastore/queries#Java_Query_cursors
[-]Fast stateless pagination[/-][+]Fast stateless pagination and efficient indexed count()[/+]danielmewes commentedon Apr 29, 2016
I renamed the issue from "Fast stateless pagination" to "Fast stateless pagination and efficient indexed count()" because both issues will (probably) be solved by the same technical solution.
v3ss0n commentedon Jun 16, 2016
i think this one is also a "need" to have in 2.4 ? since you guys touching counts in changefeeds too ?
danielmewes commentedon Jun 16, 2016
@v3ss0n This isn't required for changefeeds on counts. Changefeeds on counts will still be linear-time for the initial result, and then just update in constant time for each change.
ShadowJonathan commentedon Jun 30, 2020
Is this issue still being counted after the reboot of RethinkDB? I'm getting "mysterious" sustained 100% iowait at idle (with no jobs running), slow/frozen queries, and ofc a "slow"
.count()operation time.thelinuxlich commentedon Jun 30, 2020
Yes, all the issues you mentioned are still true as of today.