need to create Index for N1QL query

随声附和 提交于 2020-01-07 03:08:12

问题


I have below query

     select ROUND(sum(ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost)),2) total_revenue,
ROUND(sum(CASE WHEN  DailyCampaignUsage.day between '2016-12-01' and '2016-12-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_revenue,
ROUND(sum(CASe WHEN  DailyCampaignUsage.day between '2016-11-01' and '2016-11-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_prev_revenue 
from Inheritx DailyCampaignUsage
 WHERE DailyCampaignUsage._type='DailyCampaignUsage'

I have also created below Index even when I run this query it is taking 9.09s

1)

CREATE INDEX `DailyCampaignUsage_Day` ON `Inheritx`(`day`) WHERE 
((`_type` = "DailyCampaignUsage") or (`_type` is missing)) USING GSI

2)

CREATE INDEX `DailyCampaignUsage_type` ON `Inheritx`(`_type`) WHERE 
((`_type` = "DailyCampaignUsage") or (`_type` is missing)) USING GSI

my explain plan is below

{
    "#operator": "Sequence",
    "~children": [
      {
        "#operator": "IndexScan",
        "index": "DailyCampaignUsage_type",
        "keyspace": "Inheritx",
        "namespace": "default",
        "spans": [
          {
            "Range": {
              "High": [
                "\"DailyCampaignUsage\""
              ],
              "Inclusion": 3,
              "Low": [
                "\"DailyCampaignUsage\""
              ]
            }
          }
        ],
        "using": "gsi"
      },
      {
        "#operator": "Parallel",
        "~child": {
          "#operator": "Sequence",
          "~children": [
            {
              "#operator": "Fetch",
              "as": "DailyCampaignUsage",
              "keyspace": "Inheritx",
              "namespace": "default"
            },
            {
              "#operator": "Filter",
              "condition": "((`DailyCampaignUsage`.`_type`) = \"DailyCampaignUsage\")"
            },
            {
              "#operator": "InitialGroup",
              "aggregates": [
                "sum(array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)))",
                "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-11-01\" and \"2016-11-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)",
                "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-12-01\" and \"2016-12-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)"
              ],
              "group_keys": []
            }
          ]
        }
      },
      {
        "#operator": "IntermediateGroup",
        "aggregates": [
          "sum(array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)))",
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-11-01\" and \"2016-11-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)",
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-12-01\" and \"2016-12-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)"
        ],
        "group_keys": []
      },
      {
        "#operator": "FinalGroup",
        "aggregates": [
          "sum(array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)))",
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-11-01\" and \"2016-11-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)",
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-12-01\" and \"2016-12-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)"
        ],
        "group_keys": []
      },
      {
        "#operator": "Parallel",
        "~child": {
          "#operator": "Sequence",
          "~children": [
            {
              "#operator": "InitialProject",
              "result_terms": [
                {
                  "as": "total_revenue",
                  "expr": "round(sum(array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`))), 2)"
                },
                {
                  "as": "period_revenue",
                  "expr": "round(sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-12-01\" and \"2016-12-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end), 2)"
                },
                {
                  "as": "period_prev_revenue",
                  "expr": "round(sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-11-01\" and \"2016-11-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end), 2)"
                }
              ]
            },
            {
              "#operator": "FinalProject"
            }
          ]
        }
      }
    ]
  }

What I am missing here ?

why it is taking too much time ??

can you give me correct index ??

来源:https://stackoverflow.com/questions/41283154/need-to-create-index-for-n1ql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!