Skip to content

CREATE, ALTER and DROP INDEX

View as Markdown

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 [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. NULL values 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. VISIBLE is 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

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:

  1. 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.
  2. 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. PARALLEL n uses n threads to sort and load the batches (the table scan itself uses one thread).
  3. 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.

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 index_name ON table_name [COMMENT 'comment'] REBUILD
ALTER 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 index_name ON table_name
DROP 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.