// dialect comparison

Delete only the first N rows

Prune a log table 100 rows at a time: the statement only MySQL writes natively.

Filed under Inserting & updating

The verdict

Selecting the victims' keys in a subquery is the portable pattern, and it is worth knowing even on MySQL because it extends to 'delete the oldest N per group' with a window function. Run it in a loop until it deletes zero rows.

Where they differ

Batched deletes are the standard trick to keep big clean-ups from holding long locks. MySQL is the only engine with first-class DELETE ... ORDER BY ... LIMIT; the rest target the same rows through an ordered subquery over the key or row id.

How each engine writes it

SQL Server

DELETE FROM events
WHERE id IN (SELECT TOP (100) id FROM events
             WHERE logged_at < '2024-01-01'
             ORDER BY logged_at);

DELETE TOP (100) exists but cannot take ORDER BY; the subquery form picks which rows go.

PostgreSQL

DELETE FROM events
WHERE ctid IN (SELECT ctid FROM events
               WHERE logged_at < '2024-01-01'
               ORDER BY logged_at
               LIMIT 100);

No DELETE LIMIT. Target rows by primary key instead of ctid if a concurrent process may rewrite them.

MySQL

DELETE FROM events
WHERE logged_at < '2024-01-01'
ORDER BY logged_at
LIMIT 100;

The only native DELETE ... ORDER BY ... LIMIT. An index on logged_at keeps each batch cheap to find; without ORDER BY, LIMIT is unsafe for statement-based replication.

SQLite

DELETE FROM events
WHERE rowid IN (SELECT rowid FROM events
                WHERE logged_at < '2024-01-01'
                ORDER BY logged_at
                LIMIT 100);

DELETE ... LIMIT exists only in builds compiled with SQLITE_ENABLE_UPDATE_DELETE_LIMIT; the subquery works everywhere.

Oracle

DELETE FROM events
WHERE rowid IN (SELECT rowid FROM events
                WHERE logged_at < DATE '2024-01-01'
                ORDER BY logged_at
                FETCH FIRST 100 ROWS ONLY);

Pick the victims with FETCH FIRST (12c+) and address them by rowid.

Questions that come up

Why batch big deletes at all?

One giant DELETE holds its locks and undo or log space until it finishes. Batches of a few thousand rows with a commit between them keep locks short, keep the log small and let replicas keep up.

What are ctid and rowid?

Physical row addresses: PostgreSQL's ctid, Oracle's ROWID and SQLite's rowid point straight at the rows the subquery picked. ctid changes whenever a row is updated, so in busy tables prefer the primary key.

Why not delete straight from a SELECT … LIMIT?

Only MySQL accepts ORDER BY and LIMIT on DELETE itself (SQLite only in special builds). Elsewhere the statement needs a way to say which rows, and the subquery over a key or row address is that list, written explicitly.

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.