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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With