【问题标题】:CouchBase Index nested elementsCouchBase 索引嵌套元素
【发布时间】:2016-10-26 10:37:09
【问题描述】:

在 CouchBase(4.1.1) 中,N1QL 可以使用整个元素创建索引,以这两个示例为例。

假设我们有这个文档结构:

{
   "name": "blah",
   "credentials": [
       {
          "level": 5,
          "sector": "XP"
       }
   ],
}

现在假设我们想根据名称和整个凭据元素创建 index1,这可能吗?

类似

create index indexName on `bucketName` (name, credentials) USING GSI;

或 index2 基于名称和嵌套字段之一,如 level;怎么可能做到这一点?像

create index indexName on `bucketName`(name, credential.levels) USING GSI; 

运行解释时,我的二级索引没有被使用,couchbase 默认为这个存储桶的主索引。

这是我正在使用的选择。

select s.name, s.credentials 
from `security` unnest s.credentials
where credentials is not missing and name = 'tom';

这是生成的解释:

{
    "requestID": "f8d46eeb-3898-4ace-a24f-1582e0504eb7",
    "signature": "json",
    "results": [
        {
            "#operator": "Sequence",
            "~children": [
                {
                    "#operator": "PrimaryScan",
                    "index": "#primary",
                    "keyspace": "read",
                    "namespace": "default",
                    "using": "gsi"
                },
                {
                    "#operator": "Parallel",
                    "~child": {
                        "#operator": "Sequence",
                        "~children": [
                            {
                                "#operator": "Fetch",
                                "as": "s",
                                "keyspace": "bucketName",
                                "namespace": "default"
                            },
                            {
                                "#operator": "Unnest",
                                "as": "beacons",
                                "expr": "(`s`.`credentials`)"
                            },
                            {
                                "#operator": "Filter",
                                "condition": "((`s`.`name`) = \"tom\")"
                            },
                            {
                                "#operator": "InitialProject",
                                "result_terms": [
                                    {
                                        "expr": "(`s`.`id`)"
                                    },
                                    {
                                        "expr": "`credentials`"
                                    }
                                ]
                            },
                            {
                                "#operator": "FinalProject"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "2.82063ms",
        "executionTime": "2.765439ms",
        "resultCount": 1,
        "resultSize": 1917
    }
}

【问题讨论】:

  • 请编辑问题以指明 (1) Couchbase 版本,(2) 您要运行的查询(包括完整的查询),以及 (3) 您所说的“这些不适用于我”(发生了什么事?)。
  • 我刚刚在 Couchbase 上运行了它们,它们执行正确。我假设你所说的“不为我工作”的意思是你正在运行一个不使用这些索引的 SELECT ?如果您包含您正在使用的 SELECT 和它的 EXPLAIN 将会有所帮助(这可能是@geraldss 的意思)
  • @mgroves 我已经添加了示例查询
  • @geraldss 这是一个模拟示例,但在很大程度上模仿了实际文档中我感兴趣的字段的结构。实际文档非常大,并且包含大量敏感信息,因此我无法提供实际查询。希望我们仍然可以通过模拟示例解决

标签: database indexing couchbase n1ql nosql


【解决方案1】:

根据我的发现,如果您尝试创建索引,则必须排除不包含您正在索引的字段的文档。因此,对于上面的示例,一旦我添加:name is not missingwhere 语句中,我的查询就开始使用我的二级索引。

Couchbase site 下面的简介让我得出了这个发现

注意:MISSING 项目不被索引器索引。为了利用覆盖索引并使索引符合条件,查询需要排除索引键表达式计算结果为 MISSING 的文档。

【讨论】:

    【解决方案2】:

    以下查询应使用您的索引。

    select s.name, s.credentials 
    from `security` s
    where s.credentials is not missing and s.name = 'tom';
    
    select s.name, credentials 
    from `security` s unnest s.credentials
    where s.credentials is not missing and s.name = 'tom';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 2016-04-19
      • 2017-04-12
      • 2013-01-17
      相关资源
      最近更新 更多