CouchBase:是否有可能加快没有条件的查询?

时间:2019-09-06 11:12:01

标签: indexing nosql couchbase n1ql

我正在尝试通过创建索引来加快在CouchBase上的性能。我的查询之一是这样的:

SELECT p.arcid
FROM `flightplans` as p
WHERE ANY route_correct_status IN (
    SELECT DISTINCT arcid,
    ARRAY point.id FOR point IN route.point
    WHEN point.status = "correct-status" END AS list_points_correct_status
    FROM `flightplans`
) SATISFIES route_correct_status.arcid = p.arcid
AND route_correct_status.list_points_correct_status[0] = p.somepoint END

我尝试提取内部SELECT来找到合适的索引,然后我意识到其中没有任何条件。我尝试为此创建索引的所有操作均失败。但是使用主索引对我来说太慢了。

有没有一种方法可以加快存储桶中每个元素的数组构建速度?总的来说,索引(或其他某种机制)可以帮助没有条件的查询吗? (可能是通过预先计算?)

谢谢

1 个答案:

答案 0 :(得分:1)

使用ANSI Join查询。 https://blog.couchbase.com/ansi-join-support-n1ql/

CREATE INDEX ix1 ON flightplans( DISTINCT ARRAY point.status FOR point IN route.point END);
CREATE INDEX ix2 ON flightplans(arcid, somepoint);

SELECT p.arcid
   FROM   (SELECT DISTINCT cs.arcid,
            FIRST point.id FOR point IN cs.route.point WHEN point.status = "correct-status" END AS pointid
       FROM `flightplans` AS cs
       WHERE ANY point IN cs.route.point SATISFIES point.status = "correct-status" END) AS d
JOIN `flightplans` AS p ON d.arcid = p.arcid AND d.pointid = p.somepoint;

如果您一直在寻找“正确状态”,则可以在两个选择都必须使用覆盖索引之后使用

CREATE INDEX ix1 ON flightplans( FIRST point.id FOR point IN route.point WHEN point.status = "correct-status" END, arcid);
CREATE INDEX ix2 ON flightplans(arcid, somepoint);

SELECT p.arcid
    FROM  (SELECT DISTINCT cs.arcid,
            FIRST point.id FOR point IN cs.route.point WHEN point.status = "correct-status" END AS pointid
       FROM `flightplans` AS cs
       WHERE (FIRST point.id FOR point IN cs.route.point WHEN point.status = "correct-status" END) IS NOT NULL) AS d
JOIN `flightplans` AS p ON d.arcid = p.arcid AND d.pointid = p.somepoint;