// dialect comparison

DESC in index definitions across every SQL dialect

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 Индексы

The verdict

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.

Where they differ

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.

How each engine writes it

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.

PostgreSQL

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.

MySQL

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.

SQLite

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.

Oracle

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.

Questions that come up

When does DESC actually matter?

Only with mixed directions, like ORDER BY a ASC, b DESC. A single-column DESC index buys nothing over scanning an ASC index backwards.

My MySQL 5.7 ignores DESC?

Correct: 5.7 parses DESC in CREATE INDEX and discards it, building an ascending index. Upgrade to 8.0+ for real descending key parts.

How do I verify the sort was avoided?

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.

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.