我想列出两个时间戳之间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'索引添加复合索引来有效地实现此查询。
但是,如果您的结果集足够小,则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"})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句