I am trying to somehow optimize following query
SELECT *
FROM `bucket`
WHERE external = true AND -createdAt >= 1 AND -createdAt < 2287821078001 AND account IS NOT NULL AND eventType IS NOT NULL
ORDER BY external, -createdAt, account, eventType
LIMIT 100 OFFSET 50000
So I’ve created following index:
CREATE INDEX `externa-index` ON `bucket` (external, -createdAt, account, eventType) WHERE external = true;
In the bucket is in total 2M documents.
-
external
= true (boolean) (does not have it every document ~1M) -
createdAt
is a timestamp (have it every document) -
account
is a String (does not have it every document) -
eventType
is a String (have it every document)
Documents is always needed order by createdAt DESC
(thats the reason why there is an index over -createdAt
)
The issue is that the query obviously uses specified index. But it returns incorrect result set and it is because of:
-createdAt >= 1 AND -createdAt < 2287821078001
condition. How does it works when there is -
suffix? Why it is not working as I expect? I’ve not found any doc related to this.
Thanks for the feedback.
Explain
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "ext-index",
"index_id": "3aca320aa7e5df0d",
"keyspace": "bucket",
"namespace": "default",
"spans": [
{
"Range": {
"High": [
"true",
"null"
],
"Inclusion": 1,
"Low": [
"true"
]
}
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"maxParallelism": 1,
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "bucket",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "((((((`bucket`.`external`) = true) and (2287821078001 <= (-(`bucket`.`createdAt`)))) and ((-(`bucket`.`createdAt`)) < 1)) and ((`bucket`.`account`) is not null)) and ((`bucket`.`eventType`) is not null))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
}
]
}
}
]
},
{
"#operator": "Offset",
"expr": "50000"
},
{
"#operator": "Limit",
"expr": "100"
},
{
"#operator": "FinalProject"
}
]
},
"text": "SELECT *\nFROM `bucket`\nWHERE external = true AND -createdAt >= 2287821078001 AND -createdAt < 1 AND account IS NOT NULL AND eventType IS NOT NULL\nORDER BY external, -createdAt, account, eventType\nLIMIT 100 OFFSET 50000"
}
]