What is the faster statement? A MERGE or Select->Update/Insert depending on if it returns a value?
I'm trying to optimize a database, but I haven't found any speed tests comparing the two.
I found the MERGE command through a StackOverflow answer
mergeoracleperformance
What is the faster statement? A MERGE or Select->Update/Insert depending on if it returns a value?
I'm trying to optimize a database, but I haven't found any speed tests comparing the two.
I found the MERGE command through a StackOverflow answer
Best Answer
A
MERGE
would be more efficient since you're only executing the query once. Even better would be to do a set-basedMERGE
rather than doing a bunch of single-rowMERGE
statements which is what I'm assuming you'd be implementing given the alternative of doing aSELECT
to see if the row exists.Rather than doing a
SELECT
and then determining whether to do anINSERT
or anUPDATE
, you would be better off doing theUPDATE
, checking to see if you updated 0 rows or not, and then doing theINSERT
if yourUPDATE
didn't affect any rows. That way, source rows that require anUPDATE
would be a bit more efficient and rows that require anINSERT
would be essentially just as fast as if you had done aSELECT
andINSERT
. This approach is still going to be slower than aMERGE
, but it's better than doing the extraSELECT
.