Hi guys,
I am new to the Couchbase and (N1ql) world, and I have a query which I don’t really know how to optimize:
SELECT DISTINCT `subject` AS distinct_subject, MIN(`lastModification`) AS modified, `subject` AS subject FROM block WHERE 1 = 1
-- and subject IN [...]
-- and type IN [...]
-- and lastModification >= <A-DATE>
GROUP BY `subject` ORDER BY `modified` ASC LIMIT 10 OFFSET 0
This query is generated dynamically based on a set of filters the user provides. These parameters are “subject”, “type” and “lastModification”.
I have some indexes for subject, type and lastModification which of course improves the query response when these parameters are present. The problem is when there are no filters present.
An explain for this query (without filters) returns the following response:
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "block_idx",
"keyspace": "block",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "block",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "(1 = 1)"
},
{
"#operator": "InitialGroup",
"aggregates": [
"min((`block`.`lastModification`))"
],
"group_keys": [
"(`block`.`subject`)"
]
}
]
}
},
{
"#operator": "IntermediateGroup",
"aggregates": [
"min((`block`.`lastModification`))"
],
"group_keys": [
"(`block`.`subject`)"
]
},
{
"#operator": "FinalGroup",
"aggregates": [
"min((`block`.`lastModification`))"
],
"group_keys": [
"(`block`.`subject`)"
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "InitialProject",
"distinct": true,
"result_terms": [
{
"as": "distinct_subject",
"expr": "(`block`.`subject`)"
},
{
"as": "modified",
"expr": "min((`block`.`lastModification`))"
},
{
"as": "subject",
"expr": "(`block`.`subject`)"
}
]
},
{
"#operator": "Distinct"
}
]
}
},
{
"#operator": "Distinct"
}
]
},
{
"#operator": "Order",
"limit": "10",
"offset": "0",
"sort_terms": [
{
"expr": "`modified`"
}
]
},
{
"#operator": "Offset",
"expr": "0"
},
{
"#operator": "Limit",
"expr": "10"
},
{
"#operator": "FinalProject"
}
]
},
"text": "SELECT DISTINCT `subject` AS distinct_subject, MIN(`lastModification`) AS modified, `subject` AS subject FROM block WHERE 1 = 1\nGROUP BY `subject` ORDER BY `modified` ASC LIMIT 10 OFFSET 0"
}
]
My question is, is there any way to re-write the query in order to avoid a PrimaryScan? Any hint that I can use?
Thanks!.
M.-