Skip to content

Fast stateless pagination and efficient indexed count() #3949

Open

Description

@thelinuxlich

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.

Activity

  1. added this to the subsequent milestone on Mar 19, 2015
  2. danielmewes commented on Mar 19, 2015

    @danielmewes
    Member

    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.

  3. timmaxw commented on Mar 19, 2015

    @timmaxw
    Member

    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.

  4. thelinuxlich commented on Mar 19, 2015

    @thelinuxlich
    Author

    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.

  5. timmaxw commented on Mar 19, 2015

    @timmaxw
    Member

    I think you misread my comment. I'm not suggesting a workaround; I'm talking about the limitations of the solution that @danielmewes proposed.

  6. danielmewes commented on Mar 19, 2015

    @danielmewes
    Member

    I think @thelinuxlich was clarifying on my "By stateless, do you mean being able to randomly access any given page by index?" question.

  7. thelinuxlich commented on Mar 19, 2015

    @thelinuxlich
    Author

    That's right :)

  8. timmaxw commented on Mar 19, 2015

    @timmaxw
    Member

    Ah, sorry I misinterpreted you.

  9. leandropincini commented on Mar 19, 2015

    @leandropincini

    +1 for this one...
    I'm having the same kind of "performance" issue here.
    Maybe with @danielmewes solution we're getting a start point.

  10. danielmewes commented on Mar 20, 2015

    @danielmewes
    Member

    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:

    1. 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.
    2. 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.
    3. 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.
    4. 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.

  11. avimar commented on Aug 19, 2015

    @avimar

    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..

  12. danielmewes commented on Oct 14, 2015

    @danielmewes
    Member

    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.

  13. danielmewes commented on Oct 14, 2015

    @danielmewes
    Member

    @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.

  14. rlancer commented on Dec 10, 2015

    @rlancer

    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

  15. changed the title [-]Fast stateless pagination[/-] [+]Fast stateless pagination and efficient indexed count()[/+] on Apr 29, 2016
  16. danielmewes commented on Apr 29, 2016

    @danielmewes
    Member

    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.

  17. v3ss0n commented on Jun 16, 2016

    @v3ss0n

    i think this one is also a "need" to have in 2.4 ? since you guys touching counts in changefeeds too ?

  18. danielmewes commented on Jun 16, 2016

    @danielmewes
    Member

    @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.

  19. ShadowJonathan commented on Jun 30, 2020

    @ShadowJonathan

    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.

  20. thelinuxlich commented on Jun 30, 2020

    @thelinuxlich
    Author

    Yes, all the issues you mentioned are still true as of today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions