SQL Server
CREATE INDEX idx_orders_customer_created ON orders (customer_id ASC, created_at DESC);
DESC/ASC per key column, fully supported. The usual fix for ORDER BY customer_id, created_at DESC avoiding a sort.
A descending index reverses the stored order of a column, letting ORDER BY a ASC, b DESC walk the index without a sort step.
Filed under Index
Same syntax everywhere and MySQL finally honors it since 8.0. Remember the free trick: any engine can read a single-column ASC index backwards, so a plain DESC index is rarely worth it on its own.
The syntax is the same everywhere; the catch is MySQL, where DESC in the index definition was parsed and silently ignored before 8.0, so the mixed-direction optimization only exists on modern versions.
CREATE INDEX idx_orders_customer_created ON orders (customer_id ASC, created_at DESC);
DESC/ASC per key column, fully supported. The usual fix for ORDER BY customer_id, created_at DESC avoiding a sort.
CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at DESC);
Supported, plus NULLS FIRST/LAST control inside the index definition. A single-column index is scanned backwards for free, so DESC only matters in mixed-direction keys.
CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at DESC);
Descending key parts are real since 8.0 (before that DESC was accepted and ignored). EXPLAIN shows Using filesort when the index cannot supply the order.
CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at DESC);
Supported since 3.3, and SQLite scans single-column indexes backward at no cost, so DESC matters only in multi-column keys.
CREATE INDEX idx_orders_customer_created ON orders (customer_id, created_at DESC);
Oracle implements a DESC key column as a function-based index; the column shows up in USER_IND_EXPRESSIONS. A single-column DESC index is pointless because the optimizer can scan an ascending one backwards.
Only with mixed directions, like ORDER BY a ASC, b DESC. A single-column DESC index buys nothing over scanning an ASC index backwards.
Correct: 5.7 parses DESC in CREATE INDEX and discards it, building an ascending index. Upgrade to 8.0+ for real descending key parts.
Look for the missing step: no Sort operator in SQL Server's plan, no Sort node in PostgreSQL's EXPLAIN, no Using filesort in MySQL, no USE TEMP B-TREE FOR ORDER BY in SQLite's EXPLAIN QUERY PLAN, and no SORT ORDER BY in Oracle's plan.
The bread-and-butter index statement: nearly identical everywhere, with a few differences in naming and locking.
A covering index answers a query without touching the table. SQL Server and PostgreSQL have an INCLUDE clause that keeps extra columns out of the key; the rest use composite indexes.
An expression index stores a function result instead of the raw column, which makes WHERE LOWER(email) = ? use an index.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.