查询中未使用Couchbase数组索引

查询中未使用Couchbase数组索引,couchbase,n1ql,Couchbase,N1ql,我的文件结构如下: { "customerId": "", "schemeId": "scheme-a", "type": "account", "events": [ { "dateTime": "2019-03-14T02:23:58.573Z", "id": "72998bbf-94a6-4031-823b-6c304707ad49", "type": "DebitDisabled", "authoris

我的文件结构如下:


{    
  "customerId": "",
  "schemeId": "scheme-a",  
  "type": "account",
  "events": [
    {
      "dateTime": "2019-03-14T02:23:58.573Z",
      "id": "72998bbf-94a6-4031-823b-6c304707ad49",
      "type": "DebitDisabled",
      "authorisedId": ""
    },
    {
      "dateTime": "2018-05-04T12:40:15.439Z",
      "transactionReference": "005171-15-1054-7571-60990-20180503165536",      
      "id": "005171-15-1054-7571-60990-20180503165536-1",
      "type": "Credit",
      "authorisedId": ",
      "value": 34,
      "funder": "funder-a"
    },
    {
      "dateTime": "2019-03-06T04:14:54.564Z",
      "transactionReference": "000000922331",
      "eventDescription": {
        "language": "en-gb",
        "text": "
      },
      "id": "000000922331",
      "type": "Credit",
      "authorisedId": "",
      "value": 16,
      "funder": "funder-b"
    },
    {
      "dateTime": "2019-03-10T04:24:17.903Z",
      "transactionReference": "000001510154",
      "eventDescription": {
        "language": "en-gb",
        "text": ""
      },
      "id": "000001510154",
      "type": "Credit",
      "authorisedId": "",
      "value": 10,
      "funder": "funder-c"
    }
  ]
}
以及以下指标:

CREATE INDEX `scheme-a_customers_index` 
ON `default`(`type`,`schemeId`,`customerId`) 
WHERE ((`schemeId` = "scheme-a") and (`type` = "account")) 
WITH { "num_replica":1 }

CREATE INDEX `scheme-a_credits_index` 
ON `default`(
`type`,
`schemeId`,
`customerId`,
(distinct (array (`e`.`funder`) for `e` in `events` when ((`e`.`type`) = "Credit") end))
) 
WHERE ((`type` = "scheme") and (`schemeId` = "scheme-a")) 
WITH { "num_replica":1 }
我试图查询每个where type=“credit”和类似于“funder%”的funder的所有客户ID和事件

以下是我的疑问:

SELECT 
    customerId, 
    (ARRAY v.`value` FOR v IN p.events WHEN v.type = "Credit" AND v.funder like "funder%" END) AS credits 
FROM default AS p 
WHERE p.type = "account" AND p.schemeId = "scheme-a" 
AND (ANY e IN p.events SATISFIES e.funder = "funder-a" END) 


我希望查询使用索引
scheme-a\u credits\u index
,而不是使用
scheme-a\u customers\u index
。不明白为什么!查询不应该使用
scheme-a\u credits\u索引吗

您的查询在customerId上没有谓词。所以查询只能将两个谓词推送到索引器,并且两个索引都是限定的。scheme-a_-customers_索引效率更高,因为由于非数组索引,索引中的条目数量更多

你应该试试下面的方法

CREATE INDEX `ix1` ON `default`
(DISTINCT ARRAY e.funder FOR e IN events WHEN e.type = "Credit" END, `customerId`)
WHERE ((`schemeId` = "scheme-a") and (`type` = "account")) ;

SELECT
    customerId,
    (ARRAY v.`value` FOR v IN p.events WHEN v.type = "Credit" AND v.funder like "funder%" END) AS credits
FROM default AS p
WHERE p.type = "account" AND p.schemeId = "scheme-a"
AND (ANY e IN p.events SATISFIES e.funder LIKE "funder%" AND e.type = "Credit" END);