下面是我的查询:
1. SELECT META(m).id, m.sch_name AS name FROM `pricing_qa_1` AS m UNNEST m.pri_sch_ref AS r WHERE m.dtype = "mprisch" AND r IN( SELECT RAW META(a).id FROM `pricing_qa_1` AS a UNNEST a.prod_prof AS p UNNEST p.fx_prof AS f UNNEST f.spl_fx AS s WHERE a.dtype = "prisch" AND (f.fx_sch = "25" OR s.fx_sch = "25") ) ORDER BY LOWER(name) ASC
2. SELECT META(m).id, m.sch_name AS name FROM `pricing_qa_1` AS m UNNEST m.pri_sch_ref AS r WHERE m.dtype = "mprisch" AND r IN( SELECT RAW META(a).id FROM `pricing_qa_1` AS a UNNEST a.prod_prof AS p WHERE a.dtype = "prisch" AND (p.prod_cd = "WMOBAC000A1") ) ORDER BY LOWER(name) ASC
显示结果需要2分钟。我需要了解如何调整可能的索引。
答案 0 :(得分:0)
首先要尝试的是:
create index dtype_idx on pricing_qa_1(dtype)
您可以使用EXPLAIN the_query查看查询计划并找出正在使用的索引。
有关选择有效索引的更多信息,请尝试本文:
https://blog.couchbase.com/create-right-index-get-right-performance/
答案 1 :(得分:0)
您可以尝试以下索引和查询。
CREATE INDEX ix1 ON pricing_qa_1 (LOWER(sch_name), pri_sch_ref, sch_name) WHERE dtype = "mprisch";
CREATE INDEX ix2 ON pricing_qa_1 (DISTINCT ARRAY p.prod_cd FOR p IN prod_prof END) WHERE dtype = "prisch";
SELECT META(m).id, m.sch_name AS name
FROM `pricing_qa_1` AS m
WHERE m.dtype = "mprisch" AND LOWER(m.sch_name) IS NOT NULL
AND ANY r IN m.pri_sch_ref SATISFIES r IN ( SELECT RAW META(a).id
FROM `pricing_qa_1` AS a
WHERE a.dtype = "prisch"
AND ANY p IN a.prod_prof SATISFIES p.prod_cd = "WMOBAC000A1" END)
ORDER BY LOWER(m.sch_name) ASC;
SELECT META(m).id, m.sch_name AS name
FROM `pricing_qa_1` AS m
WHERE m.dtype = "mprisch" AND LOWER(m.sch_name) IS NOT NULL
AND ANY r IN m.pri_sch_ref SATISFIES r IN ( SELECT RAW META(a).id
FROM `pricing_qa_1` AS a
WHERE a.dtype = "prisch"
AND ANY p IN a.prod_prof SATISFIES
(ANY f IN p.fx_prof SATISFIES f.fx_sch = "25" OR
"25" IN f.spl_fx[*].fx_sch END) END)
ORDER BY LOWER(m.sch_name) ASC;
如果子查询结果很小,您还可以尝试以下操作:
1) Execute subquery first and get the results in application
2) Pass the right side of IN clause as query parameter from previous step and
execute query as adhoc=true
3) change ix1 as array index https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/indexing-arrays.html