Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is faster? Multiple DELETE statements or a single DELETE Statement using "IN ('x', ''y')"

Just wondering which is faster:

DELETE FROM table_name WHERE X='1'
DELETE FROM table_name WHERE X='2'
DELETE FROM table_name WHERE X='3'

or

DELETE FROM table_name WHERE X IN ('1', '2', '3')

Any ideas, hints, or references where to read?

Thank you all!

like image 660
Newbie Avatar asked Dec 30 '08 21:12

Newbie


People also ask

How do you delete multiple records in SQL?

If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.

Which statement is used to delete record?

The DELETE statement is used to delete existing records in a table.


1 Answers

The single delete will be faster for a few reasons:

  1. Only one plan will need to be generated
  2. Only one transaction will be involved (if you are using them)
  3. If you are running these from code, then there is less overhead with ODBC calls and network traffic
  4. Any indexes will need to be refreshed just once, not many times.
like image 132
JosephStyons Avatar answered Sep 22 '22 01:09

JosephStyons