Views and Indexes

Hi everyone,

I have just started to work on indexing parts of couchbase and I’m little bit confused especially distinction between indexing & views. For example, I created a view by map-reduce function so that I had an index with some filtered data. On the other hand, I decided to use CREATE INDEX command with some filter expressions and by using VIEW indexer.

My question is that are they equal?

Have a look at Global Secondary Indexes Versus Views for a comparison of the query support options.

Also see @cihangirb’s forum post:

N1QL can only use views when an index is created using the View indexer.
CREATE INDEX … USING VIEW; is the only way to use view index with N1QL.

Stepping back, N1QL can use 2 of the indexers in the system: GSI or Views. The reason for not having any view available to N1QL is we need a specific shape in an index to optimize queries. That is why we tie down the view side but this isn’t a perpetual situation. we do plan to make views more accessible in future from N1QL. Right now, my recommendation is to use N1QL and create the best indexes for your query using N1QL and use Views for cases where you need precalculated aggregations for example interactive reports etc.

And in an older post, @geraldss wrote:

Beginning with Couchbase 4.0, N1QL will be the most general way to query Couchbase (i.e. non-KV access). Views will continue to be recommended for the following cases:

  1. Using Javascript logic as part of the query
  2. Performing aggregation (reduce), materializing the results, and then maintaing the results incrementally as the source data is modified.

(2) is important for performance, because the aggregate results are essentially pre-computed.

@zero Would be great if you can share your usecase - what is the business logic that you are trying to get done? this will help in suggesting if Views or N1QL+GSI will help your cause. Generally, I would lean towards the latter(N1QL+GSI) as it is an easier data access pattern(declarative), and will try to solve most of your usecases.