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.
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.