Rethinkdb getAll ,带索引的 orderBy - 标签

Rethinkdb getAll , orderBy with index - Tags

我是 rethinkdb 的新手,我喜欢它,但是当我尝试优化我的查询并使其适用于更大的数据集时,我发现了一些问题。 问题很简单。

我需要按时间戳 (row.to)、标签 (row.tags)、按时间戳排序 (row.from) 过滤我的 "event" table然后切片分页。

row.tags 有一个多索引并且效果很好!

row.from 和 row.to 是 start/end 事件时间。

慢速查询(在 100k 个条目上测试)是这样的:

r.db("test").table("event")
.getAll(r.args(["148a6e03-b6c3-4092-afa0-3b6d1a4555cd","7008d4b0-d859-49f3-b9e0-2e121f000ddf"]), {"index": "tags"})
.filter(function(row) {return row("to").ge(r.epochTime(1480460400));})
.orderBy(r.asc("from"))
.slice(0,20)

我在 'from' 上创建了一个索引并尝试做

.orderBy(r.asc("from"),{index:'from'})

但我明白了

e: Indexed order_by can only be performed on a TABLE or TABLE_SLICE in:

我已经阅读过有关 Rethinkdb 中索引交集的问题,但也许我错过了什么,也许有一种方法可以完成这个简单的任务。

谢谢。

RethinkDB 抱怨的原因是: getAll returns 个 selection。当 filter 应用于 selection 它 returns 一个 selection。当 orderBy 应用于 selection 时,无法使用 index 参数(它只能在 orderBy 应用于 table 时使用)。

orderBy 可以应用于 tablesequenceselection只有应用于table时才能使用index参数。这是有道理的,因为在 table.

中添加和删除行时索引会更新

在您的情况下,您正在对 filter 的结果应用 orderBy,即 selection。为了对选择进行排序,数据库需要:

  1. 将所有元素读入内存(默认最大为 100,000 个元素)
  2. 使用提供的函数或字段对它们进行排序

并且在这种情况下不能使用索引。

改进查询的方法可能是先对 table 进行排序,然后再应用过滤器。在这种情况下您将能够使用索引。