Silver Dragon
Silver Dragon

Reputation: 5560

RethinkDB: effectively selecting by id not present in another table

Given two tables:

How do you query for all the elements in t1, which were not yet processed, that is, does not have a corresponding pid in t2?

For clarity, I'm looking for the functional equalent of

Select * from t1 where t1.id not in (select pid from t2)

Alternatively, a way to implement a simple processing queue to determine which new elements in t1 have not been processed yet.

Update:

Currently I'm at the following:

r.db("mydb").table("t1").filter( function(u) { return r.db("mydb").table("t2")("pid").contains( u("id") ).not(); })

However, this is horribly inefficient: specifically, it requires full tablescans for every element in t2 against t1. Are there any ways to return this more efficiently?

Many thanks!

Upvotes: 2

Views: 135

Answers (1)

mlucy
mlucy

Reputation: 5289

You can do this by creating a secondary index on t2 for pid:

r.table('t2').indexCreate('pid')
r.table('t1').filter(function(u) {
  return r.table('t2').getAll(u('id'), {index: 'pid'}).isEmpty();
})

Upvotes: 3

Related Questions