2017-01-05 89 views
-1

我有以下查詢性能提高,couchbase使用指數

explain SELECT * FROM (select ROUND(sum(ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost)),2) total_revenue, 
ROUND(sum(CASE WHEN DailyCampaignUsage.day between '2016-05-01' and '2016-05-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_revenue, 
ROUND(sum(CASe WHEN DailyCampaignUsage.day between '2016-04-01' and '2016-04-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_prev_revenue 
from Inheritx DailyCampaignUsage use index(dailyCampaignUsage_type_day_clicksCost) 
JOIN Inheritx Campaign ON KEYS ('Campaign|'||TOSTRING(DailyCampaignUsage.campaignId)) 
JOIN Inheritx Users on keys('User|'|| TOSTRING(Campaign.`user`)) 
WHERE DailyCampaignUsage._type='DailyCampaignUsage' and CASE WHEN FALSE THEN Users.`user` in FALSE ELSE TRUE END) AS __viewdef__ ORDER BY `created` DESC 

我有以下指標

CREATE INDEX dailyCampaignUsage_type_day_clicksCost ON Inheritx 
(_type,day,`statistics`[*].clicksCost) WHERE _type='DailyCampaignUsage' 

我在查詢中使用。

我的解釋計劃是打擊。

{ 
    "plan": { 
     "#operator": "Sequence", 
     "~children": [ 
     { 
      "#operator": "Sequence", 
      "~children": [ 
      { 
       "#operator": "Sequence", 
       "~children": [ 
       { 
        "#operator": "IndexScan", 
        "index": "dailyCampaignUsage_type_day_clicksCost", 
        "index_id": "37387d27d560354b", 
        "keyspace": "Inheritx", 
        "namespace": "default", 
        "spans": [ 
        { 
         "Range": { 
         "High": [ 
          "successor(\"DailyCampaignUsage\")" 
         ], 
         "Inclusion": 1, 
         "Low": [ 
          "\"DailyCampaignUsage\"" 
         ] 
         } 
        } 
        ], 
        "using": "gsi" 
       }, 
       { 
        "#operator": "Parallel", 
        "~child": { 
        "#operator": "Sequence", 
        "~children": [ 
         { 
         "#operator": "Fetch", 
         "as": "DailyCampaignUsage", 
         "keyspace": "Inheritx", 
         "namespace": "default" 
         }, 
         { 
         "#operator": "Join", 
         "as": "Campaign", 
         "keyspace": "Inheritx", 
         "namespace": "default", 
         "on_keys": "(\"Campaign|\" || to_string((`DailyCampaignUsage`.`campaignId`)))" 
         }, 
         { 
         "#operator": "Join", 
         "as": "Users", 
         "keyspace": "Inheritx", 
         "namespace": "default", 
         "on_keys": "(\"User|\" || to_string((`Campaign`.`user`)))" 
         }, 
         { 
         "#operator": "Filter", 
         "condition": "(((`DailyCampaignUsage`.`_type`) = \"DailyCampaignUsage\") and case when false then ((`Users`.`user`) in false) else true end)" 
         }, 
         { 
         "#operator": "InitialGroup", 
         "aggregates": [ 
          "sum(array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)))", 
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-04-01\" and \"2016-04-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)", 
          "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-05-01\" and \"2016-05-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-04-01\" and \"2016-04-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)", 
        "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-05-01\" and \"2016-05-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-04-01\" and \"2016-04-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end)", 
        "sum(case when ((`DailyCampaignUsage`.`day`) between \"2016-05-01\" and \"2016-05-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-05-01\" and \"2016-05-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-04-01\" and \"2016-04-23\") then array_sum((array_star((`DailyCampaignUsage`.`statistics`)).`clicksCost`)) else 0 end), 2)" 
          } 
         ] 
         }, 
         { 
         "#operator": "FinalProject" 
         } 
        ] 
        } 
       } 
       ] 
      }, 
      { 
       "#operator": "Alias", 
       "as": "__viewdef__" 
      }, 
      { 
       "#operator": "Parallel", 
       "~child": { 
       "#operator": "Sequence", 
       "~children": [ 
        { 
        "#operator": "InitialProject", 
        "result_terms": [ 
         { 
         "expr": "self", 
         "star": true 
         } 
        ] 
        } 
       ] 
       } 
      } 
      ] 
     }, 
     { 
      "#operator": "Order", 
      "sort_terms": [ 
      { 
       "desc": true, 
       "expr": "(`__viewdef__`.`created`)" 
      } 
      ] 
     }, 
     { 
      "#operator": "FinalProject" 
     } 
     ] 
    }, 
    "text": "SELECT * FROM (select ROUND(sum(ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost)),2) total_revenue,\nROUND(sum(CASE WHEN DailyCampaignUsage.day between '2016-05-01' and '2016-05-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_revenue,\nROUND(sum(CASe WHEN DailyCampaignUsage.day between '2016-04-01' and '2016-04-23' THEN ARRAY_SUM(DailyCampaignUsage.`statistics`[*].clicksCost) ELSE 0 END),2) period_prev_revenue \nfrom Inheritx DailyCampaignUsage use index(dailyCampaignUsage_type_day_clicksCost)\nJOIN Inheritx Campaign ON KEYS ('Campaign|'||TOSTRING(DailyCampaignUsage.campaignId)) \nJOIN Inheritx Users on keys('User|'|| TOSTRING(Campaign.`user`)) \nWHERE DailyCampaignUsage._type='DailyCampaignUsage' and CASE WHEN FALSE THEN Users.`user` in FALSE ELSE TRUE END) AS __viewdef__ ORDER BY `created` DESC" 
    } 

偶數索引使用我無法減少它的執行。它是13s 我怎麼可以做到這一點300至500毫秒? 的json象下面我有50K + JSON

DailyCampaignUsage|006657c0-c696-11e6-b6f2-7f0166ec7527{ 
     "_id": "006657c0-c696-11e6-b6f2-7f0166ec7527", 
     "_type": "DailyCampaignUsage", 
     "campaignId": 249, 
     "day": "2015-11-19T00:00:00Z", 
     "statistics": [ 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     {}, 
     { 
      "clicks": 1741, 
      "clicksCost": 48.748 
     } 
     ] 
    } 

回答

1

1)你可以先嚐試優化內部查詢。通過包含索引定義中where/projections/join-on-keys中使用的第一個鍵空間的所有字段來使用覆蓋索引。見https://developer.couchbase.com/documentation/server/4.5/indexes/covering-indexes.html。文檔中的最後一個示例適用於您。例如:

CREATE INDEX dailyCampaignUsage_type_day_clicksCost ON Inheritx 
(_type,day, campaignId, `statistics`[*].clicksCost) WHERE _type='DailyCampaignUsage' 

2)您可以嘗試內存優化索引(MOI)來極大提高性能。這需要企業版。請參閱https://developer.couchbase.com/documentation/server/4.5/architecture/global-secondary-indexes.html#story-h2-2

3)外部查詢僅通過'創建'來進行排序,而不是由內部查詢投影。如果它位於第一個鍵空間中,請將其包含在索引中。

4)同時檢查Users.user上CASE的WHERE條件。它總是評估爲真。不知道你是否需要第三次加入。

心連心, -Prasad

+0

所有結果都那麼它會做被外界秩序。用戶條件只適用於管理員,如果它將其他用戶有條件喜歡users.'用戶在[2,3,56,78,等]它的相關用戶 –

+0

使用您的第一個鏈接我做了單獨的查詢,並使他們聯合但你也檢查我的這個問題也http://stackoverflow.com/questions/41544159/index-not-improve-speed-in-couchbase-4-5 –