我有以下查询
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到500ms ? 我的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
}
]
}
答案 0 :(得分: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条件。它总是评估为真。不确定你是否需要第3次加入。
HTH, -Prasad