Hi,
I am using 7.1.3 Community version.
I’ve created an index by using this statement
CREATE INDEX `occurred_at_index` ON `audit`(`occurred_at`)
It’s using my index while using this query
select * from audit where occurred_at = 1234;
Query Explain :
{
“~children”: [
{
“keyspace”: “audit”,
“spans”: [
{
“exact”: true,
“range”: [
{
“inclusion”: 3,
“index_key”: “occurred_at
”,
“high”: “1234”,
“low”: “1234”
}
]
}
],
“#operator”: “IndexScan3”,
“using”: “gsi”,
“index_projection”: {
“primary_key”: true
},
“namespace”: “default”,
“index”: “occurred_at_index”,
“index_id”: “54564e8817b57318”
},
{
“namespace”: “default”,
“keyspace”: “audit”,
“#operator”: “Fetch”
},
{
“~child”: {
“~children”: [
{
“#operator”: “Filter”,
“condition”: “((audit
.occurred_at
) = 1234)”
},
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“star”: true,
“expr”: “self”
}
]
}
],
“#operator”: “Sequence”
},
“#operator”: “Parallel”
}
],
“#operator”: “Sequence”
}
It’s good, I have no problem with this.
But on Order By and Group By clause its not using my index.
For Order By:
select * from audit order by occurred_at;
{
“~children”: [
{
“~children”: [
{
“namespace”: “default”,
“keyspace”: “audit”,
“index”: “#primary”,
“#operator”: “PrimaryScan3”,
“using”: “gsi”,
“index_projection”: {
“primary_key”: true
}
},
{
“namespace”: “default”,
“keyspace”: “audit”,
“#operator”: “Fetch”
},
{
“~child”: {
“~children”: [
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“star”: true,
“expr”: “self”
}
]
}
],
“#operator”: “Sequence”
},
“#operator”: “Parallel”
}
],
“#operator”: “Sequence”
},
{
“#operator”: “Order”,
“sort_terms”: [
{
“expr”: “(audit
.occurred_at
)”
}
]
}
],
“#operator”: “Sequence”
}
AND for Group By:
select occurred_at from audit group by occurred_at;
{
“~children”: [
{
“namespace”: “default”,
“keyspace”: “audit”,
“index”: “#primary”,
“#operator”: “PrimaryScan3”,
“using”: “gsi”,
“index_projection”: {
“primary_key”: true
}
},
{
“namespace”: “default”,
“keyspace”: “audit”,
“#operator”: “Fetch”
},
{
“~child”: {
“~children”: [
{
“group_keys”: [
“(audit
.occurred_at
)”
],
“#operator”: “InitialGroup”,
“aggregates”:
}
],
“#operator”: “Sequence”
},
“#operator”: “Parallel”
},
{
“group_keys”: [
“(audit
.occurred_at
)”
],
“#operator”: “IntermediateGroup”,
“aggregates”:
},
{
“group_keys”: [
“(audit
.occurred_at
)”
],
“#operator”: “FinalGroup”,
“aggregates”:
},
{
“~child”: {
“~children”: [
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“expr”: “(audit
.occurred_at
)”
}
]
}
],
“#operator”: “Sequence”
},
“#operator”: “Parallel”
}
],
“#operator”: “Sequence”
}
After creating this →
CREATE INDEX occurred_at ON `audit`(`occurred_at` INCLUDE MISSING)
index my both queries above is working with Order By and Group By clause smoothly.
But when I use Order By clause with DESC it’s not using my index and I need to create another index like this →
CREATE INDEX occurred_atDESC ON `audit`(`occurred_at` DESC INCLUDE MISSING)
With these 2 indexes my queries working well. But creating index consume my resources. So the problem is having 2 indexes for both ASC and DESC because its costs.
In Postgresql we have just one index and this index working well with where, order by or group by.
My questions are;
- Is there anyway to having just one index for where, order by, group by and etc. clauses?
- Why indexes in Couchbase working like this?