(I didn’t realize I posed this question earlier this year. )
What if I want to use an index like:
attrib between valA and valC
(valA, B, C are positive integers in this case)
That might look like:
create index idx_test on bucket (attrib between valA and valC);
I can no longer use the IN [ … ] pattern?
Without that restriction on the the values for the index, the IN [ …] works perfectly fine.
EDIT1: Using the index with the restriction in values, I can still do the long way:
(attrib = valA) or
(attrib = valB) or
(attrib = valC)
Just not the short way:
attrib in [valA, valB, valC]
EDIT2: Index created with restriction either:
create index idx_test on bucket (attrib between valA and valC);
or
create index idx_test on bucket (attrib) where attrib between valA and valC;
Both work with the long way ( (attrib = valA) or (attrib = valB) or (attrib = valC) ) but not the short way with the IN operator.
create index idx_test on bucket (attrib);
This index works for all these queries.
SELECT * FROM bucket WHERE attrib IN [10,20,30];
SELECT * FROM bucket WHERE attrib between 10 AND 30;
SELECT * FROM bucket WHERE attrib between "abc" AND "xyz";
attrib, values can be number,string,bool, arrays, objects, or combination.
In my testing, those indeed do work. However, I wanted to create a function base index only a partial such that the coverage of the index would only be for the selected values. For example, let say that the values range from 1 to 5 (e.g., {1, 2, 3, 4, 5}), I would create the index like:
create index idx_test on bucket (attrib) where attrib between 1 and 5;
or create index idx_test on bucket (attrib between 1 and 5);
Then when I try to the N1QL query:
select * from bucket where attrib in [2, 4];
I would get the standard error stating that there is no index/create a primary index.
If I don’t put in that “between 1 and 5” as in your example for the index creation, your solution works.
Any ideas?
create index idx_test on bucket (attrib) where attrib between 1 and 5;
In above index contains only the documents attrib between 1 and 5 ad none of other documents.
The query need to use that index query predicate needs to have exact index where clause or subset of it. Other wise we can’t use that index because index will not have all the data, if used it result in wrong results.
You can try select * from bucket where attrib between 2 and 4;
create index idx_test on bucket (attrib between 1 and 5);
Index key is expression attrib between 1 and 5, so index has only result of expression i.e true or false not attrib. Query predicate needs to match exactly index key.
create index idx_test on bucket (attrib) where attrib between 1 and 5;
I have tested in 4.6.3 the following queries uses index
select * from bucket where attrib = 2 ;
select * from bucket where ( attrib = 2 or attrib = 4);
select * from bucket where (attrib between 1 and 5) AND ( attrib = 2 or attrib = 4);
select * from bucket where (attrib between 1 and 5) AND ( attrib = 2);
select * from bucket where (attrib between 1 and 5) AND ( attrib IN [2,4]);
Only IN clause will not use the index, I have provided the workaround above