0

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.

  1. Perform INSERT then if there is a duplicate key exception, I handle that.
  2. Before performing INSERT do a SELECT, if the record exists I will not perform the INSERT.

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.

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
mrmannione
  • 611
  • 9
  • 22

0 Answers0