Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

SQL locking in an UPDATE...FROM statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Goran Djuranovic - 24 Jul 2008 17:01 GMT
Hi all,
In an UPDATE...FROM statement, how do I instruct SQL server to specifically
lock only rows where the PK matches AND the value to be updated is
different. It looks like it puts an update lock on TblOne whether the values
differ or match.

For example:
UPDATE TblOne SET
   TblOne.Value = TblTwo.Value
FROM TableOne TblOne
   INNER JOIN TableTwo TblTwo ON
       TblTwo.UniqueID = TblOne.UniqueID AND
       TblTwo.Value <> TblOne.Value

Am I missing something?

Thanks
Goran Djuranovic
--CELKO-- - 24 Jul 2008 19:31 GMT
Use Standard SQL instead of dialect, and you will also catch
cardinality problems.

UPDATE TblOne
  SET TblOne.value
      = (SELECT TblTwo.value
       FROM TblTwo
      WHERE TblTwo.unique_id = TblOne.unique_id
        AND TblTwo.value <> TblOne.value)
WHERE EXISTS
      (SELECT *
     FROM TblTwo
        WHERE TblTwo.unique_id = TblOne.unique_id
      AND TblTwo.value <> TblOne.value);
Andrew J. Kelly - 24 Jul 2008 20:06 GMT
You can ignore Joe on this one since the Update lock will be there
regardless of how you do it. It is there to prevent someone from taking a
table level lock out when you have a row or page in that same table.  And if
you don't have a proper index it may attempt a table level lock to begin
with.

Signature

Andrew J. Kelly    SQL MVP
Solid Quality Mentors

> Use Standard SQL instead of dialect, and you will also catch
> cardinality problems.
[quoted text clipped - 10 lines]
>         WHERE TblTwo.unique_id = TblOne.unique_id
>    AND TblTwo.value <> TblOne.value);
Alex Kuznetsov - 25 Jul 2008 15:33 GMT
> Use Standard SQL instead of dialect, and you will also catch
> cardinality problems.
[quoted text clipped - 10 lines]
>          WHERE TblTwo.unique_id = TblOne.unique_id
>            AND TblTwo.value <> TblOne.value);

Because it usually runs about two times slower, most likely it will
make the matters much worse.
Charles Wang [MSFT] - 25 Jul 2008 04:36 GMT
Hi Goran,
Your UPDATE statement might lead to a full table scan, therefore a table lock was acquired in
this case. Also there might be Lock escalation happened. However it is hard to avoid a table
lock for such a scenario. If there are a large number of rows, it also does not make sense to
lock each row since it is a huge cost for SQL Server to maintain so many row locks.

Anyway I would like to recommend the following KB article for your reference. You may check
if it could give you some ideas to optimize your query:
How to resolve blocking problems that are caused by lock escalation in SQL Server
http://support.microsoft.com/kb/323630/en-us
Lock Escalation (Database Engine)
http://msdn.microsoft.com/en-us/library/ms184286.aspx

If you have any other questions or concerns, please feel free to let us know.

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@microsoft.com.
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
Signature

This posting is provided "AS IS" with no warranties, and confers no rights.

=========================================================
Charles Wang [MSFT] - 25 Jul 2008 04:36 GMT
Hi Goran,
Your UPDATE statement might lead to a full table scan, therefore a table lock was acquired in
this case. Also there might be Lock escalation happened. However it is hard to avoid a table
lock for such a scenario. If there are a large number of rows, it also does not make sense to
lock each row since it is a huge cost for SQL Server to maintain so many row locks.

Anyway I would like to recommend the following KB article for your reference. You may check
if it could give you some ideas to optimize your query:
How to resolve blocking problems that are caused by lock escalation in SQL Server
http://support.microsoft.com/kb/323630/en-us
Lock Escalation (Database Engine)
http://msdn.microsoft.com/en-us/library/ms184286.aspx

If you have any other questions or concerns, please feel free to let us know.

Best regards,
Charles Wang
Microsoft Online Community Support
===========================================================
Delighting our customers is our #1 priority. We welcome your
comments and suggestions about how we can improve the
support we provide to you. Please feel free to let my manager
know what you think of the level of service provided. You can
send feedback directly to my manager at: msdnmg@microsoft.com.
===========================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for
non-urgent issues where an initial response from the community
or a Microsoft Support Engineer within 1 business day is acceptable.
Please note that each follow up response may take approximately
2 business days as the support professional working with you may
need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by
contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
============================================================
Signature

This posting is provided "AS IS" with no warranties, and confers no rights.

=========================================================
Goran Djuranovic - 30 Jul 2008 14:40 GMT
Thx guys. That was very helpful.
Goran

> Hi all,
> In an UPDATE...FROM statement, how do I instruct SQL server to
[quoted text clipped - 14 lines]
> Thanks
> Goran Djuranovic
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.