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
index_columns
partition_key_columns
clustering_key_columns
included_columns
clustering_key_column_ordering
Grammar
create_index ::= CREATE [ UNIQUE ] INDEX [ IF NOT EXISTS ] index_name ON table_name '(' index_columns ')'
[ included_columns ] [ clustering_key_column_ordering ];
index_columns ::= partition_key_columns [ clustering_key_columns ]
partition_key_columns ::= column_name | '(' column_name [ ',' column_name ...] ')'
clustering_key_columns ::= column_name [ ',' column_name ...]
included_columns ::= INCLUDE '(' column_name { ',' column_name } ')'
clustering_key_column_ordering ::= WITH CLUSTERING ORDER BY '(' column_name [ ASC | DESC ] [ ',' column_name [ ASC | DESC ] ...] ')'
Where
index_name
,table_name
, andcolumn_name
are identifiers.table_name
may be qualified with a keyspace name butindex_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 theIF 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 theCLUSTERING 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.
cqlsh:example> CREATE TABLE orders (customer_id INT,
order_date TIMESTAMP,
warehouse_id INT,
amount DOUBLE,
PRIMARY KEY ((customer_id), order_date))
WITH transactions = { 'enabled' : true };
Create an index for query by the order_date column
cqlsh:example> CREATE INDEX orders_by_date ON orders (order_date) INCLUDE (amount);
Create an index for query by the warehouse_id column
cqlsh:example> CREATE INDEX orders_by_warehouse ON orders (warehouse_id, order_date) INCLUDE (amount);
Insert some data
cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
VALUES (1001, '2018-01-10', 107, 100.30);
cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
VALUES (1002, '2018-01-11', 102, 50.45);
cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
VALUES (1001, '2018-04-09', 102, 20.25);
cqlsh:example> INSERT INTO orders (customer_id, order_date, warehouse_id, amount)
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.
cqlsh:example> SELECT SUM(amount) FROM orders WHERE customer_id = 1001 AND order_date >= '2018-01-01';
sum(amount)
sum(amount)
120.55
Query by the partition column order_date in the index orders_by_date
cqlsh:example> SELECT SUM(amount) FROM orders WHERE order_date = '2018-04-09';
sum(amount)
sum(amount)
221.05
Query by the partition column warehouse_id column in the index orders_by_warehouse
You can do this as shown below.
cqlsh:example> SELECT SUM(amount) FROM orders WHERE warehouse_id = 102 AND order_date >= '2018-01-01';
sum(amount)
sum(amount)
70.7
Create a table with a unique index
cqlsh:example> CREATE TABLE emp (enum INT primary key,
lastname VARCHAR,
firstname VARCHAR,
userid VARCHAR)
WITH transactions = { 'enabled' : true };
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.
cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
VALUES (1001, 'Smith', 'John', 'jsmith');
cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
VALUES (1002, 'Smith', 'Jason', 'jsmith');
InvalidRequest: Error from server: code=2200 [Invalid query] message="SQL error: Execution Error. Duplicate value disallowed by unique index emp_by_userid
INSERT INTO emp (enum, lastname, firstname, userid)
^^^^
VALUES (1002, 'Smith', 'Jason', 'jsmith');
(error -300)"
cqlsh:example> INSERT INTO emp (enum, lastname, firstname, userid)
VALUES (1002, 'Smith', 'Jason', 'jasmith');
cqlsh:example> SELECT * FROM emp;
enum | lastname | firstname | userid
------+----------+-----------+---------
1002 | Smith | Jason | jasmith
1001 | Smith | John | jsmith
See Also
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .