gpt4 book ai didi

rethinkdb - 如何在RethinkDB中将getall与orderby一起使用

转载 作者:行者123 更新时间:2023-12-03 16:07:53 32 4
gpt4 key购买 nike

我想列出两个时间戳之间id = 1的记录,最后根据时间戳对其进行排序。

MySQL查询的东西:

Select * from test 
where (timestamp between 100099323 AND 1423699323) AND id=1
order by timestamp

重新思考数据库中有超过500万个文档。

我尝试将index用于简单的mysql查询:
Select * from test where id=1 order by timestamp

和Rethinkdb查询是:
r.table('test').getAll(1, {index: 'id'}).orderBy({index: 'timestamp'})

但我得到错误:
RqlRuntimeError: Indexed order_by can only be performed on a TABLE or 
TABLE_SLICE in:
r.table("test").getAll(1, {index: "id"}).orderBy({index: "timestamp"})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

有什么建议吗?

最佳答案

RethinkDB不支持有效的索引交集(添加此索引的Github问题是#809),但是您可以通过为'id'和'timestamp'索引添加复合索引来有效地实现此查询。

如果您的结果集足够小,则可以通过删除'index'optarg完全在内存中完成orderBy:

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"})

关于rethinkdb - 如何在RethinkDB中将getall与orderby一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28583653/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com