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

时间:2021-08-21 15:41:25

Given two tables:

给定两个表:

  • t1: a table with elements, and
  • t1:包含元素的表
  • t2: another table with list of IDs processed from t1
  • t2:从t1处理的id列表的另一个表

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?

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

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

为了清晰,我在寻找函数等式

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

从t1中选择*。id不在(从t2中选择pid)

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

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

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(); })

r.db(mydb).table(t1)。过滤器(函数(u){返回r.db(mydb).table(t2)(“pid”)。包含(u(" id "))自身之外();})

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?

然而,这是非常低效的:具体来说,它要求t2中的每个元素都要用大汤匙来对付t1。有什么方法可以更有效地回报这个问题吗?

Many thanks!

很多谢谢!

1 个解决方案

#1


3  

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

你可以通过在t2上创建一个二级索引来实现这一点:

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

#1


3  

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

你可以通过在t2上创建一个二级索引来实现这一点:

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