Dear all,
I have two similar queries for which I get different (unexpected) result.
I try to be clear as possible. The 2 queries have the same field “hit by” the where condition, that is an array called searckKeyA.
query 1 - results OK, but slow
select * from products where "photofast" in searchKeyA;
query 2 - results KO, but fast, indexed
select * from products where ANY v IN searchKeyA satisfies v = "photofast" end;
With the query word “photofast” the first query fetches 3 documents (that’s OK), the second only 1.
I double-checked to be sure that the array field “searchKeyA” contains “photofast” in every of the 3 documents.
I think the reason may be hidden beyond the array index I built on searchKeyA.
create index idx on products(all array v FOR v IN searchKeyA END, searchKeyA) using gsi;
(older index, same result - using "distinct")
create index idx on products(distinct array v FOR v IN searchKeyA END, searchKeyA) using gsi;
The plans of the 2 queries are different because of this specific index I described above.
Query 1 is executed as a PrimaryScan; Query 2 has the following plan instead:
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "DistinctScan",
"scan": {
"#operator": "IndexScan",
"index": "idx",
"index_id": "ee3c38798f820ce6",
"keyspace": "products",
"namespace": "default",
"spans": [
{
"Exact": true,
"Range": {
"High": [
"successor(\"photofast\")"
],
"Inclusion": 1,
"Low": [
"\"photofast\""
]
}
}
],
"using": "gsi"
}
},
{
"#operator": "Fetch",
"keyspace": "products",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "any `v` in (`products`.`searchKeyA`) satisfies (`v` = \"photofast\") end"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
"text": "\nselect *\nfrom products\nwhere ANY v IN searchKeyA satisfies v = \"photofast\" end;"
Shouldn’t the result set be the same for the 2 queries?
Can you tell me what makes this difference? Any suggestion would be appreciated.
PS: the 2 queries give the same result set (3 rows, correct) when I drop “idx”
Thank you, my best regards.
Stefano