Mysql – Large MySQL table, inserts with primary key check

MySQLPHP

I have a table with 12 million of rows and a following task I should frequently perform:

  1. Get search results from somewhere (50 rows). Each result has a key looking as a md5 hash and a table's PM is built on this field.
  2. Check what rows are currently stored.
  3. Store all other rows

Question is what is the best way to perform steps 2-3. I use PHP and Doctrine so not all tricky queries are possible to use. For example I can't use bulk inserts so need to run INSERT up to 50 times in a row.
I see two possible ways:

  • run SELECT … WHERE id IN(…) with all 50 IDs and see what is returned, then run as many inserts as I need
  • run 50 inserts and catch duplicated ID error

Best Answer

I think what you're looking is simply to use INSERT IGNORE. Forget about steps 1 and 2, just insert and ignore :)

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row is discarded and no error occurs. Ignored errors may generate warnings instead, although duplicate-key errors do not.