I have been testing indexes in N1QL with OR statements and have unexpected results:
I have objects in the database, all which contain a “tId” field, and I have created an index as:
create index tId_IX on default(tId);
When I run EXPLAIN on a statement like
select * from default where tId = 'xyz';
I get expected results with the tId_IX being used. When I make the query more complex, it continues to work, including using function modifiers (UPPER()), string compares (like ‘%…%’), etc. However, as soon as I put an “OR” statement in there, the query reverts to using the “Primary Index”.
For instance:
select * from default where tId = 'xyz' AND _type = 'board' OR _type = 'card';
It gets even worse if I encapsulate the OR statement such as:
select * from default where tId = 'xyz' AND (_type = 'board' OR _type = 'card');
From this, both the query and the explain completely bomb out and there are no results. However, I do get an error message logged that I’ve copied below.
My ultimate goal is to create index on tId, _type, and name fields, but that continues to elude me, possibly until this issue is worked out. For instance, my query would be like this, any advice on setting up the index is appreciated:
SELECT _id, name FROM default where tId = 'xyz' AND (_type = 'abc' OR _type = 'def') AND LOWER(name) LIKE '%abc%';
Thanks a lot for your help.
Chris
[UPDATE]
I get the same error when I use an IN statement, e.g.
select * from default where tId = 'xyz' AND _type IN ['board','card'];
-------- ERROR
goroutine 156 [running]:
github.com/couchbaselabs/query/server.func·003()
/var/root/goproj/src/github.com/couchbaselabs/query/server/server.go:128 +0xa5
runtime.panic(0x80c320, 0xe7e37c)
/usr/local/go/src/pkg/runtime/panic.c:248 +0x18d
github.com/couchbaselabs/query/planner.constrain(0xc2259e12a0, 0x0, 0x2, 0xc21d1e2f08, 0x1, 0x1, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/planner/sarg_and.go:48 +0x43f
github.com/couchbaselabs/query/planner.func·001(0x10970e0, 0xc21af31e60, 0xc2259e12a0, 0x0, 0x2, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/planner/sarg_and.go:37 +0x26c
github.com/couchbaselabs/query/planner.(*sargBase).VisitField(0xc21d1e2e68, 0xc21af31e60, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/planner/sarg.go:215 +0x93
github.com/couchbaselabs/query/expression.(*Field).Accept(0xc21af31e60, 0x109c348, 0xc21d1e2e68, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/expression/nav_field.go:48 +0x63
github.com/couchbaselabs/query/planner.SargFor(0x1096b40, 0xc20e5efd10, 0x10970e0, 0xc21af31e60, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/planner/sarg.go:38 +0xe8
github.com/couchbaselabs/query/plan.(*builder).selectScan(0xc209a6fcb0, 0x109b8f0, 0xc208912f00, 0xc20e5ee500, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build_scan.go:112 +0x775
github.com/couchbaselabs/query/plan.(*builder).VisitKeyspaceTerm(0xc209a6fcb0, 0xc20e5ee500, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build_select.go:232 +0x6a5
github.com/couchbaselabs/query/algebra.(*KeyspaceTerm).Accept(0xc20e5ee500, 0x1097208, 0xc209a6fcb0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/algebra/from.go:94 +0x60
github.com/couchbaselabs/query/plan.(*builder).VisitSubselect(0xc209a6fcb0, 0xc20e5eeff0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build_select.go:90 +0x1b46
github.com/couchbaselabs/query/algebra.(*Subselect).Accept(0xc20e5eeff0, 0x1097208, 0xc209a6fcb0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/algebra/select.go:232 +0x60
github.com/couchbaselabs/query/plan.(*builder).VisitSelect(0xc209a6fcb0, 0xc215ac82c0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build_select.go:34 +0x15d
github.com/couchbaselabs/query/algebra.(*Select).Accept(0xc215ac82c0, 0x1097180, 0xc209a6fcb0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/algebra/select.go:52 +0x60
github.com/couchbaselabs/query/plan.(*builder).VisitExplain(0xc209a6fcb0, 0xc2259e0ca0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build_explain.go:17 +0xd8
github.com/couchbaselabs/query/algebra.(*Explain).Accept(0xc2259e0ca0, 0x1097180, 0xc209a6fcb0, 0x0, 0x0, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/algebra/explain.go:38 +0x60
github.com/couchbaselabs/query/plan.Build(0x1096c68, 0xc2259e0ca0, 0x1095750, 0xc20802c070, 0x10961a8, 0xc20805a000, 0x8f67d0, 0x7, 0x1096800, 0x0, ...)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/build.go:23 +0x187
github.com/couchbaselabs/query/plan.BuildPrepared(0x1096c68, 0xc2259e0ca0, 0x1095750, 0xc20802c070, 0x10961a8, 0xc20805a000, 0x8f67d0, 0x7, 0x4ab4e00, 0x315b5, ...)
/var/root/goproj/src/github.com/couchbaselabs/query/plan/prepared.go:25 +0xb7
github.com/couchbaselabs/query/server.(*Server).getPrepared(0xc208046120, 0x1096658, 0xc219c46c60, 0x8f67d0, 0x7, 0x7, 0x0, 0x0)
/var/root/goproj/src/github.com/couchbaselabs/query/server/server.go:189 +0x167
github.com/couchbaselabs/query/server.(*Server).serviceRequest(0xc208046120, 0x1096658, 0xc219c46c60)
/var/root/goproj/src/github.com/couchbaselabs/query/server/server.go:144 +0x11b
github.com/couchbaselabs/query/server.(*Server).doServe(0xc208046120)
/var/root/goproj/src/github.com/couchbaselabs/query/server/server.go:119 +0x94
created by github.com/couchbaselabs/query/server.func·002
/var/root/goproj/src/github.com/couchbaselabs/query/server/server.go:112 +0x4a````