In theory, we would expect the best delete
performance for a table without any indexes—as it is for insert
. If there is no index, however, the database must read the full table to find the rows to be deleted. That means deleting the row would be fast but finding would be very slow. This case is therefore not shown in Figure 8.2.
Nevertheless it can make sense to execute a delete
statement without an index just as it can make sense to execute a select
statement without an index if it returns a large part of the table.
Even delete
and update
statements have an execution plan.
A delete
statement without where
clause is an obvious example in which the database cannot use an index, although this is a special case that has its own SQL command: truncate table
. This command has the same effect as delete
without where
except that it deletes all rows in one shot. It is very fast but has two important side effects: (1) it does an implicit commit
(exceptions: PostgreSQL and SQL Server); (2) it does not execute any triggers.
You can’t learn everything in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up. Have a look at modern-sql.com as well.
The essence of SQL tuning in 200 pages
Buy now!
(paperback and/or PDF)
Paperback also available at Amazon.com.
Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »