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
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 theCREATE on the table.
Synopsis
Standard index
GIN index
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.Multiple-column indexes
Multiple-column indexes sort columns in the order you list them.Unique indexes
Unique indexes do not allow duplicate values among their columns.Create GIN indexes
You can create on schemaless data in a column.Create trigram indexes
You can create onSTRING columns by specifying the gin_trgm_ops or gist_trgm_ops opclass:
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 onGEOMETRY and GEOGRAPHY columns. Spatial indexes are a special type of .
To create a spatial index on a GEOMETRY column:
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 aVECTOR column named embedding:
Store columns
Storing a column improves the performance of queries that retrieve (but do not filter) its values.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.)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 .@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 theevents table already exists:

