I have heard many time people talking about the NOLOCK hint. Some of them says it improves the performance and some of them says it reduce the blocking and deadlock as it will not acquire any locks. Let us see what is happening when we use NOCLOCK table hint in queries.
Let us see a query with out NOLOCK table hint and analyse the lock acquired by that query.
SELECT *FROM Sales.SalesOrderHeader a CROSS JOIN Sales.SalesOrderHeader b
SELECT ResourceName = CASE resource_type
WHEN 'database' THEN DB_NAME(resource_database_id)
WHEN 'object' THEN OBJECT_NAME(resource_associated_entity_id, resource_database_id)
ELSE ''
END,request_mode,
request_status, * FROM sys.dm_tran_locksWHERE request_session_id = 53
While analyzing the result, we can see a shared lock on database level. That is nothing to do with the NOLOCK hint. While opening a connection to the database, it will always take shared connection on database used by that connection to make sure that other sessions will not drop the database while in use.
BEGIN TRAN
UPDATE Sales.SalesOrderHeader SET status=5 WHERE SalesOrderID=43659
Now run the select statement and lock analyzing script. We can see that the request for Share Lock on page is in Wait status as it is conflicting with the Intent exclusive lock acquired by the update session on the page. This helps sql server to avoid the dirty read but it cause for blocking. You can see that the select statement is blocked by the update statement.
Let us see the same with NOLOCK hint
SELECT *
FROM Sales.SalesOrderHeader a WITH (NOLOCK)
CROSS JOIN Sales.SalesOrderHeader b WITH (NOLOCK)
In this case we can see only shared schema lock on the table. It is not taking a shared lock on the page and this lead to a dirty read.The shared schema lock om table level is important to make to sure that the schema of the table is not getting changed while reading the data. Let us try this select statement after the update statement and still the select statement will run without blocking as it is not trying to acquire the shared lock on pages and it also cause for the dirty read.
The READ UNCOMMITTED isolation level also works in the same way. Instead of specifying the table hint for each table we can set the isolation level to READ UNCOMMITTED. Theoretically NOLOCK hint improve the performance slightly as it need to acquires less lock compared with statement does not have a NOLOCK hint.Be careful with the usage of NOLOCK hint as it perform dirty read and may give undesired result.
Hi Nelson,
ReplyDeleteThanks for the article. I have been using the nolock hint for sometime. I wanted to use READ UNCOMMITTED isolation level and your article confirms this to be the same. Using it on an sp, do we need to reset the isolation level at the end of the sp?
Also is there a function that we can use to check if the query returned a dirty read?
Thanks,
John
Thank your for reading the article. It is a best practice to reset the isolation level at the end of procedure to avoid any surprises.I believe there is no function to check the dirty read,but the isolation level of the connection/session can be found in the DMV sys.dm_exec_sessions (transaction_isolation_level)
DeleteThanks
Nelson
I like your simple, clear and concise style! I passed this on the our Developer group, for those who may be interested in this aspect of how SQL Server works.
ReplyDeleteThank you
DeleteHi Nelson,
ReplyDeleteNice set of examples. It would be nice if you went one step further. Modify the the Select With (NoLock) by explictly begining a transaction.
The run the select first, followed by the Update.
Many people think the NoLock hint means the Select will not acquire locks and that is what reduces blocking. If you try my suggestion, I think you will see the Update query will be blocked by the Select.
Ray.Herring--NoSpam--@Hotmail.com
Thank you for reading the article. I have tried that while writing this article. What i did is, in one window I ran below query
DeleteBEGIN TRAN
SELECT *
FROM Sales.SalesOrderHeader a WITH (NOLOCK)
CROSS JOIN Sales.SalesOrderHeader b WITH (NOLOCK)
Verified the locks , it acquired only a shared schema lock on the table.
In another window I did an update on same table as below
update Sales.SalesOrderHeader set DueDate='2008-04-12 00:00:00.100' where SalesOrderID= 67258
it got executed successfully.
Ray Herring is definitely incorrect, as Nelson stated the update will be executed without any blocking.
DeleteNice, clear and to the point. READ UNCOMMITTED Isolation Level & NOLOCK hint though used for same purpose, NOLOCK do not need explicit TRANSACTIONS, where as to SET ISOLATION LEVELS you need to explicitly specify BEGIN TRAN.... END.
ReplyDeleteThank you for reading the article . No, To set the SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED , you do not have to explicitly start the transaction.
DeleteIn one session you can try
BEGIN TRAN
insert into NoLockSample values(1)
In another session you can try
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
Go
select * from NoLockSample
and you will get the expected result.
ReplyDeleteThankyou for the valuable information.
https://www.hancomfnt.com/2019/06/12/a-few-questions-you-should-ask-before-hiring-an-app-developer/
https://www.carpetcleaning-peckham.co.uk/how-to-sail-through-the-app-store/
http://www.g21studio.com/2019/06/12/mobile-business-apps-are-the-next-best-marketing-tool-for-small-businesses/
https://www.gibraltarunitedfc.com/2019/06/12/the-most-comprehensive-mobile-app/
https://www.kebab-comu.net/native-web-or-hybrid-app-points-of-difference/
Good article and knowledge for me! I found a lot of information here!
ReplyDeleteشركات جده التي تقدم خدمات نقل عفش مع الفك والتركيب تلك هي ارخص شركة نقل عفش بجدة تمتلك امكانيات كبيرة لأعمال نقل العفش في مدينة جدة وما جاورها من مناطق تابعة لها وقد نضطر قبل نقل العفش الى نظافة المنزل الجديد قبل النقل من الداخل ومن الخارج وذلك بالتواصل مع افضل شركات تنظيف منازل بجده متمكنة بأعمال التنظيف للمنازل الجديدة والمنازل المفروشة مثل ارخص شركة تنظيف مجالس بجده تقدم امكانيات غسيل الموكيت والكنب في الموقع لكي يتم تعقيم المنزل ومن الأفضل ان تقوم بعمل مكافحة للحشرات بواسطة افضل شركة مكافحه حشرات بجده التي تتعامل في مكافحة الحشرات وتستخدم مبيدات آمنة ومضمونة ونحتاج ايضا الى تنظيف الخزان وذلك بالتعرف على اكبر شركة تنظيف خزانات بجدة تقدم افضل الخدمات الجيدة في تنظيف وتعقيم الخزانات لكي تحافظ على الماء نظيفا ومعقما اطول فترة زمنية ممكنة لكي تكون مياهك نظيفة فان افضل شركة تنظيف منازل بجدة المضمونة ومستعدة لاستقبال مكالمتكم في أي وقت وطلب الخدمة من شركات تنظيف الخزانات بجدة المضمونة والتي تقدم خدمات باحترافية
ReplyDeleteخدمات التنظيف ونقل الاثاث في الرياض على مستوى لائق باهل العاصمة ونقدم لكم افضل شركة نقل عفش بالرياض مضمونة وتقدم خدمات رائعة وتستخدم سيارات نقل عفش مخصصة ومبطنة من الداخل وايضا تجد خدمات التنظيف للمنازل والفلل والشقق في ارخص شركة تنظيف فلل بالرياض تمتلك خبرة طويلة في اعمال تنظيف الشقق والفلل والقصور وجلي وتلميع جميع انواع البلاط ونقدم ايضا خدمات تنظيف المنازل بالبخار في الرياض تحت اسم اقوى شركات تنظيف كنب بالرياض آمنة بهدف الحصول على تنظيف منزلي شامل للارضيات والشبابيك والمفروشات كالكنب والمجالس والموكيت والسجاد وقد تحتاج ايضا الى تنظيف خزان المياه خاصتك وذلك بالتعاقد مع افضل شركة تنظيف خزانات بالرياض لخدمات تنظيف وتعقيم وصيانة لخزان الماء خاصتك
ReplyDeleteGood article and knowledge for me! I found a lot of information here!
ReplyDeleteAt Ashish Life Science Pvt Limited (ALS) poultry drugs distributors in ethiopia, we believe in creating values in the human-animal ecosystem. Our range of cutting-edge Veterinary products, World-class manufacturing infrastructure and relationship driven approach are the testimony to this belief. Built on this belief, we have grown manyfold since the beginning as a Veterinary pharmaceutical manufacturing company in the year 2002.
Poultry drugs distributors in Ethiopia
Thanks for the breakdown of the issues with NOLOCK. I'm bookmarking it for future reference. best seo services in gwalior
ReplyDeleteThis is a well-written piece that highlights important trends. I look forward to your future posts on this topic! Luxury Property in gwalior
ReplyDelete