EXPLAIN of the not working query, before creating the index you suggested:
[
{
“plan”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “DistinctScan”,
“scan”: {
“#operator”: “IndexScan”,
“covers”: [
“cover ((bucket
.countryname
))”,
“cover ((bucket
.cityname
))”,
“cover ((distinct (array (city
.auth_id
) for city
in (bucket
.authors
) end)))”,
“cover ((meta(bucket
).id
))”
],
“index”: “idx_countryname_cityname_auths”,
“index_id”: “b291df65543e8d06”,
“keyspace”: “bucket”,
“namespace”: “default”,
“spans”: [
{
“Exact”: true,
“Range”: {
“High”: [
““Switzerland””,
“successor(“Geneve”)”
],
“Inclusion”: 1,
“Low”: [
““Switzerland””,
““Geneve””
]
}
}
],
“using”: “gsi”
}
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “Filter”,
“condition”: “((cover ((bucket
.cityname
)) = “Geneve”) and (cover ((bucket
.countryname
)) = “Switzerland”))”
},
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“expr”: “cover ((bucket
.cityname
))”
},
{
“expr”: “cover ((bucket
.countryname
))”
}
]
},
{
“#operator”: “FinalProject”
}
]
}
}
]
},
“text”: “SELECT cityname, countryname\r\nFROM bucket\r\nWHERE cityname = ‘Geneve’ AND countryname = ‘Switzerland’”
}
]
The default index choosen from the RDBMS, is the following one:
CREATE INDEX idx_countryname_cityname_auths
ON bucket
(countryname
,cityname
,(distinct (array (city
.auth_id
) for city
in authors
end)))
I created the index you told me, and now it works! But in this moment, I’m really confused: how could that be possible? I thought that the use of indexes could not affect the result of a query, but just the execution time as indexes are just useful as optimization.
By the way the EXPLAIN with your index is the following one:
[
{
“plan”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “IndexScan”,
“covers”: [
“cover ((bucket
.cityname
))”,
“cover ((bucket
.countryname
))”,
“cover ((meta(bucket
).id
))”
],
“index”: “idx_cityname_countryname”,
“index_id”: “e0cc44b3419b750”,
“keyspace”: “bucket”,
“namespace”: “default”,
“spans”: [
{
“Exact”: true,
“Range”: {
“High”: [
““Geneve””,
““Switzerland””
],
“Inclusion”: 3,
“Low”: [
““Geneve””,
““Switzerland””
]
}
}
],
“using”: “gsi”
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “Filter”,
“condition”: “((cover ((bucket
.cityname
)) = “Geneve”) and (cover ((bucket
.countryname
)) = “Switzerland”))”
},
{
“#operator”: “InitialProject”,
“result_terms”: [
{
“expr”: “cover ((bucket
.cityname
))”
},
{
“expr”: “cover ((bucket
.countryname
))”
}
]
},
{
“#operator”: “FinalProject”
}
]
}
}
]
},
“text”: “SELECT cityname, countryname\r\nFROM bucket\r\nWHERE cityname = ‘Geneve’ AND countryname = ‘Switzerland’”
}
]