truncate vs delete
Tags: sql, truncate, delete, mysql
Published: 2022-12-19 16:36:56

The differences between the TRUNCATE TABLE and DELETE FROM TABLE SQL statements can be very significant, depending on the case in which you use these commands. Below we have a brief overview of the most important differences.


**sql delete command:**

a) Deletes all or some records from the table, you can limit the records to be deleted by using the WHERE clause

b) Does not free the space occupied by the data in the table (in the TABLESPACE - on the disk)

c) Does not reset the SEQUENCE value assigned to the table

d) DELETE works much slower than TRUNCATE

e) DELETE can be applied to tables and tables in a cluster (can be Oracle specific)

f) Oracle - For DELETE, you can use the GRANT command

g) The DELETE operation does not make unusable indexes usable again

h) DELETE requires a shared table lock

i) Triggers fire

j) DELETE can be used in the case of: database link

k) DELETE returns the number of records deleted

l) Transaction log - for each deleted record (deletes rows one at a time and records an entry in the transaction log for each deleted row) - slower execution than TRUNCATE. The table may still contain blank pages after executing the DELETE statement. DELETE needs to read records, check constraints, update block, update indexes, and generate redo / undo. All of this takes time, hence it takes time much longer than with TRUNCATE

m) DELETE uses more transaction space than the TRUNCATE statement

n) DELETE can be used with indexed views

o) It is a DML (Data Manipulation Language) command, therefore the following commands are used for this command: COMMIT and ROLLBACK


**sql truncate command:**

a) Deletes all records from the table, records cannot be limited to deletion. For Oracle, when the table is split per partition, individual partitions can be truncated (TRUNCATE) in isolation, making it possible to partially remove all data from the table

b) Frees up the space occupied by the data in the table (in the TABLESPACE - on disk). For Oracle - if you use the REUSE STORAGE clause, the data segments will not be rolled back, i.e. you will keep space from the deleted rows allocated to the table, which can be a bit more efficient if the table is to be reloaded with data. The high mark will be reset

c) Resets the SEQUENCE value assigned to the table to zero. However, the following options can be used: RESTART IDENTITY or CONTINUE IDENTITY

d) TRUNCATE works much faster than DELETE

e) TRUNCATE only affects tables or the entire cluster (may be Oracle specific)

f) Oracle - TRUNCATE cannot be granted (GRANT) without using DROP ANY TABLE

g) The TRUNCATE operation makes unusable indexes usable again

h) TRUNCATE requires an exclusive table lock, therefore, turning off exclusive table lock is a way to prevent TRUNCATE operation on the table

i) DML triggers do not fire after executing TRUNCATE (so be very careful in this case, you should not use TRUNCATE, if a delete trigger is defined in the table to perform an automatic table cleanup or a logon action after row deletion). On Oracle, DDL triggers are fired

j) Oracle - TRUNCATE cannot be used in the case of: database link

k) TRUNCATE does not return the number of records deleted

l) Transaction log - one log indicating page deallocation (removes data, releasing allocation of data pages used for storing table data and writes only page deallocations to the transaction log) - faster execution than DELETE. TRUNCATE only needs to adjust the pointer in the database to the table (High Water Mark) and the data is immediately deleted, therefore it uses less system resources and transaction logs

m) TRUNCATE takes up less transaction space than the DELETE statement

n) TRUNCATE cannot be used with indexed views

o) It is a DDL (Data Definition Language) command, therefore commands such as COMMIT and ROLLBACK do not apply to this command (the exceptions here are PostgreSQL and MSSQL, whose implementation of the TRUNCATE command allows the command to be used in a transaction)


**more details:**
[truncate vs delete](https://rozwoj-oprogramowania.pl/en/blog/databases/truncate-vs-delete.html)