Why TRUNCATE is 100x Faster Than DELETE
🎯 The Question
"If both
DELETE FROM table;andTRUNCATE TABLE table;remove all records from a table, why does TRUNCATE execute in 5 milliseconds while DELETE takes 2 minutes on 10 million rows?"
⚡ 30-Second Elevator Pitch
DELETEis a Data Manipulation Language (DML) row-by-row operation:- Scans every single row in the table.
- Writes a full rollback record to the Undo / WAL log for every deleted row so the transaction can be rolled back.
- Updates and rebalances every index for every single deletion.
- Fires row-level triggers (
ON DELETE) for every row.
TRUNCATEis a Data Definition Language (DDL) storage operation:- Deallocates the entire data pages / extents assigned to the table at the metadata level.
- Logs only the page deallocation in the WAL log (a few bytes).
- Bypasses all row-level triggers and index rebalancing, completing in constant time.
🧠 Under-the-Hood: Row-by-Row Logging vs. Page Extent Deallocation
🔬 Space Reclamation & Identity Resets
- Space Reclamation:
DELETEmarks rows as dead tuples, but leaves the disk space allocated to the table file (causing table bloat untilVACUUM FULL/OPTIMIZE TABLEruns).TRUNCATEimmediately releases all disk pages back to the operating system or tablespace free pool.
- Auto-Increment Counters:
TRUNCATEresets theAUTO_INCREMENT/IDENTITYsequence back to 1.DELETEretains the current sequence counter.
📌 Comparison Matrix: DELETE vs. TRUNCATE
| Feature | DELETE FROM table; | TRUNCATE TABLE table; |
|---|---|---|
| Command Type | DML (Data Manipulation) | DDL (Data Definition) |
| Execution Mechanics | Row-by-row deletion & logging | Page extent deallocation in metadata |
| Time Complexity | 🐢 Linear | ⚡ Constant |
| WAL / Undo Log Generation | Massive (Logs every deleted row) | Minimal (Logs page deallocations) |
| WHERE Clause Filter | ✅ Supported (WHERE id > 50) | ❌ Not Supported (All rows cleared) |
| Triggers | Fires ON DELETE row triggers | Does NOT fire row-level triggers |
| Identity Counter Reset | Counter preserved | Resets AUTO_INCREMENT to seed |
| Disk Space Recovery | Requires manual VACUUM/OPTIMIZE | Reclaims physical disk space instantly |
💡 What Interviewers Ask Next (Follow-Up Traps)
-
"Can TRUNCATE be rolled back inside a transaction?"
- Answer: In PostgreSQL and SQL Server: YES! Because DDL commands are transactional in Postgres and SQL Server,
TRUNCATEcan be rolled back if insideBEGIN ... ROLLBACK;. In MySQL (InnoDB): NO, because DDL operations issue an implicit commit in MySQL.
- Answer: In PostgreSQL and SQL Server: YES! Because DDL commands are transactional in Postgres and SQL Server,
-
"Why does TRUNCATE fail if a foreign key references the table?"
- Answer: Because
TRUNCATEdoes not scan rows or check foreign key constraints row-by-row. To prevent orphaned records in child tables, SQL engines blockTRUNCATEon tables referenced by foreign keys until the foreign key constraint is dropped or disabled.
- Answer: Because
Placement & Interview Takeaway
Interview Answer: TRUNCATE is 100x faster than DELETE because it deallocates the table's underlying storage pages via DDL metadata operations in time. In contrast, DELETE processes rows individually, writing extensive undo logs and updating secondary indexes for every row.
📺 Video Explanation
💬