//create view index
CREATE INDEX idx_tp_app_mt ON default(tp,app,mt) WHERE tbl == 'dv';
//query on view index
EXPLAIN SELECT * FROM default USE INDEX(idx_tp_app_mt USING VIEW)WHERE tp = 'i'
When I try query with only one view index. the server returns
[
{
"code": 4000,
"msg": "No primary index on keyspace default. Use CREATE PRIMARY INDEX to create one."
}
]
What I need is doing N1QL query without primary index (it use too much resource).
First, yes, I should change the statement, adding “USING VIEW”
Second, I believe SELECT * is right.
Turns out the root cause is " WHERE tbl == 'dv' "
When I change the “create index” statement as below: CREATE INDEX idx_tp_app_mt ON default(tp,app,mt) USING VIEW;
Then everything works well (including "SELECT *").
Sounds good. If you select specific fields instead of *, you can also get covering indexes with Couchbase 4.1.
You should also consider GSI indexes. Anyway, all these approaches work. It’s just tradeoffs, which you should try out using EXPLAIN and the metrics of your queries.