0

Hello folks!

We're working on a multithreading application.

We need to insert information into a MySQL database, ONLY if it hasn't been inserted before.

The current SQL query:

INSERT INTO core_links SET scan_id = '$scan->id', address = '$link_new'

There's more than 600 records inserted per second, and I would like to avoid using a select query before. Any possible solution?

Thanks in advance!

Chris Russo
  • 422
  • 7
  • 18
  • Possible duplicate of http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql – Aiias Mar 24 '13 at 04:18
  • Your answer is here http://stackoverflow.com/questions/2219786/best-way-to-avoid-duplicate-entry-into-mysql-database – Raphael Caixeta Mar 24 '13 at 04:18

2 Answers2

2

INSERT IGNORE is what you are looking for.

INSERT IGNORE INTO core_links SET scan_id = '$scan->id', address = '$link_new'

Luke Shaheen
  • 4,046
  • 11
  • 47
  • 78
  • Yeah, I messed up (searching on Google for 'insert ignore mysql' gave me the 5.5). However, I would suggest updating your answer to linking to the page you just commented with; the page you link to doesn't reference `INSERT IGNORE` – Chris Forrence Mar 24 '13 at 04:23
0

If the table's primary key is a composite of scan_id and address (or maybe only scan_id), then the table itself will reject duplicate values.

No'am Newman
  • 5,936
  • 5
  • 33
  • 48