Index created by the Sync Gateway (sg_channels_1) has more items than there are documents in the bucket

Hi there,

I have a question about the sg_channels_1 index that gets created when the Sync Gateway starts up. I have 61,469 documents in our data bucket, but the sg_channels_1 index that gets created shows it has indexed 105,530 items. I’m not super strong with the N1QL being used for the index so I’m not sure if this is normal. I would appreciate it if you could help me understand a bit more about why there are so many more items indexed compared to documents in the bucket.

Here is the index:

CREATE INDEX `sg_channels_1` ON `data`((all (array [(`op`.`name`), least(((self.`_sync`).`sequence`), ((`op`.`val`).`seq`)), ifmissing(((`op`.`val`).`rev`), null), ifmissing(((`op`.`val`).`del`), null)] for `op` in object_pairs(((self.`_sync`).`channels`)) end)),((self.`_sync`).`rev`),((self.`_sync`).`sequence`),((self.`_sync`).`flags`)) WITH { "defer_build":true }

This happens in our production environment which has many more documents/items, but the numbers I provided above is from my own development environment where I am seeing similar results. I’m not positive about our production environment cluster (I can get that info if it’s important). My development environment is a single server node with single Sync Gateway instance.
Couchbase Server: 6.5.0-4960
Sync Gateway: 2.5.0-271

Thank you for any assistance you can give me!

The Index is Array Index (first key is ALL ARRAY …
Array used in object_pairs(((self._sync).channels)) .
Each value of array will have separate entry in the index. This is normal.
Example: If you have 5 documents ARRAY has 5,4,4,1, 0 elements , index can have 5+4+4+1+0 = 14 entries.

https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/indexing-arrays.html

Arrays in JSON: Modeling, Querying and Indexing Performance | The Couchbase Blog Array Indexing:


2 documents in bucket with Array index will have 5 entries.

INSERT INTO default VALUES("blob1",{ "name": "bob-1", "A": [{"n":1}, {"n":2}, {"n":3}] });
INSERT INTO default VALUES("blob2",{ "name": "bob-2", "A": [{"n":11}, {"n":12}] });
CREATE INDEX ix1 ON default (ALL ARRAY v.n FOR v IN A END, name);

cbindex  -auth  user:pass -type scanAll -bucket default  -index ix1

ScanAll index:
[1 "bob-1"] ... blob1
[2 "bob-1"] ... blob1
[3 "bob-1"] ... blob1
[11 "bob-2"] ... blob2
[12 "bob-2"] ... blob2
Total number of entries:  5