CREATE INDEX Beta

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

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

index_columns

CREATE INDEX - 图2

partition_key_columns

CREATE INDEX - 图3

clustering_key_columns

CREATE INDEX - 图4

included_columns

CREATE INDEX - 图5

clustering_key_column_ordering

CREATE INDEX - 图6

Grammar

  1. create_index ::= CREATE [ UNIQUE ] INDEX [ IF NOT EXISTS ] index_name ON table_name '(' index_columns ')'
  2. [ included_columns ] [ clustering_key_column_ordering ];
  3. index_columns ::= partition_key_columns [ clustering_key_columns ]
  4. partition_key_columns ::= column_name | '(' column_name [ ',' column_name ...] ')'
  5. clustering_key_columns ::= column_name [ ',' column_name ...]
  6. included_columns ::= INCLUDE '(' column_name { ',' column_name } ')'
  7. clustering_key_column_ordering ::= WITH CLUSTERING ORDER BY '(' column_name [ ASC | DESC ] [ ',' column_name [ ASC | DESC ] ...] ')'

Where

  • index_name, table_name, and column_name are identifiers. table_name may be qualified with a keyspace name but index_name may not 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.
  • 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. This is done in order to respond to queries directly from the index without querying the table.
  • Currently, an index is used to execute a query only when all columns selected by the query are included by the index, either as index columns or additional included columns. This limitation will be removed in future versions.

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. warehouse_id INT,
  4. amount DOUBLE,
  5. PRIMARY KEY ((customer_id), order_date))
  6. 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 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, warehouse_id, amount)
  2. VALUES (1001, '2018-01-10', 107, 100.30);
  3. cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
  4. VALUES (1002, '2018-01-11', 102, 50.45);
  5. cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
  6. VALUES (1001, '2018-04-09', 102, 20.25);
  7. cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
  8. VALUES (1003, '2018-04-09', 108, 200.80);

Query by the partition column customer_id in the table

You can do this as shown below.

  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 warehouse_id column in the index orders_by_warehouse

You can do this as shown below.

  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

  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

You can do this as shown below.

  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

CREATE TABLEOther CQL Statements