I have a table with columns defined under a unique index. When inserting data into the table, I’m currently not checking to see if a matching row already exists, instead letting MySQL handle it.
Is this the preferred approach? I mean, I could add a SELECT to check for dupe rows before insert, but it seems that would be more expensive from performance perspective, since maybe like 5-10% of inserts will trigger a dupe.
Yes, just let MySQL figure it out. It already knows what the next possible value is and will pick it much faster than you can.
Not only could your
selectmethod fail, it also involves two round-trips to the server instead of one. Not to mention that just letting the server figure it out gives it more opportunity to optimize.