Skip to main content
The CREATE INDEX creates an index for a table. improve your database’s performance by helping SQL locate data without having to look through every row of a table. Indexes are automatically created for a table’s and columns. When querying a table, CockroachDB uses the fastest index. For more information about that process, see Index Selection in CockroachDB. The following types cannot be included in an index key, but can be stored (and used in a covered query) using the clause:
  • The computed type, even if it is constructed from indexed fields
To create an index on the schemaless data in a column or on the data in an , use a . The CREATE INDEX statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Required privileges

The user must have the CREATE on the table.

Synopsis

Standard index

create_index syntax diagram

GIN index

create_inverted_index syntax diagram

Parameters

Viewing schema changes

This schema change statement is registered as a job. You can view long-running jobs with .

Examples

Setup

To follow along, run to start a temporary, in-memory cluster with the sample dataset preloaded:

Create standard indexes

To create the most efficient indexes, we recommend reviewing:

Single-column indexes

Single-column indexes sort the values of a single column.
Because each query can only use one index, single-column indexes are not typically as useful as multiple-column indexes.

Multiple-column indexes

Multiple-column indexes sort columns in the order you list them.
To create the most useful multiple-column indexes, we recommend reviewing our .

Unique indexes

Unique indexes do not allow duplicate values among their columns.
This also applies the at the table level, similar to . The preceding example is equivalent to:
Primary key columns that are not specified within a unique index are automatically marked as in the table and in .

Create GIN indexes

You can create on schemaless data in a column.
The following syntax is equivalent:

Create trigram indexes

You can create on STRING columns by specifying the gin_trgm_ops or gist_trgm_ops opclass:
The following syntax is equivalent:
GIN and GiST indexes are implemented identically on CockroachDB. GIN and GIST are therefore synonymous when defining a trigram index.

Create spatial indexes

You can create on GEOMETRY and GEOGRAPHY columns. Spatial indexes are a special type of . To create a spatial index on a GEOMETRY column:
Unlike GIN indexes, spatial indexes do not support an alternate CREATE INVERTED INDEX... syntax. Only the syntax shown here is supported. For advanced users, there are a number of that can be passed in using the syntax WITH (var1=val1, var2=val2) as follows:
Most users should not change the default spatial index settings. There is a risk that you will get worse performance by changing the default settings. For more information, see .

Create vector indexes

You can create on columns. To create a vector index on a VECTOR column named embedding:

Store columns

Storing a column improves the performance of queries that retrieve (but do not filter) its values.
However, to use stored columns, queries must filter another column in the same index. For example, SQL can retrieve name values from the above index only when a query’s WHERE clause filters city. An index that stores all the columns needed by a query is also known as a covering index for that query. When a query has a covering index, CockroachDB can use that index directly instead of doing an “index join” with the primary index, which is likely to be slower.

Change column sort order

To sort columns in descending order, you must explicitly set the option when creating the index. (Ascending order is the default.)
How a column is ordered in the index will affect the ordering of the index keys, and may affect the efficiency of queries that include an ORDER BY clause.

Query specific indexes

Normally, CockroachDB selects the index that it calculates will scan the fewest rows. However, you can override that selection and specify the name of the index you want to use. To find the name, use .
You can use the @primary alias to use the table’s primary key in your query if no secondary index explicitly named primary exists on that table.

Create a hash-sharded secondary index

We . If a table must be indexed on sequential keys, use . Hash-sharded indexes distribute sequential traffic uniformly across ranges, eliminating single-range and improving write performance on sequentially-keyed indexes at a small cost to read performance. Let’s assume the events table already exists:
You can create a hash-sharded index on an existing table:

See also