过滤器 + orderby 的 RethinkDB 索引 [英] RethinkDB index for filter + orderby

查看:58
本文介绍了过滤器 + orderby 的 RethinkDB 索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个 comments 表具有以下结构:

Lets say a comments table has the following structure:

id | author | timestamp | body

我想使用索引来有效地执行以下查询:

I want to use index for efficiently execute the following query:

r.table('comments').getAll("me", {index: "author"}).orderBy('timestamp').run(conn, callback)

我可以使用其他有效的方法吗?

看起来当前索引不支持表的过滤结果.为 timestamp 创建索引并将其添加为 orderBy('timestamp', {index: timestamp}) 中的提示时,我收到以下错误:

It looks that currently index is not supported for a filtered result of a table. When creating an index for timestamp and adding it as a hint in orderBy('timestamp', {index: timestamp}) I'm getting the following error:

RqlRuntimeError:索引 order_by 只能在 TABLE 上执行.在:

RqlRuntimeError: Indexed order_by can only be performed on a TABLE. in:

推荐答案

这可以通过作者"和时间戳"字段上的复合索引来完成.您可以像这样创建这样的索引:

This can be accomplished with a compound index on the "author" and "timestamp" fields. You can create such an index like so:

r.table("comments").index_create("author_timestamp", lambda x: [x["author"], x["timestamp"]])

然后您可以使用它来执行查询,如下所示:

Then you can use it to perform the query like so:

r.table("comments")
 .between(["me", r.minval], ["me", r.maxval]
 .order_by(index="author_timestamp)

between 的工作就像 get_all 在原始查询中所做的那样,因为它只获取具有作者我"和任何时间戳的文档.然后我们在同一个索引上做一个 order_by,按时间戳排序(因为所有的键都有相同的作者.)这里的关键是每个表访问只能使用一个索引,所以我们需要把所有这些信息塞进去相同的索引.

The between works like the get_all did in your original query because it gets only documents that have the author "me" and any timestamp. Then we do an order_by on the same index which orders by the timestamp(since all of the keys have the same author.) the key here is that you can only use one index per table access so we need to cram all this information in to the same index.

这篇关于过滤器 + orderby 的 RethinkDB 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆