Showing posts with label bundle update. Show all posts
Showing posts with label bundle update. Show all posts

Friday, September 24, 2010

Update multiple records with single query, or Bundle Record Update

Actually it's a lie, it's not a single query, it's 3 queries, but it's still much more sufficient if you deal with more than 3 records to be updated with different values.

So, the problem is to run lot of update queries like:
update table_name set value_column='value1' where key_column = key1;
update table_name set value_column='value2' where key_column = key2;
...
update table_name set value_column='valueN' where key_column = keyN;

If you have N more than 3, then you should test the performance of the following approach:
1. create temporary table temp_table(k int,v varchar);
2. insert int temp_tabl values
(key1,'value1'),
(key2,'value2'),
...,
(keyN,'valueN')

3. update table_name, temp_table set
table_name.value_column=temp_table.v
where table_name.key_column=temp_table.k

For ~50 records, it works ~2 times faster, so think about it.

Как-то-так