|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: 2 days ago @ 8:29 AM
Points: 10,
Visits: 71
|
|
So I've been reading up on Merge and have been experimenting with it lately in my 2012 environment.
I've run a few tests and was curious if aside from the readability improvements offered, if you found it useful in your production environment. Opinions welcome.
Also for your source tables, I've only tested against @temp tables so far, but did you have better performance with #tables or physical tables? In the latter two, assuming proper indexing added.
Thanks for reading.
P.S. I find MERGE incredibly strong, but dangerous during a delete if you're clause isn't accurate. I like it personally, but before I start using it in real life situations just looking for the opinions of others who've used it successfully in their day to day work environment.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 7:22 AM
Points: 6,693,
Visits: 11,707
|
|
sqlPirate_Fl (3/7/2013) So I've been reading up on Merge and have been experimenting with it lately in my 2012 environment.
I've run a few tests and was curious if aside from the readability improvements offered, if you found it useful in your production environment. Opinions welcome.
Also for your source tables, I've only tested against @temp tables so far, but did you have better performance with #tables or physical tables? In the latter two, assuming proper indexing added.
Thanks for reading. I use MERGE often. In addition to the scenario where multiple operations are useful, I also use it in place of single UPDATE...FROM/JOIN operations because it protects us from ambiguously updating the same row multiple times due to an improper JOIN clause.
P.S. I find MERGE incredibly strong, but dangerous during a delete if you're clause isn't accurate. I like it personally, but before I start using it in real life situations just looking for the opinions of others who've used it successfully in their day to day work environment. I would say that a MERGE that deletes data is no more dangerous than a DELETE...FROM/JOIN that deletes data. The risk in writing an improper JOIN clause seems to be about the same. Maybe not initially though, but once you learn and are comfortable with the MERGE syntax.
__________________________________________________________________________________________________ There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 7:58 AM
Points: 283,
Visits: 1,236
|
|
I always use MERGE now rather than several IF blocks (unless I'm stuck with SQL2005 and earlier). It's important to test the merge before using it on live data though! I have a template I use with a hot-key to drop the basic structure into a new procedure. If you are new to MERGE you may not know that the DELETE/INSERT/UPDATE statements can be in different orders and each statement will accept any additional logic you want to apply.
You can do all sorts of things like:
;MERGE INTO dbo.[TargetTable] AS [Target] USING (SELECT ID AS TargetTableID FROM dbo.[BookTable]) AS [Source] ON [Target].TargetTableID = [Source].TargetTableID
WHEN MATCHED AND [Target].isDelete = 1 THEN DELETE WHEN MATCHED AND NULLIF([Source].Title,'') IS NOT NULL THEN UPDATE SET [Target].Title = [Source].Title ,[Target].Text = [Source].Text
WHEN NOT MATCHED BY TARGET AND NULLIF([Source].Title,'') IS NOT NULL THEN INSERT ( Title ,Text ) VALUES ( [Source].Title ,[Source].Text )
WHEN NOT MATCHED BY SOURCE AND YEAR([Target].DatePublished) < 2013 THEN DELETE;
|
|
|
|