Skip to content

orderBy and limit #1208

Closed
Closed

Description

@neumino

This query should be optimized

r.table("foo").order_by("bar").limit(1).run()

It's a common use case (as soon as you paginate things), so it would be nice if we wouldn't have to sort the whole table to get the maximum (or the top k) elements.

It was mentionned on this issue, but it's not really what it is about: #159

It's also mentionned in #1174 but there is no issue linked to it

Also mentionned (at least) on this google groups thread https://groups.google.com/forum/?fromgroups#!topic/rethinkdb/8S4Zmpc0FiY

Activity

  1. jdoliner commented on Jul 27, 2013

    @jdoliner
    Contributor

    I actually don't think we should do this. For one thing it really doesn't make this operation enough faster that it changes the scale at which you can use it. It saves storing the objects in memory and transferring all of them over the network (which is a significant savings). But you still have to read every single document off disk which is slow enough that you still won't be able to use this at scale. However this will scale if you use:

    table.index_create("bar").run()
    table.order_by(index="bar").limit(1).run()

    and I think it's a much better policy to tell people to just use an index for efficient order_by. The other big reason is that this is actually incredibly difficult to implement on the backend (FRTAFTN&O) and I think it's going to be a long time (post 2.0) before we want to spend developer time on an optimization that's a substantially less efficient than an alternative that already exists. The only argument I could see for this is that it saves you having to use extra space for an index but I'd much rather spend the time fixing that problem than working around it.

    In general for optimizations I think we should limit ourselves to the 2 ends of the spectrum and try to avoid the middle ground as much as possible. I see a lot of value in having an inefficient way for people to do things when they're just playing around with the product (in memory order_by) and I see a lot of value in having the efficient solution which is maximally efficient algorithmically. However I think if we start mucking about in solutions that are a bit more efficient than an alternative but not as efficient as the best one we'll:

    • waste a lot of dev time
    • make the backend more complicated
    • make the language more complicated and harder to understand the best practices
  2. neumino commented on Jul 27, 2013

    @neumino
    MemberAuthor

    Oh, maybe I didn't really understand what was going on #159
    I thought it was just to let people sort tables with more than one million documents.

    If I do create an index and do

    table.order_by(index="bar").limit(k).run()

    What's the complexity of the query?

  3. jdoliner commented on Jul 29, 2013

    @jdoliner
    Contributor

    Complexity with an index is k log n where n is the number of documents in the database. While the best we could do without an index is n log k which still isn't going to scale to big tables.

  4. neumino commented on Jul 29, 2013

    @neumino
    MemberAuthor

    Oh, that's cool. Should we close this issue then?

  5. jdoliner commented on Jul 29, 2013

    @jdoliner
    Contributor

    As long as your satisfied that indexes would be a good solution close away.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions