A newer version of this documentation is available.

View Latest

Indexes and Indexing

      +

      Description — Couchbase mobile database indexing concepts

      Overview

      Creating indexes can speed up the performance of queries. While indexes make queries faster, they also make writes slightly slower, and the Couchbase Lite database file slightly larger. As such, it is best to only create indexes when you need to optimize a specific case for better query performance.

      The following example creates a new index for the type and name properties.

      {
          "_id": "hotel123",
          "type": "hotel",
          "name": "Apple Droid"
      }
      CBLValueIndexItem *type = [CBLValueIndexItem property:@"type"];
      CBLValueIndexItem *name = [CBLValueIndexItem property:@"name"];
      CBLIndex* index = [CBLIndexBuilder valueIndexWithItems:@[type, name]];
      [database createIndex:index withName:@"TypeNameIndex" error:&error];

      If there are multiple expressions, the first one will be the primary key, the second the secondary key, etc.

      Every index has to be updated whenever a document is updated, so too many indexes can hurt performance. Thus, good performance depends on designing and creating the right indexes to go along with your queries.