I’m trying to find ways to improve performance of n1ql queries. The couchbase documentation on http://developer.couchbase.com/documentation/server/current/indexes/gsi-for-n1ql.html describes “Partitioning with GSI” by dividing the index among multiple nodes.
I’ve split the index for the time field by month like this on every node (for redundancy):
Definition: CREATE INDEX bwidx_bwecl_sp_startTime_10_17_4_155_2016_1
ON bwecl
(serviceProviderUid
,startTime
) WHERE (startTime
between 1454306400000 and 1456811999999) USING GSI
Definition: CREATE INDEX bwidx_bwecl_sp_startTime_10_17_4_155_2016_2
ON bwecl
(serviceProviderUid
,startTime
) WHERE (startTime
between 1456812000000 and 1459486799999) USING GSI
Definition: CREATE INDEX bwidx_bwecl_sp_startTime_10_17_4_155_2016_3
ON bwecl
(serviceProviderUid
,startTime
) WHERE (startTime
between 1459486800000 and 1462078799999) USING GSI
… and so on
The query encapsulating jan-march
EXPLAIN SELECT * FROM bwecl
AS ecl
WHERE serviceProviderUid
= ‘SP2200000000’ AND startTime BETWEEN 1454306400000 AND 1462078799999 ORDER BY serviceProviderUid, startTime DESC;
returns
"errors": [
{
“code”: 4000,
“msg”: “No primary index on keyspace bwecl. Use CREATE PRIMARY INDEX to create one.”
}
]
the query encapsualting all of january
EXPLAIN SELECT * FROM bwecl
AS ecl
WHERE serviceProviderUid
= ‘SP2200000000’ AND startTime BETWEEN 1454306400000 AND 1456811999999 ORDER BY serviceProviderUid, startTime DESC;
returns
{ "requestID": "409ec16e-10fa-4ba5-922d-158547fc61dd", "signature": "json", "results": [ { "#operator": "Sequence", "~children": [ { "#operator": "Sequence", "~children": [ { "#operator": "IndexScan", "index": "bwidx_bwecl_sp_startTime_10_17_4_155_2016_1", "keyspace": "bwecl", "namespace": "default", "spans": [ { "Range": { "High": [ "\"SP2200000000\"", "1456811999999" ], "Inclusion": 3, "Low": [ "\"SP2200000000\"", "1454306400000" ] } } ], "using": "gsi" }, { "#operator": "Parallel", "~child": { "#operator": "Sequence", "~children": [ { "#operator": "Fetch", "as": "ecl", "keyspace": "bwecl", "namespace": "default" }, { "#operator": "Filter", "condition": "(((
ecl.
serviceProviderUid) = \"SP2200000000\") and ((
ecl.
startTime) between 1454306400000 and 1456811999999))" }, { "#operator": "InitialProject", "result_terms": [ { "expr": "self", "star": true } ] } ] } } ] }, { "#operator": "Order", "sort_terms": [ { "expr": "(
ecl.
serviceProviderUid)" }, { "desc": true, "expr": "(
ecl.
startTime)" } ] }, { "#operator": "FinalProject" } ] } ], "status": "success", "metrics": { "elapsedTime": "89.747684ms", "executionTime": "89.568326ms", "resultCount": 1, "resultSize": 3173 } }
this is the exact same query, but the time span for the first one should hit multiple indexes and the time span for the first hits just one.