A newer version of this documentation is available.

View Latest
February 16, 2025
+ 12

How to create deferred indexes and build them later.
This guide is for Couchbase Server.

Introduction

When you create a primary or secondary index, you can mark it as deferred. This means the index is not built at once; you can build the deferred index later. This enables you to build multiple indexes more efficiently.

If you want to try out the examples in this section, follow the instructions given in Do a Quick Install to install Couchbase Server, configure a cluster, and load a sample dataset. Read the following for further information about the tools available for editing and executing queries:

Deferring an Index

You can defer an index to be built later using a SQL++ statement or an SDK call.

The SDK calls only enable you to create indexes in the default collection and default scope within a bucket. A N1QL statement enables you to create indexes in any collection and scope within a bucket.

To defer a primary or secondary index to be built later:

  1. Use the WITH clause to specify the index options.

  2. In the index options, set the defer_build attribute to true.


Context

For this example, set the query context to the inventory scope in the travel sample dataset. For more information, see Setting the Query Context.

Queries

The following queries create a set of primary and secondary indexes in the landmark keyspace, with build deferred until later.

CREATE INDEX idx_landmark_country
  ON landmark(country)
  USING GSI
  WITH {"defer_build":true};
CREATE INDEX idx_landmark_name 
  ON landmark(name)
  USING GSI
  WITH {"defer_build":true};
CREATE PRIMARY INDEX idx_landmark_primary
  ON landmark
  USING GSI
  WITH {"defer_build":true};

For further details and examples, refer to CREATE PRIMARY INDEX and CREATE INDEX.

Building a Deferred Index

You can build one or more deferred primary or secondary indexes using a SQL++ statement. You can also build all deferred indexes in a keyspace using an SDK call.

The SDK calls only enable you to build indexes in the default collection and default scope within a bucket. A N1QL statement enables you to build indexes in any collection and scope within a bucket.

To build one or more deferred indexes, use the BUILD INDEX statement:

  1. Use the ON keyword to specify the keyspace which contains the index or indexes.

  2. Specify the index or indexes that you want to build in parentheses ().


Context

For this example, set the query context to the inventory scope in the travel sample dataset. For more information, see Setting the Query Context.

Queries

The following query builds a single deferred index.

BUILD INDEX ON landmark(idx_landmark_country) USING GSI;

The following query builds multiple deferred indexes.

BUILD INDEX ON hotel(idx_landmark_name, idx_landmark_primary);

For further details and examples, refer to BUILD INDEX.

Reference and explanation:

Administrator guides:

Indexes with SDKs: