I have a query that is taking more time then required for execution. Below is my query (Its taking about 27 to 30 seconds)
SELECT
s.platform,
COUNT(DISTINCT s.channel) AS Channels,
COUNT(DISTINCT s.advertisement) AS Advertisements,
SUM(CASE WHEN s.click = 1 THEN 1 ELSE 0 END) AS TotalClicks,
SUM(CASE WHEN s.impression == 1 THEN 1 ELSE 0 END) AS TotalImpressions,
COUNT (DISTINCT (CASE WHEN s.click = 1 THEN ip ELSE NULL END)) As UniqueClicks,
COUNT (DISTINCT (CASE WHEN s.impression = 1 THEN ip ELSE NULL END)) As UniqueImpressions,
COUNT (DISTINCT (CASE WHEN s.impression = 1 THEN f2fsession ELSE NULL END)) As UniqueSessionImpressions,
COUNT(DISTINCT (CASE WHEN s.click = 1 THEN f2fsession ELSE NULL END)) As UniqueSessionClicks
FROM
Advertisement AS s
WHERE
s.date >= ‘2017-01-27’ AND s.date <= ‘2017-01-31’ GROUP BY s.platform ORDER BY platform;
Addition to this i have created the following indexes
CREATE PRIMARY INDEX #primary
ON Advertisement
CREATE INDEX date_idx_advertisement
ON Advertisement
(date
)
CREATE INDEX id_ix_advertisment
ON Advertisement
((meta().id
))
CREATE INDEX platform_idx_advertisement
ON Advertisement
(platform
)
And below is the explain of my query
[
{
“plan”: {
"#operator": “Sequence”,
"~children": [
{
"#operator": “Sequence”,
"~children": [
{
"#operator": “IndexScan”,
“index”: “date_idx_advertisement”,
“index_id”: “1ea8091a22342352”,
“keyspace”: “Advertisement”,
“namespace”: “default”,
“spans”: [
{
“Range”: {
“High”: [
"“2017-01-31"”
],
“Inclusion”: 3,
“Low”: [
"“2017-01-27"”
]
}
}
],
“using”: “gsi”
},
{
"#operator": “Parallel”,
"~child": {
"#operator": “Sequence”,
"~children": [
{
"#operator": “Fetch”,
“as”: “s”,
“keyspace”: “Advertisement”,
“namespace”: “default”
},
{
"#operator": “Filter”,
“condition”: “((“2017-01-27” <= (s
.date
)) and ((s
.date
) <= “2017-01-31”))”
},
{
"#operator": “InitialGroup”,
“aggregates”: [
“count(distinct (s
.advertisement
))”,
“count(distinct (s
.channel
))”,
“count(distinct case when ((s
.click
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.click
) = 1) then (s
.ip
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.ip
) else null end)”,
“sum(case when ((s
.click
) = 1) then 1 else 0 end)”,
“sum(case when ((s
.impression
) = 1) then 1 else 0 end)”
],
“group_keys”: [
"(s
.platform
)"
]
}
]
}
},
{
"#operator": “IntermediateGroup”,
“aggregates”: [
“count(distinct (s
.advertisement
))”,
“count(distinct (s
.channel
))”,
“count(distinct case when ((s
.click
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.click
) = 1) then (s
.ip
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.ip
) else null end)”,
“sum(case when ((s
.click
) = 1) then 1 else 0 end)”,
“sum(case when ((s
.impression
) = 1) then 1 else 0 end)”
],
“group_keys”: [
"(s
.platform
)"
]
},
{
"#operator": “FinalGroup”,
“aggregates”: [
“count(distinct (s
.advertisement
))”,
“count(distinct (s
.channel
))”,
“count(distinct case when ((s
.click
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.click
) = 1) then (s
.ip
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.f2fsession
) else null end)”,
“count(distinct case when ((s
.impression
) = 1) then (s
.ip
) else null end)”,
“sum(case when ((s
.click
) = 1) then 1 else 0 end)”,
“sum(case when ((s
.impression
) = 1) then 1 else 0 end)”
],
“group_keys”: [
"(s
.platform
)"
]
},
{
"#operator": “Parallel”,
"~child": {
"#operator": “Sequence”,
"~children": [
{
"#operator": “InitialProject”,
“result_terms”: [
{
“expr”: “(s
.platform
)”
},
{
“as”: “Channels”,
“expr”: “count(distinct (s
.channel
))”
},
{
“as”: “Advertisements”,
“expr”: “count(distinct (s
.advertisement
))”
},
{
“as”: “TotalClicks”,
“expr”: “sum(case when ((s
.click
) = 1) then 1 else 0 end)”
},
{
“as”: “TotalImpressions”,
“expr”: “sum(case when ((s
.impression
) = 1) then 1 else 0 end)”
},
{
“as”: “UniqueClicks”,
“expr”: “count(distinct case when ((s
.click
) = 1) then (s
.ip
) else null end)”
},
{
“as”: “UniqueImpressions”,
“expr”: “count(distinct case when ((s
.impression
) = 1) then (s
.ip
) else null end)”
},
{
“as”: “UniqueSessionImpressions”,
“expr”: “count(distinct case when ((s
.impression
) = 1) then (s
.f2fsession
) else null end)”
},
{
“as”: “UniqueSessionClicks”,
“expr”: “count(distinct case when ((s
.click
) = 1) then (s
.f2fsession
) else null end)”
}
]
}
]
}
}
]
},
{
"#operator": “Order”,
“sort_terms”: [
{
“expr”: “(s
.platform
)”
}
]
},
{
"#operator": “FinalProject”
}
]
},
“text”: “SELECT \n s.platform, \n COUNT(DISTINCT s.channel) AS Channels, \n COUNT(DISTINCT s.advertisement) AS Advertisements, \n SUM(CASE WHEN s.click = 1 THEN 1 ELSE 0 END) AS TotalClicks, \n SUM(CASE WHEN s.impression == 1 THEN 1 ELSE 0 END) AS TotalImpressions, \n COUNT (DISTINCT (CASE WHEN s.click = 1 THEN ip ELSE NULL END)) As UniqueClicks, \n COUNT (DISTINCT (CASE WHEN s.impression = 1 THEN ip ELSE NULL END)) As UniqueImpressions,\n COUNT (DISTINCT (CASE WHEN s.impression = 1 THEN f2fsession ELSE NULL END)) As UniqueSessionImpressions, \n COUNT(DISTINCT (CASE WHEN s.click = 1 THEN f2fsession ELSE NULL END)) As UniqueSessionClicks \nFROM \n Advertisement AS s \nWHERE \n s.date >= ‘2017-01-27’ AND s.date <= ‘2017-01-31’ GROUP BY s.platform ORDER BY platform;”
}
]
CouchBase Server Version used : 4.5.0-2601 Community Edition (build-2601)
Can some one please help me to know what index i am missing or why it is taking too long.
Your kind co-operation is highly appreciated.