|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:10 AM
Points: 838,
Visits: 2,199
|
|
I'm doing a DB review and have always been taught to write updates with a Join like using this syntax.
Update Target Set ColList=s.ColList From Table1 Target JOIN Table2 Source on Target.NaturalKey=Source.NaturalKey where Target.ColList!=s.ColList
but I've noticed the following syntax being used in several cases
Update Target Set ColList=s.ColList From Table2 Source JOIN Table1 Target on Target.NaturalKey=Source.NaturalKey where Target.ColList!=s.ColList
Are there any difference or performance implications in using the second syntax? or are they effectively both the same?
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
Both statements will perform exactly the same update, with most likely exactly the the same performance. However, it's considered to be a good practice to drive your query from the smallest set (table). Saying above, I have only seen real performance implication in a complex multi-JOIN queries.
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:10 AM
Points: 838,
Visits: 2,199
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 7:03 AM
Points: 2,562,
Visits: 3,453
|
|
Jason-299789 (11/21/2012) Thanks Eugene, it just looks odd to my eyes. you can test it with actual execution plan . that will give you exact picture what/where/why any object is making difference (if any )
-------Bhuvnesh---------- While 1 = 1 (Learning SQL....) Click to get fast response of your post
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:10 AM
Points: 838,
Visits: 2,199
|
|
Bhuvnesh (11/21/2012)
Jason-299789 (11/21/2012) Thanks Eugene, it just looks odd to my eyes. you can test it with actual execution plan . that will give you exact picture what/where/why any object is making difference (if any  )
Its not exactly practical, as the DB is a 2 TB DW, and requires a restore after each run as its based in an offsite server in order to tune the ETL process.
It just struck me as odd that the Target table wasnt in the FROM and the Source in the join, basically it looked wierd, as I've always coded updates as per my first query.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
I'm doing a DB review and have always been taught to write updates with a JOIN like using this syntax.
UPDATE Target SET col_list = S.col_list FROM Table1 AS Target INNER JOIN Table2 AS Source ON Target.NaturalKey=Source.NaturalKey WHERE Target.col_list <> S.col_list;
but I've noticed the following syntax being used in several cases ..
You were taught a 1970's Sybase dialect and need to unlearn it. You even used the old != that we had from the Sybase/UNIX days. It has an interesting history of cardinality problems. They both suck and make you look like a hillbilly. 
Today, we have the ANSI/ISO Standard MERGE statement which has no cardinality problems and will port. Read about it. And do a bulk change of those old !=, ISNULL and other 1970's code before the other kids see your old code and make fun of you in gym class. 
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 9:40 AM
Points: 2,596,
Visits: 4,507
|
|
CELKO (11/22/2012)
I'm doing a DB review and have always been taught to write updates with a JOIN like using this syntax.
UPDATE Target SET col_list = S.col_list FROM Table1 AS Target INNER JOIN Table2 AS Source ON Target.NaturalKey=Source.NaturalKey WHERE Target.col_list <> S.col_list;
but I've noticed the following syntax being used in several cases ..
You were taught a 1970's Sybase dialect and need to unlearn it. You even used the old != that we had from the Sybase/UNIX days. It has an interesting history of cardinality problems. They both suck and make you look like a hillbilly.  Today, we have the ANSI/ISO Standard MERGE statement which has no cardinality problems and will port. Read about it. And do a bulk change of those old !=, ISNULL and other 1970's code before the other kids see your old code and make fun of you in gym class. 
Here we go again. Dear J.C. you do need to calm down. "!=" has nothing to do with 1970's Sybase and/or UNIX. Yes it was used old times, but it's still in use in modern technologies (eg. C# .NET). And I also, can tell you (keep it secret please) that will port easily into most of existing RDBM's (eg. ORACLE)
If your UPDATE with JOIN is written correctly, you very unlikely to have cardinality issues with it. Actually, MERGE is quite new thing in T-SQL and it is definitely useful. But, right now, I think it is less "portable" than "!=", for example MySql has different way to do upserts...
_____________________________________________ "The only true wisdom is in knowing you know nothing" "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!" (So many miracle inventions provided by MS to us...)
How to post your question to get the best and quick help
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Today @ 10:10 AM
Points: 838,
Visits: 2,199
|
|
No offence joe, my reason for asking was due to slivers of memories about there being a possible performance hit with the update table being in the Join rather than the from and just wanted to clarify if this was still the case.
In regards to the != Its an old colding habit from a previous life of being a c/c++ programmer. 
I do know about the merge, and it has its place, but there are occasions when it doesnt do as well as an update, horses for courses.
In regards to your other comments, meh, people are entitled to an opinion, if its worth considering I'll consider it, if not it gets thrown away with all the other junk.
_________________________________________________________________________ SSC Guide to Posting and Best Practices
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
Here we go again. Dear J.C. you do need to calm down.
At my age, always 
"!=" has nothing to do with 1970's Sybase and/or UNIX.
Wrong. When I was on the ANSI X3H2 committee, this was an issue proposed by Sybase. The SQL community rejected it. But it lives on in C, C++, C# and even in PL/I with "<frege bar>=" if anyone remembers that disaster. It was rejected because of the UNICODE Standards and conflict with other standards for the use of ! in a string.
UPDATE with JOIN is written correctly, you very unlikely to have cardinality issues with it.
I have found it to be a serious problem. Remember, I earn my living repairing SQL. A small syrem works fine inteh test environment then fails in production becaue of crap code like this.
Actually, MERGE is quite new thing in T-SQL and it is definitely useful. But, right now, I think it is less "portable" than "!=", for example MySql has different way to do upserts...
MERGE is ANSI/ISO Standard SQL and ports easily. Oh, I worked for "MySQL" and they are picky about the spelling and saying "my S Que El" in their presentations.
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 33,112,
Visits: 27,038
|
|
CELKO (11/22/2012)
I'm doing a DB review and have always been taught to write updates with a JOIN like using this syntax.
UPDATE Target SET col_list = S.col_list FROM Table1 AS Target INNER JOIN Table2 AS Source ON Target.NaturalKey=Source.NaturalKey WHERE Target.col_list <> S.col_list;
but I've noticed the following syntax being used in several cases ..
You were taught a 1970's Sybase dialect and need to unlearn it. You even used the old != that we had from the Sybase/UNIX days. It has an interesting history of cardinality problems. They both suck and make you look like a hillbilly.  Today, we have the ANSI/ISO Standard MERGE statement which has no cardinality problems and will port. Read about it. And do a bulk change of those old !=, ISNULL and other 1970's code before the other kids see your old code and make fun of you in gym class. 
No, we don't need to unlearn anything. Like anything else, UPDATE works just fine if you use it correctly. MERGE has its own set of problems, as well. And true portability is a myth, so don't worry about that either.
You also shouldn't quibble too much about 1970's code when you still use things like a push-stack While loop to do nested sets conversions. It "make[s] you look like a hillbilly [sic]".
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|