WHERE filter) defined at index creation.
How do partial indexes work?
When you create a partial index, CockroachDB “indexes” the columns and rows that evaluate to true on the index’s boolean predicate expression, creating a sorted copy of the subset of row values, without modifying the values in the table itself. CockroachDB can use a partial index to efficiently execute queries on any subset of rows implied by the partial index. When possible, the creates a plan that limits table scans on rows implied by the partial index to just the rows in the index. It also limits index rewrites to fewer rows. Partial indexes can improve cluster performance in a number of ways:- They contain fewer rows than full indexes, making them less expensive to create and store on a cluster.
- Read queries on rows included in a partial index only scan the rows in the partial index. This contrasts with queries on columns in full indexes, which must scan all rows in the indexed column.
- Write queries on tables with a partial index only perform an index write when the rows inserted satisfy the partial index predicate. This contrasts with write queries on tables with full indexes, which incur the overhead of a full index write when the rows inserted modify an indexed column.
When a query on a table with a partial index has a filter expression, the attempts to prove that the filter implies the partial index predicate. It is not guaranteed that the optimizer can prove the implication of arbitrarily complex expressions. Although unlikely, it is possible that a filter implies a predicate, but the optimizer cannot prove the implication.
Creation
To create a partial index, use a statement, with a standardWHERE clause defining a predicate expression.
For example, to define a partial index on columns a and b of table t, filtering on rows in column c greater than 5:
- The predicate expression must result in a .
- The predicate expression can only refer to columns in the table being indexed.
- used in predicates must be immutable. For example, the
now()function is not allowed in predicates because its value depends on more than its arguments.
Unique partial indexes
You can enforce on a subset of rows withCREATE UNIQUE INDEX ... WHERE ....
For example, to define a unique partial index on columns a and b for table t, filtering on rows in column d equal to 'x':
UNIQUE constraint on the subset of rows where d is equal to 'x'.
For another example, see Create a partial index that enforces uniqueness on a subset of rows.
Partial GIN indexes
You can create partial , which are indexes on a subset ofJSON, ARRAY, or geospatial container column data.
Index hints
You can force queries (also known as “index hinting”), like you can with full indexes. However, unlike full indexes, partial indexes cannot be used to satisfy all queries. If a query’s filter implies the partial index predicate expression, the partial index will be used in the query plan. If not, an error will be returned.Known limitations
- CockroachDB does not currently support statements on tables with partial indexes.
- CockroachDB does not currently support multiple arbiter indexes for
INSERT ON CONFLICT DO UPDATE, and will return an error if there are multiple unique or exclusion constraints matching theON CONFLICT DO UPDATEspecification. - CockroachDB prevents a column from being dropped using if it is referenced by a partial index predicate. To drop such a column, the partial indexes need to be dropped first using .
Examples
Setup
The following examples use the . Start the on a 3-node CockroachDB demo cluster with a larger data set.Create an index on a subset of rows
Suppose that you want to query the subset ofrides with a revenue greater than 90.
rides table with a WHERE revenue > 90 clause will scan the entire table. To see the plan for such a query, you can use an :
estimated row count in the scan node lists the number of rows that the query plan will scan (in this case, the entire table row count of 125,000). The table property lists the index used in the scan (in this case, the ).
To limit the number of rows scanned to just the rows that you are querying, you can create a partial index:
EXPLAIN statement shows that the number of rows scanned by the original query decreases significantly with a partial index on the rides table:
SELECT statement queries all columns in the rides table, not just the indexed columns. As a result, an “index join” is required on both the primary index and the partial index.
Querying only the columns in the index will make the query more efficient by removing the index join from the query plan:
revenue > 90) will also use the partial index:
FULL SCAN of the index). This is because the WHERE clause does not filter on the first column in the index prefix (city). Filtering the query on both columns in the partial index will limit the scan to just the rows that match the filter:
revenue filter expression to match just a subset of the partial index will lower the scanned row count even more:
Create an index that excludes values
Suppose that you have a number of rows in a table with values that you regularly filter out of selection queries (e.g.,NULL values).
A selection query on these values will require a full table scan, using the primary index, as shown by the below:
NULL values more efficient.
Create a partial index that enforces uniqueness on a subset of rows
Suppose that you want to constrain a subset of the rows in a table, such that all values for a particular column in the subset are unique. For example, let’s say that every user in New York City must have a unique name. You can do this efficiently with a unique partial index:city='new york'.
city='new york', the UNIQUE constraint does not apply to all rows in the table.

