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

什么

我想列出两个时间戳之间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"})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

有什么建议吗?

Tryneus

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring中将OrderBy与findAll一起使用

如何在 Laravel 中将 orderBy() 与模型的方法一起使用

如何在Spring Data中将OrderBy与findAll一起使用

如何在mongodb中将查找与$ push一起使用

如何在Laravel中将docker与npm一起使用?

如何在 Angular 中将 patchValue 与 FormArray 一起使用?

如何在 Windows 中将 curl 与 xampp 一起使用?

如何在RSpec中将`allow`与`let`一起使用

如何在TestNG中将@Parameters与ITestContext一起使用?

如何在Airflow中将MySqlOperator与xcom一起使用?

如何在bash中将`tail`与`timeout`一起使用

如何在JavaScript中将ArrayBuffers与DataViews一起使用

如何在Firefox中将`jpm`与webextension一起使用?

如何在Primefaces中将contextMenu与ContentFlow一起使用

如何在React中将PropTypes与Typescript一起使用

如何在Java中将枚举与值一起使用

如何在 Angular 中将 ngFor 与 Observable 一起使用?

如何在Python中将if语句与数组一起使用?

如何在RecyclerView中将DataBinding与LiveData一起使用

如何在awk中将模式与变量一起使用

如何在Swift中将UILongPressGestureRecognizer与UICollectionViewCell一起使用?

如何在OSX中将OpenCV与python一起使用?

如何在reactjs中将usestate与对象一起使用?

如何在VSCode中将cmder与powershell一起使用?

如何在Rails中将jQuery与Sunspot一起使用?

如何在 C 中将 EOF 与 fgets() 一起使用

如何在 IonButton 中将 href 与 onClick() 一起使用?

如何在Flutter中将JWT与WebSocketChannel一起使用

如何在SwiftUI中将ObservedObject与DatePicker一起使用?