// dialect comparison

Build an index without blocking writes

Indexes on live tables: keep writes flowing while the index builds.

Filed under Tables & schema

The verdict

PostgreSQL's CONCURRENTLY works in every edition; SQL Server and Oracle reserve online builds for their enterprise editions; MySQL's InnoDB builds online by default; SQLite is the one engine where you schedule the pause.

Where they differ

A plain CREATE INDEX blocks writes for the whole build in PostgreSQL, SQL Server and Oracle. PostgreSQL's CONCURRENTLY, SQL Server's ONLINE = ON and Oracle's ONLINE lift that; MySQL's InnoDB builds secondary indexes online by default. SQLite holds the database write lock for the whole build and has no alternative.

How each engine writes it

SQL Server

CREATE INDEX ix_orders_total ON orders (total) WITH (ONLINE = ON);

Enterprise and Developer editions and Azure SQL only. The build takes a short shared lock at the start and a schema-modification lock at the end; in between, DML continues.

PostgreSQL

CREATE INDEX CONCURRENTLY ix_orders_total ON orders (total);

Two table scans, slower, and it cannot run inside a transaction block. A failed build leaves an INVALID index you should DROP INDEX CONCURRENTLY.

MySQL

ALTER TABLE orders ADD INDEX ix_orders_total (total),
  ALGORITHM = INPLACE, LOCK = NONE;

InnoDB online DDL. Secondary index builds are INPLACE with concurrent DML by default; spelling out ALGORITHM and LOCK (also accepted by CREATE INDEX) makes the statement fail instead of silently blocking if online isn't possible.

SQLite

CREATE INDEX ix_orders_total ON orders (total);

No online build: the database write lock is held until the index is done. In WAL mode readers keep working; other writers wait or get SQLITE_BUSY.

Oracle

CREATE INDEX ix_orders_total ON orders (total) ONLINE;

ONLINE needs Enterprise Edition. Add PARALLEL n for big builds. If an online build is interrupted, DBMS_REPAIR.ONLINE_INDEX_CLEAN tidies up before you retry.

Questions that come up

Why is CONCURRENTLY slower in PostgreSQL?

It scans the table twice and waits for every transaction that might still use the old snapshot to finish, so it can take much longer than a plain build. It also can't run inside a transaction block.

What is an INVALID index?

A PostgreSQL index left behind by a failed CONCURRENTLY build. Queries ignore it but every write still maintains it. Find them with SELECT indexrelid::regclass FROM pg_index WHERE NOT indisvalid, then DROP INDEX CONCURRENTLY and rebuild.

Does online index creation cost anything in MySQL?

The build still uses I/O and buffer pool, and concurrent changes are logged and replayed at the end. The DDL also needs a brief exclusive metadata lock at its start and end, so a long-running transaction on the table can stall it, and new queries queue behind it.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.