views:

593

answers:

7

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!

+3  A: 

A single delete is faster, there is only one plan to create and execute.

Otávio Décio
+13  A: 

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.
JosephStyons
What is a good way to generate a legitimate IN() string, and one that might get really long?
Haoest
@Haoest, that should just be a separate question. But most DBs have a max length of the in-list. Look into a nested subquery.
BQ
Re: #1 would this still be an issue if you used a prepared statements?Re: #2 If you don't have auto commit enabled and you manually started your transaction wouldn't everything be a single transaction?
Zoredache
If you have lots of values for IN(), it could be way better to assemble them in temporary table and DELETE with a JOIN on that table.
Gnudiff
+3  A: 

The single statement is faster for reasons already stated.

In the example given

DELETE FROM table_name WHERE X BETWEEN 1 AND 3

will be even faster, especially if you have a clustered index on X.

WOPR
+1  A: 

Profile it in your DB with your indexes and your data. I'm inclined to agree with using a single statement, though, I can think of a few quick instances were the 3 statements would be much faster. If it really matters, profile it.

sam
+1  A: 

A single delete is generally faster for the reasons kogus mentioned.

But ... keep in mind that if you have to wipe out 50% of a 2 million row table and there is a lot of activity against the table, deleting in small batches or selecting into a new table and swapping that table in may be better approaches.

Sam Saffron
+1  A: 

The single statement:

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

...would be faster. I'm not sure what database you're using, but I'd recommend looking into the execution plan of your queries. If you're using MySQL, you can use the EXPLAIN command like:

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

Also as you've wrote in the comments if you're looking to dynamically fill you IN() clause you can use a subquery like:

DELETE FROM table_name WHERE x IN (SELECT id FROM table_name WHERE Y = Z)

(or whatever)

sammich
It's tag "sqlserver"
Juan Manuel
A: 

There is a third option that I think you may want to consider. I think it may be better then your first example, but not as good as your second.

If your database supports using prepared statements then you could do a prepare on the statement.

DELETE FROM table_name WHERE X='?'

Then simply calling that with value of 1, 2, and 3.

Generally my experience has been that you get the best performance when you use a set-based operation like your second example.

Zoredache