RethinkDB 不支持高效的索引交集(添加此内容的 Github 问题是#809),但是您可以通过为“id”和“timestamp”索引添加复合索引来有效地实现此查询。
不过,如果您的结果集足够小,orderBy
可以通过删除“index”optarg 来完全在内存中完成:
r.table("test").getAll(1, {index: "id"}).orderBy("timestamp")
为了对大型结果集有效地执行此操作,您需要一个索引。假设您的“id”和“timestamp”索引直接对应于行中的字段,添加索引将如下所示:
r.table("test").indexCreate("id_time",
function(row) {
return [row("id"), row("timestamp")];
})
获取所有行id=1
并按时间戳排序,然后运行:
r.table("test").between([1], [2], {"index": "id_time"})
.orderBy({"index": "id_time"})
此外,回到您发布的原始查询,您可以在两个时间戳之间查询id=1
通过运行:
r.table("test").between([1, <time 1>], [1, <time 2>], {"index": "id_time"})
.orderBy({"index": "id_time"})