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 1234»»»

Update statement question Expand / Collapse
Author
Message
Posted Wednesday, November 21, 2012 3:32 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:25 AM
Points: 918, Visits: 2,507
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
Post #1387328
Posted Wednesday, November 21, 2012 4:05 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 3:45 AM
Points: 2,873, Visits: 5,188
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
Post #1387337
Posted Wednesday, November 21, 2012 4:32 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:25 AM
Points: 918, Visits: 2,507
Thanks Eugene, it just looks odd to my eyes.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1387353
Posted Wednesday, November 21, 2012 4:39 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 5:03 AM
Points: 2,840, Visits: 3,975
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----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1387360
Posted Wednesday, November 21, 2012 8:58 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:25 AM
Points: 918, Visits: 2,507
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
Post #1387466
Posted Thursday, November 22, 2012 7:18 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 12:29 PM
Points: 1,945, Visits: 3,121
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
Post #1387885
Posted Thursday, November 22, 2012 7:55 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 3:45 AM
Points: 2,873, Visits: 5,188
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
Post #1387908
Posted Thursday, November 22, 2012 8:14 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:25 AM
Points: 918, Visits: 2,507
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
Post #1387918
Posted Thursday, November 22, 2012 7:04 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 12:29 PM
Points: 1,945, Visits: 3,121
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
Post #1388002
Posted Thursday, November 22, 2012 10:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 1:41 PM
Points: 35,538, Visits: 32,127
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1388030
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse