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

CREATE INDEX - 图1

partition_key_columns

CREATE INDEX - 图2

clustering_key_columns

CREATE INDEX - 图3

clustering_key_column_ordering

CREATE INDEX - 图4

index_column

CREATE INDEX - 图5

jsonb_attribute

CREATE INDEX - 图6

covering_columns

CREATE INDEX - 图7

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 ]
  4. partition_key_columns ::= index_column | ( index_column [ , ... ] )
  5. clustering_key_columns ::= index_column [ , ... ]
  6. clustering_key_column_ordering ::= WITH CLUSTERING ORDER BY ( { index_column [ ASC | DESC ] } [ , ... ] )
  7. index_column ::= column_name | jsonb_attribute
  8. jsonb_attribute ::= column_name [ -> 'attribute_name' [ ... ] ] ->> 'attribute_name'
  9. covering_columns ::= { COVERING | INCLUDE } ( column_name [ , ... ] )

Where

  • index_name, table_name, and column_name are identifiers. table_name may be qualified with a keyspace name. index_name cannot 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_name already exists in the associated keyspace unless the IF NOT EXISTS option 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 the CLUSTERING ORDER BY property.
  • Any primary key column of the table not indexed explicitly in index_columns is 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

See also