Home Page
  • March 28, 2024, 04:36:06 pm *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: MySQL: Update multiple rows with different values  (Read 15619 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 535
    • View Profile
    • Dakusan's Domain
MySQL: Update multiple rows with different values
« on: October 03, 2016, 09:43:08 am »


There are 3 different methods for updating multiple rows at once in MySQL with different values:
  1. INSERT: INSERT with ON DUPLICATE KEY UPDATE

             INSERT INTO FooBar (ID, foo)
             VALUES (1, 5), (2, 8), (3, 2)
             ON DUPLICATE KEY UPDATE foo=VALUES(foo);
          
  2. TRANSACTION: Where you do an update for each record within a transaction (InnoDB or other DBs with transactions)

             START TRANSACTION;
             UPDATE FooBar SET foo=5 WHERE ID=1;
             UPDATE FooBar SET foo=8 WHERE ID=2;
             UPDATE FooBar SET foo=2 WHERE ID=3;
             COMMIT;
          
  3. CASE: In which you a case/when for each different record within an UPDATE

             UPDATE FooBar SET foo=CASE ID
                WHEN 1 THEN 5
                WHEN 2 THEN 8
                WHEN 3 THEN 2
             END
             WHERE ID IN (1,2,3);
          

I feel knowing the speeds of the 3 different methods is important.

All of the following numbers apply to InnoDB.


I just tested this, and the INSERT method was 6.7x faster for me than the TRANSACTION method. I tried on a set of both 3,000 and 30,000 rows and got the same results.


The TRANSACTION method still has to run each individually query, which takes time, though it batches the results in memory, or something, while executing. The TRANSACTION method is also pretty expensive in both replication and query logs.


Even worse, the CASE method was 41.1x slower than the INSERT method w/ 30,000 records (6.1x slower than TRANSACTION). And 75x slower in MyISAM. INSERT and CASE methods broke even at ~1,000 records. Even at 100 records, the CASE method is BARELY faster.


So in general, I feel the INSERT method is both best and easiest to use. The queries are smaller and easier to read and only take up 1 query of action. This applies to both InnoDB and MyISAM.


Bonus stuff:

Using the INSERT method, there can be a problem in which NON-NULL fields with no default (in other words, required fields) are not being updated. You will get an error like “Field 'fieldname' doesn't have a default value”. The solution is to temporarily turn off STRICT_TRANS_TABLES and STRICT_ALL_TABLES in the SQL mode: SET SESSION sql_mode=REPLACE(REPLACE(@@SESSION.sql_mode,"STRICT_TRANS_TABLES",""),"STRICT_ALL_TABLES",""). Make sure to save the sql_mode first if you plan on reverting it.


As for other comments I’ve seen that say the auto_increment goes up using the INSERT method, I tested that too and it seems to not be the case.


Code to run the tests is as follows: (It also outputs .SQL files to remove PHP interpreter overhead)

<?
//Variables
$NumRows=30000;

//These 2 functions need to be filled in
function InitSQL()
{

}
function RunSQLQuery($Q)
{

}

//Run the 3 tests
InitSQL();
for($i=0;$i<3;$i++)
   RunTest($i, $NumRows);

function RunTest($TestNum, $NumRows)
{
   $TheQueries=Array();
   $DoQuery=function($Query) use (&$TheQueries)
   {
       RunSQLQuery($Query);
       $TheQueries[]=$Query;
   };

   $TableName='Test';
   $DoQuery('DROP TABLE IF EXISTS '.$TableName);
   $DoQuery('CREATE TABLE '.$TableName.' (i1 int NOT NULL AUTO_INCREMENT, i2 int NOT NULL, primary key (i1)) ENGINE=InnoDB');
   $DoQuery('INSERT INTO '.$TableName.' (i2) VALUES ('.implode('), (', range(2, $NumRows+1)).')');

   if($TestNum==0)
   {
       $TestName='Transaction';
       $Start=microtime(true);
       $DoQuery('START TRANSACTION');
       for($i=1;$i<=$NumRows;$i++)
           $DoQuery('UPDATE '.$TableName.' SET i2='.(($i+5)*1000).' WHERE i1='.$i);
       $DoQuery('COMMIT');
   }

   if($TestNum==1)
   {
       $TestName='Insert';
       $Query=Array();
       for($i=1;$i<=$NumRows;$i++)
           $Query[]=sprintf("(%d,%d)", $i, (($i+5)*1000));
       $Start=microtime(true);
       $DoQuery('INSERT INTO '.$TableName.' VALUES '.implode(', ', $Query).' ON DUPLICATE KEY UPDATE i2=VALUES(i2)');
   }

   if($TestNum==2)
   {
       $TestName='Case';
       $Query=Array();
       for($i=1;$i<=$NumRows;$i++)
           $Query[]=sprintf('WHEN %d THEN %d', $i, (($i+5)*1000));
       $Start=microtime(true);
       $DoQuery("UPDATE $TableName SET i2=CASE i1\n".implode("\n", $Query)."\nEND\nWHERE i1 IN (".implode(',', range(1, $NumRows)).')');
   }

   print "$TestName: ".(microtime(true)-$Start)."
\n";

   file_put_contents("./$TestName.sql", implode(";\n", $TheQueries).';');
}
Logged