我是在couchbase上的新手,我正在使用N1QL进行一些查询,但需要花费很多时间(9分钟) 我的数据有200.000个文件,文件有嵌套类型,文件里面的嵌套类型数量是6.000.000,分布在200.000个文件之间,所以UNNEST操作很重要。我的数据的一个样本是:
{"p_partkey": 2, "lineorder": [{"customer": [{"c_city": "INDONESIA1"}], "lo_supplycost": 54120, "orderdate": [{"d_weeknuminyear": 19}], "supplier": [{"s_phone": "16-789-973-6601|"}], "commitdate": [{"d_year": 1993}], "lo_tax": 7}, {"customer": [{...
我正在做的一个问题是:
SELECT SUM(l.lo_extendedprice*l.lo_discount*0.01) as revenue
from part p UNNEST p.lineorder l UNNEST l.orderdate o
where o.d_year=1993 and l.lo_discount between 1 and 3 and l.lo_quantity<25;
数据具有上述字段。 但执行需要9分钟。 我只用我的电脑来做,所以只有一个节点。 我的电脑有16GB内存,集群RAM cota是3,2GB,只有一个3GB的存储桶。我的数据总大小为2,45GB。我已经使用了这里提到的计算:http://docs.couchbase.com/admin/admin/Concepts/bp-sizingGuidelines.html来调整我的群集和存储区的大小。 我做错了什么或者这次对于这个数据量是否正确?
现在我创建了索引,如:
CREATE INDEX idx_discount ON part( DISTINCT ARRAY l.lo_discount FOR l IN lineorder END );
CREATE INDEX idx_quantity ON part( DISTINCT ARRAY l.lo_quantity FOR l IN lineorder END );
CREATE INDEX idx_year ON part( DISTINCT ARRAY o.d_year FOR o IN ( DISTINCT ARRAY l.orderdate FOR l IN lineorder END ) END );
但数据库不使用它。
一个查询示例是:
SELECT SUM(l.lo_extendedprice*l.lo_discount*0.01) as revenue
from part p UNNEST p.lineorder l UNNEST l.orderdate o
where o.d_year=1993 and l.lo_discount between 1 and 3 and l.lo_quantity<25;
另一个例子,我创建了索引:
CREATE INDEX teste3 ON `part` (DISTINCT ARRAY l.lo_quantity FOR l IN lineorder END );
并询问:
select l.lo_quantity from part as p UNNEST p.lineorder l where l.lo_quantity>20 limit 3
因为我删除了主索引,所以它不会执行。返回错误: “键空间部分没有主索引。使用CREATE PRIMARY INDEX创建一个。”,
答案 0 :(得分:2)
在阅读了http://blog.couchbase.com/2016/may/1.making-most-of-your-arrays..-with-covering-array-indexes-and-more上的博客后,我发现了问题:
如果你像这样创建INDEX:
CREATE INDEX iflight_day
ON `travel-sample` ( DISTINCT ARRAY v.flight FOR v IN schedule END );
您必须在查询中使用相同的字母,在本例中为字母“v”。
SELECT v.day from `travel-sample` as t UNNEST t.schedule v where v.flight="LY104";
最深层次的情况也一样:
CREATE INDEX inested ON `travel-sample`
( DISTINCT ARRAY (DISTINCT ARRAY y.flight FOR y IN x.special_flights END) FOR x IN schedule END);
在这种情况下,你必须使用'y'和'x':
SELECT x.day from `travel-sample` as t UNNEST t.schedule x UNNEST x.special_flights y where y.flight="AI444";
现在一切正常。
但是当我这样查询时出现了另一个问题:
SELECT * from `travel-sample` as t UNNEST t.schedule x UNNEST x.special_flights y
where x.day=7 and y.flight="AI444";
仅使用如上所述创建的日期索引。
CREATE INDEX day
ON `travel-sample` ( DISTINCT ARRAY y.day FOR y IN schedule END );
它只使用一个索引,有时是'天',有时'inested'。
答案 1 :(得分:1)
您可以将Couchbase 4.5(GA即将推出)与阵列索引配合使用。数组索引可以与UNNEST一起使用。它允许您索引数组的各个元素,包括嵌套在其他数组中的数组。
您可以创建以下索引,然后使用EXPLAIN确保使用您想要的索引存在IndexScan。
CREATE INDEX idx_discount ON part( DISTINCT ARRAY l.lo_discount FOR l IN lineorder END );
CREATE INDEX idx_quantity ON part( DISTINCT ARRAY l.lo_quantity FOR l IN lineorder END );
CREATE INDEX idx_year ON part( DISTINCT ARRAY ( DISTINCT ARRAY o.d_year FOR o IN l.orderdate END ) FOR l IN lineorder END );