Couchbase N1QL查询真的很慢

时间:2016-05-31 10:32:35

标签: couchbase n1ql nosql

我正在开发一个使用Couchbase 4.1的项目,我们正在尝试使用N1QL来查询文档。问题是,即使我创建了索引,它似乎也很慢。使用~11000个文档,查询大约需要2秒钟。

查询:

{
  "parentName": "8442",
  "categoryPath": "category-displayname/subcategory-displayName",
  "lastUpdated": "2016-05-31T11:02:03.5129252+02:00",
  "childCategories": [
    {
      "name": "0041",
      "displayName": "Category 1",
      "imageId": "0041"
    },
    {
      "name": "0042",
      "displayName": "Category 2",
      "imageId": "0042"
    },
    {
      "name": "0043",
      "displayName": "Category 3",
      "imageId": "0043"
    },
    {
      "name": "0044",
      "displayName": "Category 4",
      "imageId": "0044"
    },
    {
      "name": "0045",
      "displayName": "Category 5",
      "imageId": "0045"
    },
    {
      "name": "0046",
      "displayName": "Category 6",
      "imageId": "0046"
    }
  ],
  "assortment": "CategoryAssortmentOne",
  "name": "0040",
  "displayName": "MyCategory",
  "imageId": "0040"
}

我的文档看起来像这样:

CREATE INDEX `category_idx` ON `bd-couchbase`((meta().`id`),`name`,`displayName`,`imageId`,`categoryPath`,`childCategories`,`assortment`) USING GSI;

我有以下索引:

{
    "requestID": "da1946f3-5cc8-4d1e-a05b-06789aa6be92",
    "signature": "json",
    "results": [
        {
            "#operator": "Sequence",
            "~children": [
                {
                    "#operator": "PrimaryScan",
                    "index": "#primary",
                    "keyspace": "my-couchbase",
                    "namespace": "default",
                    "using": "gsi"
                },
                {
                    "#operator": "Parallel",
                    "~child": {
                        "#operator": "Sequence",
                        "~children": [
                            {
                                "#operator": "Fetch",
                                "keyspace": "my-couchbase",
                                "namespace": "default"
                            },
                            {
                                "#operator": "Filter",
                                "condition": "((((`my-couchbase`.`assortment`) =
 \"CategoryAssortmentOne\") and ((`my-couchbase`.`categoryPath`) = \"category-displayname/subcategory-displayName\")) and ((`my-couchbase`.`displayName`) is not missing))"
                            },
                            {
                                "#operator": "InitialProject",
                                "result_terms": [
                                    {
                                        "expr": "(`my-couchbase`.`name`)"
                                    },
                                    {
                                        "expr": "(`my-couchbase`.`displayName`)"

                                    },
                                    {
                                        "expr": "(`my-couchbase`.`imageId`)"
                                    },
                                    {
                                        "expr": "(`my-couchbase`.`childCategorie
s`)"
                                    }
                                ]
                            },
                            {
                                "#operator": "FinalProject"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "13.6696ms",
        "executionTime": "13.6696ms",
        "resultCount": 1,
        "resultSize": 2089
    }
}

当我执行解释时,我可以看到它使用#primary索引,然后进行提取(我猜这是查询的缓慢部分)。但是,当我创建我的索引时,不应该使用它吗?

我解释的结果:

GROUP BY

有什么建议吗?

提前致谢。

1 个答案:

答案 0 :(得分:4)

查询服务似乎无法将您的索引与查询匹配。你在索引中包含meta.id的任何特殊原因吗?

尝试重新定义索引以仅覆盖WHERE子句中使用的字段:assortmentcategoryPathdisplayName,并查看之后是否在EXPLAIN中提及它。

CREATE INDEX category_idx ON `bd-couchbase`(assortment, categoryPath, displayName, imageId, childCategories, name);