Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the performance of "Merge" clause in sql server 2008?

Merge can performs insert, update, or delete operations on a target table based on the results of a join with a source table. For example, you can synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.

Is anyone familiar with the performance to use "Merge" versus the traditional logic to check existence and decide the update or insert then?

Thanks!

like image 756
zs2020 Avatar asked Mar 30 '10 20:03

zs2020


People also ask

Is MERGE faster than update in SQL Server?

The basic set-up data is as follows. We've purposely set up our source table so that the INSERTs it will do when merged with the target are interleaved with existing records for the first 500,000 rows. These indicate that MERGE took about 28% more CPU and 29% more elapsed time than the equivalent INSERT/UPDATE.

What is the purpose of MERGE in SQL Server 2008?

Starting with SQL Server 2008, you can use a MERGE statement to modify data in a target table based on data in a source table. The statement joins the target to the source by using a column common to both tables, such as a primary key.

Which is faster MERGE or insert?

"Select Insert/Update" Execution time seems better than Merge.

Which is faster MERGE or update?

The UPDATE statement will most likely be more efficient than a MERGE if the all you are doing is updating rows. Given the complex nature of the MERGE command's match condition, it can result in more overhead to process the source and target rows.


2 Answers

MERGE is generally faster because there are less DML operations, and it's also the recommended approach from the documentation. With the "traditional" way, you're processing the tables twice - once to check for existence and once to execute your DML. With MERGE, everything is encapsulated with one operation - hence one set of locks issued, one set of logging, etc. etc.

However, it is pretty subjective on what your queries are actually doing. You should probably take a look at Optimizing MERGE Statement Performance on MSDN.

like image 74
womp Avatar answered Oct 09 '22 21:10

womp


Technet has some information in Optimizing MERGE Statement Performance. It basically says that performance is better than doing individual checks, because only one pass over the data is required. However indexes etc. are of course still important.

like image 26
Anders Abel Avatar answered Oct 09 '22 19:10

Anders Abel