// indexes reference

SQL CREATE INDEX

Builds the structure that makes lookups and sorts fast, at the cost of write overhead. The core clause is shared; everything interesting (covering columns, partial filters, online builds) is spelled differently.

카테고리
인덱스
In this category
1 of 2

구문

CREATE [UNIQUE] INDEX index_name ON table_name (column [ASC | DESC] [, ...])
CREATE INDEX i ON t (col) INCLUDE (other_col) [WHERE predicate]  -- SQL Server
CREATE [UNIQUE] INDEX [CONCURRENTLY] i ON t (expression) [WHERE predicate]  -- PostgreSQL
CREATE INDEX i ON t (LOWER(email))  -- expression index: PostgreSQL, SQLite, Oracle; MySQL 8.0.13+ needs double parentheses: ((LOWER(email)))

Each block is a separate form. The support table and the examples show which engine takes which.

엔진 지원

엔진Support메모
SQL Server 예 INCLUDE columns, filtered WHERE, ONLINE = ON on supported editions
PostgreSQL 예 CONCURRENTLY builds without blocking writes; partial, expression and USING method
MySQL 예 prefix indexes col(n); functional key parts from 8.0.13; no IF NOT EXISTS
SQLite 예 IF NOT EXISTS; partial WHERE; expression indexes from 3.9
Oracle 예 function-based, bitmap, INVISIBLE and ONLINE builds

예시

Any engine

CREATE INDEX idx_orders_customer ON orders (customer_id);

PostgreSQL

CREATE INDEX CONCURRENTLY idx_orders_placed
ON orders (placed_at DESC)
WHERE status <> 'cancelled';

SQL Server

CREATE NONCLUSTERED INDEX idx_orders_customer
ON dbo.orders (customer_id)
INCLUDE (placed_at, total)
WHERE status = 'open';

MySQL

-- 8.0.13+: note the double parentheses
CREATE INDEX idx_users_email_lc ON users ((LOWER(email)));

SQLite

CREATE INDEX IF NOT EXISTS idx_orders_placed
ON orders (placed_at DESC)
WHERE status <> 'cancelled';

Oracle

CREATE INDEX idx_customers_email_lc ON customers (LOWER(email)) ONLINE;

알아 두면 좋은 정보

  • Index-name scope differs: SQL Server and MySQL keep names per table, so two tables can each have idx_email. PostgreSQL, Oracle and SQLite share one namespace per schema or database, where the same name cannot be reused anywhere else.
  • PostgreSQL's CONCURRENTLY, Oracle's ONLINE and SQL Server's ONLINE = ON (where licensed) let reads and writes continue while the index builds. A plain build holds a lock that blocks writes for the duration, which is minutes on a big table.
  • MySQL has no IF NOT EXISTS for CREATE INDEX and no partial indexes: WHERE is not part of its grammar. Filtered or partial indexes are PostgreSQL, SQLite and SQL Server territory.
  • Expression indexes index a function's result: CREATE INDEX idx_users_email_lc ON users (LOWER(email)) makes LOWER(email) = ? fast on PostgreSQL, SQLite and Oracle, and on MySQL 8.0.13+ with double parentheses: ((LOWER(email))).

Questions about CREATE INDEX

How do I create an index without blocking writes?

PostgreSQL: CREATE INDEX CONCURRENTLY. Oracle: CREATE INDEX ... ONLINE. SQL Server: ONLINE = ON on editions that support it. MySQL's InnoDB builds secondary indexes online by default. SQLite has no such option, so build on a quiet database.

What is the difference between INCLUDE and an indexed column?

INCLUDE columns (SQL Server; PostgreSQL 11+ has the same clause) are stored in the index but are not part of the search key. The index can then cover a query, with no table lookup, without bloating the key or hitting key-size limits.

Why is my function index not used?

The query must spell the expression the same way the index does. An index on LOWER(email) does not help WHERE email = 'x', only WHERE LOWER(email) = 'x'. MySQL additionally needs the double parentheses and 8.0.13+.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.