RethinkDB:​​通过不存在于另一个 table 中的 id 进行有效选择

RethinkDB: effectively selecting by id not present in another table

给定两个 tables:

如何查询t1中所有尚未处理的元素,即t2中没有对应的pid?

为清楚起见,我正在寻找

的函数等价物

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

或者,一种实现简单处理队列以确定 t1 中哪些新元素尚未处理的方法。

更新:

目前我在以下位置:

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

然而,这是非常低效的:具体来说,它需要针对 t1 对 t2 中的每个元素进行完整 table 扫描。有什么方法可以更有效地 return 吗?

非常感谢!

您可以通过在 t2 上为 pid 创建二级索引来做到这一点:

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