Select distinct widgetId from default where type = ‘123’
I create a partial index like so:
CREATE INDEX myIndex ON app((distinct (widgetId))) WHERE (type = “123”)
The query does not hit the above index even with an explicit hint.
Also the index stats (items, and data size) indicate that all the millions of documents are represented in the index, where there should only be about 10k (distinct widgetIds).
What am I missing about how indexes work?
How can I write an index that will allow me to get the data efficiently?
Index can’t create DISTINCT values across the documents for scalar values. DISTINCT keyword for ARRAY indexing. i.e widgetID must be ARRAY and it will flatten ARRAY and index DISTINCT values per document as separate entry in index so that you can query with ANY clause.
Right index will be:
CREATE INDEX `myIndex` ON `app` (`widgetId`)
WHERE ( `type` = “123”);
Ok thank you, indeed, I got the idea from usage with Arrays. It would be a great feature to have that generally. Would enable efficient distinct queries with tiny indexes.
Indexer needs to store one entry per document. One you are looking more falls in pre-computed group/aggregation. If CE may be you can explore this use case as map/reduce views.