While executing an INSERT
statement with many rows, I want to skip duplicate entries that would otherwise cause failure. After some research, my options appear
INSERT...ON DUPLICATE KEY UPDATE
is prefered to prevent unexpected Exceptions management.
In my case I know that col1
and col2
make a unique composite index.
It keeps track of the error, but does not throw an exception on duplicate. Regarding the performance, the update by the same value is efficient as MySQL notices this and does not update it
INSERT INTO table
(col1, col2, col3, col4)
VALUES
(?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
col1 = VALUES(col1),
col2 = VALUES(col2)
The idea to use this approach came from the comments at phpdelusions.net/pdo.