Couchdb,Mango查询和索引 [英] Couchdb, Mango queries and Indexes

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

问题描述

当用户想要搜索数据库时,我使用Couchdb进行Mango查询。他们可以通过文档中的多个字段进行搜索。

I use Mango queries with Couchdb when users want to search the database. They can search by multiple fields in the document.

为了简化,我有一个包含三个字段的文档,所有这些字段都可以被搜索以查找文档。

To simplify I have a document with three fields all of which can be searched to find the document.

FieldOne: cow
FieldTwo: pig
FieldThree: dog

为查询创建芒果索引时,我应该使用哪些字段?我应该用多个字段创建和索引吗?订单是否重要?

When creating a mango index for the query what fields should I use? Should I create and index with multiple fields? Does the order matter?

字段中还定义了三种不同的文档类型(A,B C)。我只需要搜索一个,是否可以从索引中排除其他类型以使其更快?

There are also three different document types (A, B C) defined in a field. I only need to search one, is it possible to exclude the other types from the index to make it faster?

Field_Type: A

能够针对视图运行索引以仅搜索我感兴趣的文件。这可能吗?

It would make sense to me to be able to run an index against a view to only search through the documents I am interested in. Is this possible?

示例索引

一已知出现在查询中的字段的索引

One index on field known to appear in the query

  {
       "index": {
          "fields": [
             "FieldOne"
          ]
       },
       "name": "foo-json-index",
       "type": "json"
    }

多个索引,不确定是否使用?

Multiple indexes, not sure if used or not?

 {
       "index": {
          "fields": [
             "FieldOne",
             "FieldTwo",
             "FieldThree"
          ]
       },
       "name": "foo-json-index",
       "type": "json"
    }

或多个索引在构建查询时选择正确的索引?

Or multiple indexes to choose the correct one when building the query?

这是正确的做法,以获得最快的搜索结果?

which is the correct approach to get the fastest search results?

推荐答案

我不要认为你可以在一般情况下轻松解决这个问题 - 你需要一个特定的选择器(或一组选择器)进行优化。但是,创建可能有帮助的索引时需要考虑以下三个方面:

I don't think you can easily solve this in the general case - you'd need a specific selector (or set of selectors) to optimise for. However, there are 3 aspects to consider when creating an index that may help:


  1. 创建Mango索引时,只有文档包含所有索引字段的索引包含在索引中。您引用的第二个索引示例(包含所有3个字段)完全没问题,只要您始终在查询选择器中指定所有3个字段。

  1. When you create a Mango index, only documents containing all of the indexed fields are included in the index. The second index example you cite, containing all 3 fields, is perfectly fine so long as you are always going to specify all 3 fields in the query selector.

在查询时,只有在根据选择器需要存在所有索引字段时才能使用索引。例如,如果索引包含字段A和B但您只查询A,则我们无法使用索引,因为它不包含包含A但不包含B的文档。

At query time, an index can only be used if all it's indexed fields are required to exist according to the selector. For example, if your index contains fields A and B but you only query for A, we can't use the index because it won't include documents that contain A but not B.

索引中的字段顺序很重要。只有当索引可以为索引字段找到连续的值范围时才能使用索引 - 与_view的复合键相同。例如,假设您在字段A和B上有索引,文档 [{A:1,B:1},{A:1,B:2},{A:2,B :1},{A:2,B:2}] 。索引将如下所示: [[1,1],[1,2],[2,1],[2,2]] 。如果您的查询是 A> = 1 AND B == 2 ,则匹配的文档( [1,2] [2,2] )涵盖所有匹配文件的唯一范围是 [1,2],[2,1],[2] ,2]] - 值 [2,1] 需要在内存中过滤掉。

Order of fields in the index matters. An index can only be used if it can find a contiguous range of values for the indexed fields - the same as a compound key for a _view. For example, let's say you have an index on fields A and B and the documents [{A:1,B:1},{A:1,B:2},{A:2,B:1},{A:2,B:2}]. The index will look like: [[1,1],[1,2],[2,1],[2,2]]. If your query is then A >= 1 AND B == 2, the matching documents ([1,2] and [2,2]) the only range that covers all matching documents is [1,2],[2,1],[2,2]] - the value [2,1] would need to be filtered out in memory.

您可以使用 _explain 端点查看用于完成查询的索引,这应该是给出一些线索,了解索引对于给定的选择器的选择性。

You can see what index is used to fulfil a query using the _explain endpoint, and that should give you some clue as to how selective the index is able to be for a given selector.

这篇关于Couchdb,Mango查询和索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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