RethinkDB:有效地selectid不存在于另一个表中
给定两个表格:
- t1:包含元素的表格
- t2:具有从t1处理的ID列表的另一个表
你如何查询t1中所有尚未处理的元素,即t2中没有对应的pid?
为了清楚起见,我正在寻找的functionequalent
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(); })
然而,这是非常低效的:特别是,它需要对t2中的每个元素进行全面的表扫描,而不是t1。 有什么方法可以更有效地回报吗?
非常感谢!
你可以通过在t2
为pid
创build一个二级索引来做到这一点:
r.table('t2').indexCreate('pid') r.table('t1').filter(function(u) { return r.table('t2').getAll(u('id'), {index: 'pid'}).isEmpty(); })