Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL bulk INSERT or UPDATE

Tags:

sql

mysql

bulk

Is there any way of performing in bulk a query like INSERT OR UPDATE on the MySQL server?

INSERT IGNORE ... 

won't work, because if the field already exists, it will simply ignore it and not insert anything.

REPLACE ... 

won't work, because if the field already exists, it will first DELETE it and then INSERT it again, rather than updating it.

INSERT ... ON DUPLICATE KEY UPDATE 

will work, but it can't be used in bulk.

So I'd like to know if there's any command like INSERT ... ON DUPLICATE KEY UPDATE that can be issued in bulk (more than one row at the same time).

like image 504
rid Avatar asked Jun 08 '11 23:06

rid


People also ask

Does MySQL support bulk insert?

The INSERT statement in MySQL also supports the use of VALUES syntax to insert multiple rows as a bulk insert statement. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.

What is bulk update in MySQL?

By bulk updating only one query can be sent to the server instead of one query for each row to update. The cases should contain all possible parameters looked up in the WHERE clause.

Does update take more time than insert?

Insertion is inserting a new key and update is updating the value of an existing key. If that is the case (a very common case) , update would be faster than insertion because update involves an indexed lookup and changing an existing value without touching the index.

What is the difference between update and insert?

Insert is for adding data to the table, update is for updating data that is already in the table.


2 Answers

You can insert/update multiple rows using INSERT ... ON DUPLICATE KEY UPDATE. The documentation has the following example:

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); 

Or am I misunderstanding your question?

like image 77
user359996 Avatar answered Sep 21 '22 22:09

user359996


One possible way to do this is to create a temporary table, insert the data into that, and then do 1 query with a join to insert the records that don't exist followed by and update to the fields that do exist. The basics would be something like this.

CREATE TABLE MyTable_Temp LIKE MyTable  LOAD DATA INFILE..... INTO MyTable_Temp  UPDATE MyTable INNER JOIN  MyTable_Temp ON MyTable.ID=MyTable_Temp.ID SET MyTable.Col1=MyTable_Temp.Col1, MyTable.Col2=MyTable_Temp.Col2.....  INSERT INTO MyTable(ID,Col1,Col2,...) SELECT ID,Col1,Col2,...  FROM MyTable_Temp LEFT JOIN MyTable  ON MyTable_Temp.ID = MyTable.ID WHERE myTable.ID IS NULL  DROP TABLE MyTable_Temp 

The syntax may not be exact, but this should give you the basics. Also, I know it's not pretty, but it gets the job done.

Update

I swapped the order of the insert and update, because doing insert first causes all the inserted rows to be updated when the update is called. If you do update first, only the existing records are updated. This should mean a little less work for the server, although the results should be the same.

like image 38
Kibbee Avatar answered Sep 22 '22 22:09

Kibbee