Sidenote: Please read this topic for background on this question (specifically #8 response)
Why doesn’t my index kick-in (in the first 3 queries) to give me blazing-fast results? I guess that array operations / filters are hard but the main keys are already there
cbq> select * from system:indexes;
{
"requestID": "cf3b9ed6-18e0-475a-92f4-0cf3489dc8a2",
"signature": {
"*": "*"
},
"results": [
{
"indexes": {
"datastore_id": "http://127.0.0.1:8091",
"id": "582e2bb37cdebf18",
"index_key": [],
"is_primary": true,
"keyspace_id": "sync_gateway",
"name": "#primary",
"namespace_id": "default",
"state": "online",
"using": "gsi"
}
},
{
"indexes": {
"condition": "(`type` = \"bcn_scan\")",
"datastore_id": "http://127.0.0.1:8091",
"id": "3847cba3cb75f8a9",
"index_key": [
"`did`",
"`t`"
],
"keyspace_id": "sync_gateway",
"name": "bcn_scan-did-t",
"namespace_id": "default",
"state": "online",
"using": "gsi"
}
},
{
"indexes": {
"datastore_id": "http://127.0.0.1:8091",
"id": "a8c8c25786449f1",
"index_key": [],
"is_primary": true,
"keyspace_id": "playground",
"name": "#primary",
"namespace_id": "default",
"state": "online",
"using": "gsi"
}
}
],
"status": "success",
"metrics": {
"elapsedTime": "203.155093ms",
"executionTime": "203.108497ms",
"resultCount": 3,
"resultSize": 1325
}
}
-
I tried to read https://dzone.com/articles/understanding-index-scans-in-couchbase-n1ql-query; however I do not understand what kind of index would be necessary to make those queries faster?
-
Between those 2 queries, which would be considered “faster” post-index (and why)?
(Code follow - parser needs an extra line to make preformatted text … preformatted! )
SELECT did, {date : {
"foull_entries" : ARRAY_SUM(ARRAY_AGG(TONUMBER(ARRAY_LENGTH(bcn)<>0))),
"nulld_entries" : ARRAY_SUM(ARRAY_AGG(TONUMBER(ARRAY_LENGTH(bcn)==0))),
"total_entries" : COUNT(*)
}
}
FROM sync_gateway
LET date = SUBSTR(t, 0, 10)
WHERE type="bcn_scan" AND date BETWEEN "2016-11-01" AND "2016-12-15"
GROUP BY did, date
ORDER BY did ASC, date ASC;
"metrics": {
"elapsedTime": "3m19.595350541s",
"executionTime": "3m19.595267868s",
"resultCount": 215,
"resultSize": 59099,
"sortCount": 215
}
and
SELECT date, {did : {
"foull_entries" : COUNT(NULLIF(ARRAY_LENGTH(bcn), 0)),
"total_entries" : COUNT(*),
"nulld_entries" : COUNT(*) - COUNT(NULLIF(ARRAY_LENGTH(bcn), 0))
}
}
FROM sync_gateway
LET date = SUBSTR(t, 0, 10)
WHERE type="bcn_scan" AND date BETWEEN "2016-11-01" AND "2016-12-15"
GROUP BY date, did
ORDER BY date ASC, did ASC;
"metrics": {
"elapsedTime": "3m5.42893805s",
"executionTime": "3m5.428881988s",
"resultCount": 215,
"resultSize": 59314,
"sortCount": 215
}
Obviously differences ~5s are negligible pre-index - maybe post-index array operations will be significant.
Maybe some syntax makes more sense to professionals.
Maybe there are serious issues with one of the two syntaxes.
Or […] idk. All comments / assistance welcome