Covering Indexes

When an index includes the actual values of all the fields specified in the query, the index covers the query and does not require an additional step to fetch the actual values from the data service.
An index, in this case, is called a covering index and the query is called a covered query.
As a result, covered queries are faster and deliver better performance.

The following diagram illustrates the query execution work flow without covering indexes:

The following diagram illustrates the query execution work flow with covering indexes:

As you can see in the second diagram, a well designed query that uses a covering index avoids the additional steps to fetch the data from the data service.
This results in a considerable performance improvement.

The examples on this page use the travel-sample and default keyspaces and need to be enabled before using them.
See Installing Sample Buckets for details.

You can see the query execution plan using the EXPLAIN statement.
When a query uses a covering index, the EXPLAIN statement shows that a covering index is used for data access, thus avoiding the overhead associated with key-value document fetches.
Consider a simple index, idx_state, on the attribute state in the travel-sample keyspace:

CREATE INDEX idx_state on `travel-sample` (state,type) USING GSI;

If we select state from the `travel-sample` keyspace, the actual values of the field state that are to be returned are present in the index idx_state, and avoids an additional step to fetch the data.
In this case, the index idx_state is called a covering index and the query is a covered query.

If you modify the query to select the state and city from the `travel-sample` bucket using the same index idx_state, the index does not contain the values of the city field to satisfy the query, and hence a key-value fetch is performed to retrieve this data.

MISSING items are not indexed by indexers.
To take advantage of covering indexes and for the index to qualify, a query needs to exclude documents where the index key expression evaluates to MISSING.
For example, index index1 defined below covers the following query.

CREATE INDEX index1 ON bucket(attribute1) WHERE attribute2 = "value";

SELECT attribute1 FROM bucket WHERE attribute2 = "value" AND attribute1 IS NOT MISSING;

Covering indexes are applicable to secondary index scans and can be used with view and global secondary indexes (GSI).
Queries with expressions and aggregates benefit from covering indexes.

You cannot use multiple GSI indexes to cover a query.
You must create a composite index with all the required fields for the query engine to cover by GSI and not require reading the documents from the data nodes.

The following queries can benefit from covering indexes.
Try these statements using cbq to see the query execution plan.

Expressions and Aggregates

EXPLAIN SELECT MAX(country) FROM `travel-sample` WHERE city = "Paris";

EXPLAIN SELECT country || city FROM `travel-sample` USE INDEX (idx_country_city) WHERE city = "Paris";

UNION/INTERSECT/EXCEPT

SELECT country FROM `travel-sample` WHERE city = "Paris"
UNION ALL
SELECT country FROM `travel-sample` WHERE city = "San Francisco";

Sub-queries

SELECT *
FROM (
SELECT country FROM `travel-sample` WHERE city = "Paris"
UNION ALL
SELECT country FROM `travel-sample` WHERE city = "San Francisco"
) AS newtab;

SELECT in INSERT statements

INSERT INTO `travel-sample`(KEY k, VALUE city)
SELECT country, city FROM `travel-sample` WHERE city = "Paris";

Arrays in WHERE clauses

First, create a new index, idx_array.

CREATE INDEX idx_array ON `travel-sample`(a, b);

Then, run the following query:

SELECT b FROM `travel-sample` WHERE a = [1, 2, 3, 4];

Collection Operators: FIRST, ARRAY, ANY, EVERY, and ANY AND EVERY

Since the default bucket is empty by default, let’s first insert the following documents into the default bucket: