SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update statement question


Update statement question

Author
Message
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4777 Visits: 3232
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
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11918 Visits: 5478
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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4777 Visits: 3232
Thanks Eugene, it just looks odd to my eyes.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Bhuvnesh
Bhuvnesh
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12784 Visits: 4077
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 :-D ;-) )

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4777 Visits: 3232
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 :-D ;-) )


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
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11918 Visits: 5478
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. w00t

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!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Jason-299789
Jason-299789
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4777 Visits: 3232
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. Whistling

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
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)SSC Guru (207K reputation)

Group: General Forum Members
Points: 207428 Visits: 41961
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. w00t

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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search