CREATE INDEX
Synopsis
The CREATE INDEX statement is used to create a new index on a table. It defines the index name, index columns, and additional columns to include.
Syntax
Diagram
create_index

partition_key_columns

clustering_key_columns

clustering_key_column_ordering

index_column

jsonb_attribute

covering_columns

Grammar
1. create_index ::= CREATE INDEX [ IF NOT EXISTS ] index_name
2. ON table_name ( partition_key_columns [ clustering_key_columns ] )
3. [ clustering_key_column_ordering ] [ covering_columns ]
5. partition_key_columns ::= index_column | ( index_column [ , ... ] )
7. clustering_key_columns ::= index_column [ , ... ]
9. clustering_key_column_ordering ::= WITH CLUSTERING ORDER BY ( { index_column [ ASC | DESC ] } [ , ... ] )
11. index_column ::= column_name | jsonb_attribute
13. jsonb_attribute ::= column_name [ -> 'attribute_name' [ ... ] ] ->> 'attribute_name'
15. covering_columns ::= { COVERING | INCLUDE } ( column_name [ , ... ] )
Where
index_name,table_name, andcolumn_nameare identifiers.table_namemay be qualified with a keyspace name.index_namecannot be qualified with a keyspace name because an index must be created in the table’s keyspace.
Semantics
- An error is raised if transactions have not be enabled using the
WITH transactions = { 'enabled' : true }clause on the table to be indexed. This is because secondary indexes internally use distributed transactions to ensure ACID guarantees in the updates to the secondary index and the associated primary key. More details here. - An error is raised if
index_namealready exists in the associated keyspace unless theIF NOT EXISTSoption is used. - Indexes do not support TTL. An error is raised if data is inserted with TTL into a table with indexes.
- Currently, when an index is created on a table, the existing data in the table is not indexed. Therefore, the index should be created before any data is inserted into the table.
PARTITION KEY
- Partition key is required and defines a split of the index into partitions.
CLUSTERING KEY
- Clustering key is optional and defines an ordering for index rows within a partition.
- Default ordering is ascending (
ASC) but can be set for each clustering column as ascending or descending using theCLUSTERING ORDER BYproperty. - Any primary key column of the table not indexed explicitly in
index_columnsis added as a clustering column to the index implicitly. This is necessary so that the whole primary key of the table is indexed.
INCLUDED COLUMNS
- Included columns are optional table columns whose values are copied into the index in addition to storing them in the table. When additional columns are included in the index, they can be used to respond to queries directly from the index without querying the table.
UNIQUE INDEX
- A unique index disallows duplicate values from being inserted into the indexed columns. It can be used to ensure uniqueness of index column values.
Examples
Create a table to be indexed
‘customer_id’ is the partitioning column and ‘order_date’ is the clustering column.
1. cqlsh:example> CREATE TABLE orders (customer_id INT,
2. order_date TIMESTAMP,
3. product JSONB,
4. warehouse_id INT,
5. amount DOUBLE,
6. PRIMARY KEY ((customer_id), order_date))
7. WITH transactions = { 'enabled' : true };
Create an index for query by the order_date column
1. cqlsh:example> CREATE INDEX orders_by_date ON orders (order_date) INCLUDE (amount);
Create an index for query by the JSONB attribute product->>'name'
1. cqlsh:example> CREATE INDEX product_name ON orders (product->>'name') INCLUDE (amount);
Create an index for query by the warehouse_id column
1. cqlsh:example> CREATE INDEX orders_by_warehouse ON orders (warehouse_id, order_date) INCLUDE (amount);
Insert some data
1. cqlsh:example> INSERT INTO orders (customer_id, order_date, product, warehouse_id, amount)
2. VALUES (1001, '2018-01-10', '{ "name":"desk" }', 107, 100.30);
3. cqlsh:example> INSERT INTO orders (customer_id, order_date, product, warehouse_id, amount)
4. VALUES (1002, '2018-01-11', '{ "name":"chair" }', 102, 50.45);
5. cqlsh:example> INSERT INTO orders (customer_id, order_date, product, warehouse_id, amount)
6. VALUES (1001, '2018-04-09', '{ "name":"pen" }', 102, 20.25);
7. cqlsh:example> INSERT INTO orders (customer_id, order_date, product, warehouse_id, amount)
8. VALUES (1003, '2018-04-09', '{ "name":"pencil" }', 108, 200.80);
Query by the partition column customer_id in the table
1. cqlsh:example> SELECT SUM(amount) FROM orders WHERE customer_id = 1001 AND order_date >= '2018-01-01';
1. sum(amount)
1. 120.55
Query by the partition column order_date in the index orders_by_date
1. cqlsh:example> SELECT SUM(amount) FROM orders WHERE order_date = '2018-04-09';
1. sum(amount)
1. 221.05
Query by the partition column product->>'name' in the index product_name
1. cqlsh:example> SELECT SUM(amount) FROM orders WHERE product->>'name' = 'desk';
1. sum(amount)
1. 100.30
Query by the partition column warehouse_id column in the index orders_by_warehouse
1. cqlsh:example> SELECT SUM(amount) FROM orders WHERE warehouse_id = 102 AND order_date >= '2018-01-01';
1. sum(amount)
1. 70.7
Create a table with a unique index
You can do this as shown below.
1. cqlsh:example> CREATE TABLE emp (enum INT primary key,
2. lastname VARCHAR,
3. firstname VARCHAR,
4. userid VARCHAR)
5. WITH transactions = { 'enabled' : true };
6. cqlsh:example> CREATE UNIQUE INDEX emp_by_userid ON emp (userid);
Insert values into the table and verify no duplicate userid is inserted
1. cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
2. VALUES (1001, 'Smith', 'John', 'jsmith');
3. cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
4. VALUES (1002, 'Smith', 'Jason', 'jsmith');
5. InvalidRequest: Error from server: code=2200 [Invalid query] message="SQL error: Execution Error. Duplicate value disallowed by unique index emp_by_userid
6. INSERT INTO emp (enum, lastname, firstname, userid)
7. ^^^^
8. VALUES (1002, 'Smith', 'Jason', 'jsmith');
9. (error -300)"
10. cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
11. VALUES (1002, 'Smith', 'Jason', 'jasmith');
1. cqlsh:example> SELECT * FROM emp;
1. enum | lastname | firstname | userid
2. ------+----------+-----------+---------
3. 1002 | Smith | Jason | jasmith
4. 1001 | Smith | John | jsmith
