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 ??