CREATE, ALTER and DROP INDEX
An index lets CoraDB find rows by the values of one or more columns without reading the whole table. CoraDB indexes are B+trees. Primary keys, unique constraints and foreign keys create their indexes automatically; this page covers the indexes you create yourself. How the optimizer uses indexes, and how to steer it, is explained in Query optimization.
CREATE INDEX
Section titled “CREATE INDEX”CREATE [UNIQUE] INDEX index_name ON table_name <index_definition> [WITH ONLINE [PARALLEL thread_count] | INVISIBLE | VISIBLE] [COMMENT 'comment']
<index_definition> ::= (column_name [ASC | DESC] [, column_name [ASC | DESC] ...]) [WHERE <filter_predicate>] | (function_name (argument_list))- UNIQUE: no two rows can have the same key.
NULLvalues do not count as duplicates. - index_name: required, and unique within the table; see Identifiers.
- column_name: one or more columns. In a composite index, the order of the columns matters: the index can be searched by its first column, its first two columns, and so on.
- ASC | DESC: the order of the column in the index (default
ASC). - WHERE filter_predicate: a filtered index contains only the rows that satisfy the predicate. Several conditions must be combined with
AND. - function_name(argument_list): a function-based index stores the result of an expression, so queries that filter on the same expression can use it.
- WITH ONLINE: build the index while other transactions keep changing the table (see Online index creation).
- INVISIBLE: create the index without letting the optimizer use it yet.
VISIBLEis the default. - COMMENT: a description of the index.
A descending index on product prices, and a composite index on a customer's orders, newest first:
CREATE INDEX idx_product_price ON product (unit_price DESC);
CREATE INDEX idx_orders_customer_date ON orders (customer_id, order_date DESC) COMMENT 'orders of a customer, newest first';A filtered index that contains only unshipped orders, and a function-based index for case-insensitive searches on company names:
CREATE INDEX idx_orders_unshipped ON orders (customer_id, order_date) WHERE shipped_date IS NULL;
CREATE INDEX idx_customer_lower_name ON customer (LOWER(company_name));A unique index rejects duplicates, like a UNIQUE constraint:
CREATE UNIQUE INDEX uq_countries_name ON countries (name);INSERT INTO countries VALUES ('XXX', 'France');ERROR: Operation would have caused one or more unique constraint violations. INDEX uq_countries_name ... key: 'France'Two indexes on the same columns with the same definition are not allowed: CoraDB reports Index "..." already defined for class "...".
Index comments and visibility are listed by SHOW INDEX and in the db_index catalog view:
SELECT index_name, is_unique, comment FROM db_index WHERE class_name = 'orders' ORDER BY index_name;| index_name | is_unique | comment |
|---|---|---|
| fk_orders_customer_id | NO | NULL |
| idx_orders_customer_date | NO | Orders of a customer, newest first |
| idx_orders_order_date | NO | NULL |
| idx_orders_shipped_date | NO | NULL |
| idx_orders_unshipped | NO | NULL |
| pk_orders_id | YES | NULL |
Online index creation
Section titled “Online index creation”A normal CREATE INDEX locks the table against changes until the index is built. WITH ONLINE keeps the table writable:
CREATE INDEX idx_order_items_discount ON order_items (discount_pct) WITH ONLINE PARALLEL 4;The build has three stages:
- The index is added to the schema with the status online building, under an exclusive schema lock that is then lowered so that other transactions can write to the table.
- The table is scanned in batches; the batches are sorted and loaded into the index. Changes committed by other transactions in the meantime are applied to the index as well.
PARALLELn uses n threads to sort and load the batches (the table scan itself uses one thread). - The lock is raised again and the index status becomes normal.
While the build runs, other sessions see the index in SHOW INDEX with Visible = NO and in ;schema with the note IN PROGRESS. For a UNIQUE index, other transactions may commit rows that violate uniqueness during the build; the violation is detected at the end and the index creation fails with a unique constraint error. In standalone mode (corasql -S), WITH ONLINE is ignored and a normal index is created.
Invisible indexes
Section titled “Invisible indexes”An invisible index is maintained like any other, but the optimizer ignores it. Use this to measure the effect of an index before relying on it, or to check what happens without an index before dropping it.
CREATE INDEX idx_city_postal ON city (postal_code) INVISIBLE;ALTER INDEX idx_city_postal ON city VISIBLE;ALTER INDEX
Section titled “ALTER INDEX”ALTER INDEX index_name ON table_name [COMMENT 'comment'] REBUILDALTER INDEX index_name ON table_name COMMENT 'comment'ALTER INDEX index_name ON table_name {VISIBLE | INVISIBLE}- REBUILD: drop and recreate the index with the same definition, for example after many deletes. Column names given after the table name are ignored.
- COMMENT without
REBUILD: change only the comment. - VISIBLE | INVISIBLE: let the optimizer use the index, or not.
ALTER INDEX idx_product_price ON product COMMENT 'price lookups' REBUILD;ALTER INDEX idx_product_price ON product COMMENT 'price lookups, highest first';ALTER INDEX idx_city_postal ON city INVISIBLE;An index cannot be renamed; drop it and create it again under the new name.
DROP INDEX
Section titled “DROP INDEX”DROP INDEX index_name ON table_nameDROP INDEX idx_product_price ON product;The table name is required. Indexes that belong to a constraint (primary key, unique, foreign key) are removed with ALTER TABLE ... DROP CONSTRAINT; a unique index created with CREATE UNIQUE INDEX can be dropped with either statement.
See also
Section titled “See also”- Query optimization: query plans, index hints, covering indexes and other index techniques
- CREATE TABLE: constraints and the indexes they create
- SHOW INDEX