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).
Try adding an insert trigger that does a pre-flight check and cancels the insert on duplicate key (after updating the existing row).
Not sure it'll scale well for bulk inserts, let alone work for load data infile, but it's the best I can think of. :-)
If you were using Oracle or Microsoft SQL, you could use the
MERGE
. However, MySQL does not have a direct correlation to that statement. There is the single-row solution that you mentioned but, as you pointed out, it doesn't do bulk very well. Here is a blog post I found on the difference between Oracle and MySQL and how to do what Oracle does withMERGE
in MySQL:http://blog.mclaughlinsoftware.com/2009/05/25/mysql-merge-gone-awry/
It isn't a pretty solution and it probably isn't as full a solution as you would like, but I believe that is the best there is for a solution.
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.
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.
Although this question has been answered correctly already (that MySQL does support this via ON DUPLICATE UPDATE with the expected multiple value set syntax), I'd like to expand on this by providing a demonstration that anyone with MySQL can run:
The output is as follows:
You can insert/update multiple rows using INSERT ... ON DUPLICATE KEY UPDATE. The documentation has the following example:
Or am I misunderstanding your question?