CREATE INDEX, DROP INDEX
Add a secondary index to speed up lookups on non-key columns, or a unique index that refuses duplicate values.
A secondary index speeds up lookups by columns other than the primary key. An ordinary index only makes reads faster and never makes a write fail. A unique index also refuses a write that would give two live rows the same key value.
Syntax
CREATE [UNIQUE] INDEX <index_name> ON <table> (<column> [, ...]) [COVERING (<column> [, ...])]
DROP INDEX <index_name>
Arguments
| Argument | Description |
|---|---|
UNIQUE | Optional. Refuse a write that would give two live rows the same values in the key columns. |
<index_name> | Name of the index, unique across the instance. |
<table> | The table to index. |
(<column>, ...) | Key columns, up to 4. |
COVERING (<column>, ...) | Extra columns stored in the index, up to 8, so a lookup can read them without visiting the table. |
Examples
CREATE INDEX ix_passenger_name ON passengers(name) COVERING (member);
CREATED INDEX name=ix_passenger_name on=passengers index_oid=2 root_page=157 key_width=33 entry_width=42 entries=0
SHOW INDEXES;
index_oid,name,rel_oid,rel_name,keys,covering,root_page,key_width,entry_width,height,entries,unique
1,ix_tickets_passenger,4039,tickets,passenger_id,flight_id,baggage_kg,154,9,33,1,9,0
2,ix_passenger_name,4025,passengers,name,member,157,33,42,1,8,0
DROP INDEX ix_passenger_name;
The sample tickets table already has the index ix_tickets_passenger, on passenger_id.
A unique index
kdbs> CREATE UNIQUE INDEX ux_items_sku ON items(sku)
CREATED INDEX name=ux_items_sku on=items index_oid=3 root_page=158 key_width=9 entry_width=17 entries=0
kdbs> INSERT INTO items VALUES ('KB-0001', 'Keyboard clone', 'peripherals', 10.00, 1)
ERR unique index 'ux_items_sku' on (sku): (KB-0001) is already held by tuple key 1 (byte 26)
kdbs> UPDATE items SET sku = 'MS-0002' WHERE id = 1
ERR unique index 'ux_items_sku' on (sku): (MS-0002) is already held by tuple key 2 (byte 23)
SHOW INDEXES marks it unique=1. A unique index over data that already has duplicates is refused, naming two of the rows:
kdbs> CREATE UNIQUE INDEX ux_items_cat ON items(category)
ERR unique index 'ux_items_cat' on (category): (peripherals) is held by both tuple key 1 and tuple key 2 (byte 20)
How it works
- Each entry holds the key columns, the covered columns and the row’s primary key.
- A
WHERE column = valueon a non-key column uses an index first, then a Cabin, then a full scan, in that fixed order. The choice depends on the statement and the catalog, never on data statistics. - Index maintenance only appends.
DELETEdoes not touch the index; every read checks entries against the row, so a stale entry is skipped. CREATE INDEXandDROP INDEXwait for open writes and running reads of the table, and hold new writes until they finish. A wait longer thanlock_wait_fault_net_ms(default 1 second) is refused with a retryableTXN_CONFLICT.- Both are transactional: inside a transaction,
ROLLBACKundoes them.
Unique indexes
- Live rows hold a value. A deleted, rolled-back or purged row holds nothing, so its value can be written again at once. The writer’s own uncommitted rows count.
- Concurrent writers. If another transaction’s undecided write may hold the value, the write waits for that transaction to commit or roll back and checks again. Row 2 onward of a multi-row
INSERTdoes not wait: it is refused with the retryableTXN_CONFLICT. - Checked per row, at once.
UPDATE t SET k = k + 1over consecutive values is refused at the first row that collides, where standard SQL would admit it. - Only key columns are checked. An
UPDATEthat changes only covered columns is not checked. - A refused write writes nothing. The refusal names the index, its columns and values, and the primary key of the row that holds the value.
CREATE UNIQUE INDEXchecks every existing row while it builds and is refused if two rows share a value.
Limitations
UNIQUEis spelled only asCREATE UNIQUE INDEX. There is noUNIQUEcolumn or table constraint inCREATE TABLE, and noADD CONSTRAINT.- An index cannot be on the primary key: the table’s own tree already is that index.
- There are no index-only scans;
COVERINGsaves the visit to the table for the covered columns, nothing more. - Key and covered columns cannot be nullable, so a unique index never holds
NULL. - More than 4 key columns or 8 covered columns is refused, never truncated.