このように N1QL を使用してデータベースにクエリを実行しています。
SELECT sum(l.lo_revenue) as revenue, o.d_year, p.p_brand1 from (SELECT p_brand1, lineorder from part where p_brand1='MFGR#2221') as p UNNEST p.lineorder l UNNEST l.supplier s UNNEST l.orderdate o
where s.s_region='EUROPE'
group by o.d_year, p.p_brand1
order by o.d_year, p.p_brand1
limit 5;
しかし、それは収益を返します: null
次のように、並べ替えなしでクエリを実行すると、正しい答えが得られますが、並べ替えられていません。
SELECT sum(l.lo_revenue) as revenue, o.d_year, p.p_brand1 from (SELECT p_brand1, lineorder from part where p_brand1='MFGR#2221') as p UNNEST p.lineorder l UNNEST l.supplier s UNNEST l.orderdate o
where s.s_region='EUROPE'
group by o.d_year, p.p_brand1
order by o.d_year, p.p_brand1
limit 5;
この方法でもクエリを実行でき、正しい答えがソートされます。
SELECT SUM(l.lo_revenue), o.d_year, p.p_brand1
from part p UNNEST p.lineorder l UNNEST l.supplier s UNNEST l.orderdate o
where p.p_brand1='MFGR#2221' and s.s_region='EUROPE'
group by o.d_year, p.p_brand1
order by o.d_year, p.p_brand1;
しかし、時間がかかるので、すべてのドキュメントを UNNEST するのではなく、UNNEST の前にドキュメントをフィルター処理したいと考えています。それはバグですか、それとも何か間違っていますか?
私のデータは次のようなものです:
{
"p_partkey": 1,
"p_name": "lace spring",
"p_mfgr": "MFGR#1",
"p_category": "MFGR#11",
"p_brand1": "MFGR#1121",
"p_color": "goldenrod",
"p_type": "PROMO BURNISHED COPPER",
"p_size": 7,
"p_container": "JUMBO PKG",
"lineorder": [{
"lo_orderkey": 504065,
"lo_linenumber": 6,
"lo_custkey": 14704,
"lo_partkey": 1,
"lo_suppkey": 557,
"lo_orderdate": 19920603,
"lo_orderpriority": "5-LOW",
"lo_shippriority": "0",
"lo_quantity": 49,
"lo_extendedprice": 4414900,
"lo_ordtotalprice": 26849571,
"lo_discount": 3,
"lo_revenue": 4282453,
"lo_supplycost": 54060,
"lo_tax": 0,
"lo_commitdate": 19920712,
"lo_shipmode": "RAIL",
"orderdate": [{
"d_datekey": 19920603,
"d_date": "June 3, 1992",
"d_dayofweek": "Thursday",
"d_month": "June",
"d_year": 1992,
"d_yearmonthnum": 199206,
"d_yearmonth": "jun\/92",
"d_daynuminweek": 5,
"d_daynuminmonth": 3,
"d_daynuminyear": 155,
"d_monthnuminyear": 6,
"d_weeknuminyear": 23,
"d_sellingseason": "Summer",
"d_lastdayinweekfl": false,
"d_lastdayinmonthfl": true,
"d_holidayfl": false,
"d_weekdayfl": true
}],
"commitdate": [{
"d_datekey": 19920712,
"d_date": "July 12, 1992",
"d_dayofweek": "Monday",
"d_month": "July",
"d_year": 1992,
"d_yearmonthnum": 199207,
"d_yearmonth": "jul\/92",
"d_daynuminweek": 2,
"d_daynuminmonth": 12,
"d_daynuminyear": 194,
"d_monthnuminyear": 7,
"d_weeknuminyear": 28,
"d_sellingseason": "Summer",
"d_lastdayinweekfl": false,
"d_lastdayinmonthfl": true,
"d_holidayfl": false,
"d_weekdayfl": true
}],
"customer": [{
"c_custkey": 14704,
"c_name": "Customer#000014704",
"c_address": "uZaxFV8o9IGgayUEWtPU1Xmw",
"c_city": "JORDAN 5",
"c_nation": "JORDAN",
"c_region": "MIDDLE EAST",
"c_phone": "23-688-772-4209",
"c_mktsegment": "BUILDING"
}],
"supplier": [{
"s_suppkey": 557,
"s_name": "Supplier#000000557",
"s_address": "jj0wUYh9K3fG5Jh",
"s_city": "CANADA 5",
"s_nation": "CANADA",
"s_region": "AMERICA",
"s_phone": "13-390-153-6699"
}]
}, {
"lo_orderkey": ...}
これは Explain の出力です。関数ごとの順序を示していますが、null を返します。
{
"requestID": "654ee29a-69b9-437a-9f0e-242ac936b4f7",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "part",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "part",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "((`part`.`p_brand1`) = \"MFGR#1121\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`part`.`p_brand1`)"
},
{
"expr": "(`part`.`lineorder`)"
}
]
}
]
}
}
]
},
{
"#operator": "Alias",
"as": "p"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Unnest",
"as": "l",
"expr": "(`p`.`lineorder`)"
},
{
"#operator": "Unnest",
"as": "s",
"expr": "(`l`.`supplier`)"
},
{
"#operator": "Unnest",
"as": "o",
"expr": "(`l`.`orderdate`)"
},
{
"#operator": "Filter",
"condition": "((`s`.`s_region`) = \"AMERICA\")"
},
{
"#operator": "InitialGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
}
]
}
},
{
"#operator": "IntermediateGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
},
{
"#operator": "FinalGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "InitialProject",
"result_terms": [
{
"as": "revenue",
"expr": "sum((`l`.`lo_revenue`))"
},
{
"as": "year",
"expr": "(`o`.`d_year`)"
},
{
"as": "p_brand1",
"expr": "(`p`.`p_brand1`)"
}
]
}
]
}
}
]
},
{
"#operator": "Order",
"sort_terms": [
{
"expr": "(`o`.`d_year`)"
},
{
"expr": "(`p`.`p_brand1`)"
}
]
},
{
"#operator": "FinalProject"
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "8.921246ms",
"executionTime": "8.838345ms",
"resultCount": 1,
"resultSize": 6915
}
}
順序なしで EXPLAIN に続く:
cbq> EXPLAIN
> {CT SUM(l.lo_revenue) as revenue, o.d_year as year, p.p_brand1 as p_brand1
> {1121') as p UNNEST p.lineorder l UNNEST l.supplier s UNNEST l.orderdate o
> where s.s_region='AMERICA'
> group by o.d_year, p.p_brand1
> ;
{
"requestID": "160cbad9-1d32-4d67-9c94-1623bba27d51",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "part",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "part",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "((`part`.`p_brand1`) = \"MFGR#1121\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`part`.`p_brand1`)"
},
{
"expr": "(`part`.`lineorder`)"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
{
"#operator": "Alias",
"as": "p"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Unnest",
"as": "l",
"expr": "(`p`.`lineorder`)"
},
{
"#operator": "Unnest",
"as": "s",
"expr": "(`l`.`supplier`)"
},
{
"#operator": "Unnest",
"as": "o",
"expr": "(`l`.`orderdate`)"
},
{
"#operator": "Filter",
"condition": "((`s`.`s_region`) = \"AMERICA\")"
},
{
"#operator": "InitialGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
}
]
}
},
{
"#operator": "IntermediateGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
},
{
"#operator": "FinalGroup",
"aggregates": [
"sum((`l`.`lo_revenue`))"
],
"group_keys": [
"(`o`.`d_year`)",
"(`p`.`p_brand1`)"
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "InitialProject",
"result_terms": [
{
"as": "revenue",
"expr": "sum((`l`.`lo_revenue`))"
},
{
"as": "year",
"expr": "(`o`.`d_year`)"
},
{
"as": "p_brand1",
"expr": "(`p`.`p_brand1`)"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "10.661133ms",
"executionTime": "10.575926ms",
"resultCount": 1,
"resultSize": 5600
}
}