I use PostgreSQL 8.4, but i think my question can be expanded to most of the RDBMS.
I need to perform data-changing operation like update or delete for those rows, where the specified column has value from the specified set. For example I want to delete those rows, where id is in (1,4,7,8).
The whole operation should either succeed or fail, so I have two options here:
- Use
INsyntax, like
DELETE FROM my_table WHERE id IN (1,4,7,8)
- Use several single operations executed within one transaction, like
DELETE FROM my_table WHERE id = 1;
DELETE FROM my_table WHERE id = 4;
...
Is there any difference between these two approaches when executed as plain SQL commands? Which one is better?
The same questions when using JDBC prepared statements for these operations?
You should always use transactions, in my opinion. With the several-operation approach you will have many more round trips across the network. There may also be a difference in what indexes get used, so check out the query plan.