tags:

views:

77

answers:

3

Given a query like the following:

DELETE FROM FOO WHERE ID in (1,2,3,4,....);
  • Is there an upper limit to the number of values in the inclusion? (I've seen Oracle complain at 1000, but that was a long time ago. I have no idea if that was installation-dependant, oracle-version dependant, etc. etc., or how mysql limits this.

  • What are the performance implications? Would it be faster to break up the set of values into smaller subsets and send multiple delete requests?

+2  A: 

If you want to be sure that your statement will always work, put them in a temp table and do the IN statement against it.

Otávio Décio
How exactly would that be done? "DELETE FROM FOO WHERE ID in bar.id;"?
Thomas
DELETE FROM FOO WHERE ID IN (SELECT ID FROM BAR)
Otávio Décio
Ah...thank you very much.
Thomas
Your solution is useful in that there are other ways to accomplish this; however I'm actually looking for information on big, honking "IN" clauses, and whether or not they'll make the db explode.Additionally, given that you'll have to do an insert and then a join, how will that impact the performance? If I were checking against a much larger number of id's this might be a good solution, though.
Steve B.
@Steve - I understand. It just gives me the creeps.
Otávio Décio
A: 

See also http://stackoverflow.com/questions/1069415/t-sql-where-col-in (it's for t/sql, but the answers are general)

Hans Kesting
+4  A: 

From the docs:

The number of values in the IN list is only limited by the max_allowed_packet value.

As to performance, I would break up the queries when they start approaching a thousand IDs or so. IN is pretty well-optimized, but it still doesn't seem like any good can come of throwing enormous lists at it.

chaos
Thank you for posting from the doc - I don't know why people don't do this immediately for questions like this.
Jonathan Sampson