I want to know what is the optimal approach for inserting a duplicate record. I am using Postgres. I am trying 2 approaches and have timing metrics on the results but I would like to know which is best or if there is another way.
- Perform INSERTthen if there is a duplicate key exception, I handle that.
- Before performing INSERTdo aSELECT, if the record exists I will not perform theINSERT.
I feel approach 1 is best as there is only one request to the DB but if there are many duplicates then approach 1 is quite slow, due to the duplicate key exception handling I assume.
In my use case, I am expecting round 3-4% duplicates.
