Each of the possibilities has different tradeoffs, so there isn't a clear single answer. Some notes:
If you use
SELECT
followed byINSERT
orUPDATE
, you will need to use transactionsSTART TRANSACTION
andCOMMIT
or setautocommit = 0
, and then useSELECT ... FOR UPDATE
otherwise you may have conflicts with other transactions. This has the advantage though that you can potentially avoid doing anything if there are no conflicts.Using
DELETE
followed byINSERT
is manually doing exactly whatREPLACE
does. TheREPLACE
statement in a single statement and single transaction attempts toINSERT
in a loop, and each time a key conflicts, it deletes the offending row. Doing this manually is more work both for you and the database and is trickier to implement correctly.