嵌套数组查询的Couchbase索引

时间:2017-01-13 11:13:39

标签: arrays indexing couchbase n1ql

我正在尝试通过数组索引来加速下面的查询,我无法做到正确。

SELECT count(*) FROM pacientes UNNEST COMPOSITION.content contentItem 
WHERE contentItem.items IS NOT NULL AND ANY i WITHIN contentItem.items 
SATISFIES i.archetype_id="at0007" AND i.`value`.`value` < 200 AND 
i.`value`.`value` > 100 END;

我尝试过创建以下索引,但没有一个正在运行:

CREATE INDEX idx_item_value ON pacientes (ARRAY i FOR i IN 
COMPOSITION.content.items.`value`.`value` WHEN i.archetype_id="at0007" END);

CREATE INDEX idx_item ON pacientes
(ARRAY i FOR i IN COMPOSITION.content.items END);

CREATE INDEX idx_item ON pacientes UNNEST COMPOSITION.content contentItem
(ARRAY i FOR i IN contentItem.items WHEN i.archetype_id="at0007" END);

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

在CREATE INDEX语句中,使用DISTINCT ARRAY而不是ARRAY。

以下是修订后的查询和索引。

SELECT count(*)
FROM pacientes AS p
UNNEST COMPOSITION.content AS contentItem
WHERE
        ANY ci IN p.COMPOSITION.content SATISFIES
            ( ANY i WITHIN ci.items SATISFIES
                i.archetype_id="at0007"
                AND i.`value`.`value` < 200
                AND i.`value`.`value` > 100 END
            )
        END
;

CREATE INDEX idx_archetype ON pacientes( DISTINCT ARRAY ( DISTINCT ARRAY i.archetype_id FOR i WITHIN ci.items END ) FOR ci IN COMPOSITION.content END );

CREATE INDEX idx_value ON pacientes( DISTINCT ARRAY ( DISTINCT ARRAY i.`value`.`value` FOR i WITHIN ci.items END ) FOR ci IN COMPOSITION.content END );