Skip to content

Efficient way to find orphaned documents? #4566

Closed

Description

@marshall007

I'm basically trying to do an inverse of eqJoin where the right-hand side is null. Is there an efficient way to do something like this?

r.table('diff_results').eqJoin('diff_id', r.table('diffs')).filter({ right: null })

Using filter is proving to be rather slow. Left table contains ~1.7M docs, right table contains ~4K docs:

r.table('diff_results').filter(function (row) {
  return r.table('diffs').get(row('diff_id')).eq(null);
})

Activity

  1. marshall007 commented on Jul 23, 2015

    @marshall007
    ContributorAuthor

    I think this would be solved by the include_missing optarg mentioned in #2720, but is there a workaround in the meantime?

  2. thelinuxlich commented on Jul 23, 2015

    @thelinuxlich

    Outer join(hope that someday this will be easier to write)

    r.table("foo").concatMap(function(s) {
        return r.table("bar").getAll(s("join_id")).coerceTo('ARRAY').do(function(data) {
          return r.branch(data.isEmpty(),[{left: s, right: null}],data.map(function(c) {
            return { left: s, right: c };
            }));
        });
    })
    
  3. danielmewes commented on Jul 23, 2015

    @danielmewes
    Member

    I think at the moment the filter with the get is the most efficient one. At least I cannot think of a more efficient alternative. I suspect that the outer join that @thelinuxlich describes is also going to be a bit slower than the filter(other.get.ne(null)).

  4. marshall007 commented on Jul 24, 2015

    @marshall007
    ContributorAuthor

    Thanks for the suggestions. I also tried loading all the keys in from the right table first, but that didn't help much either:

    r.table('right')('id')
    .map(r.object(r.row, true))
    .reduce(function (l, r) { return l.merge(r); })
    .do(function (keys) {
      return r.table('left').filter(function (row) {
        return keys.hasFields(row('right_id')).not()
      })
    })

    I would have thought that loading 4K keys into a hashmap upfront and doing lookups against that would have been significantly faster than calling .get() 1.7M times. @danielmewes any insight into this?

    Aside from that, my questions have been answered so feel free to close this in favor of #2720.

  5. danielmewes commented on Jul 24, 2015

    @danielmewes
    Member

    @marshall007 Ah, that's an interesting thought. While objects aren't stored as hash-maps on the server (instead they are sorted arrays of key/value pairs, on which we perform binary search to look up a key), I would still imagine this to be somewhat faster if there are few documents in the second table since it avoids all kinds of usual subquery processing overhead.

    I wonder if the slow part in this case is building the object with 4K keys one by one. Every element being added to the object in the reduction function will essentially build a new object structure, including having the keys sorted again.

    A slightly different way of doing that step that might or might not be faster is:

    r.table('right')('id')
    .map([[r.row, true]])
    .reduce(function (l, r) { return l.add(r); })
    .coerceTo('object')
    .do(function (keys) {
      return r.table('left').filter(function (row) {
        return keys.hasFields(row('right_id')).not()
      })
    })
  6. marshall007 commented on Jul 24, 2015

    @marshall007
    ContributorAuthor

    @danielmewes I was mistaken yesterday, loading the keys upfront is actually ~4-5x faster!

    You're right, good catch. I always forget about #3408. The time it took to build up the object was pretty negligible compared to the total query duration, though.

    Your suggestion can be further simplified which also makes it a bit faster:

    r.table('right')('id').map([ r.row, true ]).coerceTo('array').coerceTo('object')
    
    // original:             500ms
    // coerceTo:             55ms
    // coerceTo simplified:  38ms

    I found it slightly odd that sequence.coerceTo('object') fails. It seems like that should implicitly coerce to array first.

  7. mlucy commented on Jul 24, 2015

    @mlucy
    Member

    @marshall007 -- long term the plan is to implement #2721 which should fix the .coerceTo('object') case as a side-effect.

  8. danielmewes commented on Jul 24, 2015

    @danielmewes
    Member

    Ah, good to know that this is faster.

    Closing as a duplicate of #2720.
    Once this functionality is supported by a join command, we should just spend our effort on making that generic command as fast as possible.

  9. added this to the duplicate milestone on Jul 24, 2015
  10. marshall007 commented on Jul 25, 2015

    @marshall007
    ContributorAuthor

    Sorry to bump this again, but one more quick question. I don't know enough about how indices are persisted or the data structures used to speak about this intelligently (is it documented somewhere?), but would it be possible to optimize the case of getting primary/secondary index values from a table? For example, is table('id') any more efficient than table('any_other_field') or do both require scanning every document? Likewise, it seems like table.distinct({ index: 'secondary' }) ends up doing a lot of reads and I'd be interested to better understand why that's necessary.

  11. danielmewes commented on Jul 25, 2015

    @danielmewes
    Member

    @marshall007 This is currently not optimized and whether you read the primary key or another field is equally expensive. See #4342 for the secondary index case. In short it's often not necessary. We just need to implement the optimized code path for this case.

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