Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql Performance: INSERT...ON DUPLICATE KEY UPDATE or UPDATE & ROW_COUNT

I query that most of the times will be updating an existing record. However, I need to check if this records exists and if not I should create it.

Usually I would use INSERT...ON DUPLICATE KEY UPDAT, but as only a few queries will need to insert I was thinking of doing an UPDATE and latter checking with if the ROW_COUNT() returned is zero and doing an INSERT in this case.

What would give me the best performance?

like image 266
IgalSt Avatar asked Mar 01 '26 07:03

IgalSt


1 Answers

MySQL has a REPLACE INTO syntax that could be what you are looking for. If that doesn't work for you, using UPDATE and checking ROW_COUNT() should work, you might want to wrap that whole thing in a stored proc so you save yourself a trip back to the server.

like image 194
bobwienholt Avatar answered Mar 03 '26 20:03

bobwienholt