Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Question to you all on MERGE Expand / Collapse
Author
Message
Posted Thursday, March 07, 2013 11:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 07, 2014 2:31 PM
Points: 11, Visits: 74
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.
Post #1428169
Posted Thursday, March 07, 2013 1:08 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 9:25 AM
Points: 7,070, Visits: 12,523
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
Post #1428230
Posted Thursday, March 07, 2013 3:05 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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;


Post #1428284
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse