Couchbase顺序通过重新调整null结果

时间:2016-03-17 14:25:18

标签: couchbase n1ql

我用这种方式使用N1QL查询我的数据库:

<tr>
  <td>
    <div class="restyle">red</div>
  </td>
  <td>
    this will color along
  </td>
</tr>

但它返回收入: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) 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;

但这需要花费很多时间,我想在UNNEST之前过滤这些文件,并向UNNEST提交所有文件。这是一个错误,还是我做错了什么?

我的数据是这样的:

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;

这是解释的输出,它按功能显示顺序,但它返回null:

{
        "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:

{
    "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
    }
}

1 个答案:

答案 0 :(得分:0)

子查询中缺少FinalProject。修复将包含在即将推出的4.5 Beta中。