【问题标题】:How to use getall with orderby in RethinkDB如何在 RethinkDB 中使用 getall 和 orderby
【发布时间】:2015-02-18 12:25:16
【问题描述】:

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

Mysql查询一些东西:

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

rethink 数据库中有超过 500 万份文档。

我尝试使用索引进行简单的 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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2014-10-18
      相关资源
      最近更新 更多