When I Use NOLOCK AND OTHER HINTS

Slides:



Advertisements
Similar presentations
Sofia, Bulgaria | 9-10 October SQL Server 2005 High Availability for developers Vladimir Tchalkov Crossroad Ltd. Vladimir Tchalkov Crossroad Ltd.
Advertisements

1099 Why Use InterBase? Bill Todd The Database Group, Inc.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
1 CS 430 Database Theory Winter 2005 Lecture 16: Inside a DBMS.
Module 11 Creating Highly Concurrent SQL Server® 2008 R2 Applications.
Random Logic l Forum.NET l Transaction Isolation Levels Forum.NET Meeting ● Nov
SQL Server 2005 Engine Optimistic Concurrency Tony Rogerson, SQL Server MVP Independent Consultant 26 th.
Module 6: Data Protection. Overview What does Data Protection include? Protecting data from unauthorized users and authorized users who are trying to.
Roy Ernest Database Administrator Pinnacle Sports Worldwide
SQLintersection Understanding Transaction Isolation Levels Randy Knight Wednesday, 3:45-5:00.
Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
Does the Optimistic Concurrency resolve your blocking problems Margarita Naumova, SQL Master Academy.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Read Dirty to Me: SQL Server Isolation Levels Wendy Pastrick Arrow IT Consulting.
Session Name Pelin ATICI SQL Premier Field Engineer.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQLintersection Locks, Blocks, and Deadlocks Oh My! Randy Knight Wednesday, 2:15-3:15.
Database Transactions  Transaction Management and Concurrency Control.
In-Memory Capabilities
Query Optimization Techniques
Stored Procedures – Facts and Myths
Antonio Abalos Castillo
Weird Stuff I Saw While ... Supporting a Java Team
SQL Server Internals Overview
Let Me Finish... Isolating Write Operations
Isolation Levels Understanding Transaction Temper Tantrums
Building Modern Transaction Systems on SQL Server
Weird Stuff I Saw While ... Supporting a Java Team
Database Performance Tuning and Query Optimization
Let Me Finish... Isolating Write Operations
Let Me Finish... Isolating Write Operations
Introduction to Execution Plans
Weird Stuff I Saw While ... Supporting a Java Team
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
When I Use NOLOCK AND OTHER HINTS
Agenda Database Development – Best Practices Why Performance Matters ?
Cardinality Estimator 2014/2016
Query Optimization Techniques
Top Tips for Better TSQL Stored Procedures
Batches, Transactions, & Errors
Optimistic Concurrency Internals
Transactions, Locking and Query Optimisation
The PROCESS of Queries John Deardurff
Weird Stuff I Saw While ... Supporting a Java Team
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
Understanding Transaction Isolation Levels
Weird Stuff I Saw While … Working With Heaps
Why Is My DBA So Grumpy When I Use NOLOCK?
The PROCESS of Queries John Deardurff
Let Me Finish... Isolating Write Operations
Batches, Transactions, & Errors
Introduction to Execution Plans
Let Me Finish... Isolating Write Operations
Go, go Query Store! Gail Shaw.
When I Use NOLOCK AND OTHER HINTS
Chapter 11 Database Performance Tuning and Query Optimization
Sioux Falls, SD | Hosted by (605) SQL
Diving into Query Execution Plans
Weird Stuff I Saw While … Working With Heaps
A Masters view on Locking and blocking
Introduction to Execution Plans
Query Optimization Techniques
About Wolf DBA for over 19 years At RDX for nearly 9
Isolation Levels Understanding Transaction Temper Tantrums
Introduction to Execution Plans
Module 13: Creating Highly Concurrent SQL Server 2012 Applications
A Masters view on Locking and blocking
A Masters view on Locking and blocking
Presentation transcript:

When I Use NOLOCK AND OTHER HINTS Why Is My DBA So Grumpy? When I Use NOLOCK AND OTHER HINTS

Rick Lowe rick@data-flowe.com DataFLowe http://dataflowe.wordpress.com/

Beginning at the End Hints should be rare Rare is not the same as _never_. Most hints exist for a reason. Best added as a response to verified issues IMO, should always be accompanied by a comment

What Is NOLOCK Tells SQL Server we care about a fast response more than we care about a consistent response Specifically, suppresses the shared locks for queries Ignored by insert/update/delete statements (fortunately) NOT a “turbo button” More like “running with scissors” mode

Why NOLOCK Is Tempting READ COMMITTED is the default Readers take shared locks Writers take exclusive locks Shared and exclusive locks not compatible Many ways to block Writers block other writers Writers block readers Readers block writers

Contributing Factors Past issues cleared up with NOLOCK Belief NOLOCK means … _NO_ lock Bad code samples / misinformation on internet Lock escalation concerns Belief dirty reads are usually OK

NOLOCK Issues Usually not necessary Possible to read uncommitted data Possible to miss records as they move Possible to double-count records as they move If persisted (e.g. ETL), corruption can spread Unpredictable results when joining

Demo 1: NOLOCK

Eliminating NOLOCK If hints don’t actually prevent blocking, simply remove If blocking exists, consider optimistic concurrency (row versioning) Read Committed Snapshot Isolation (RCSI) makes row versioning the default behavior rather than blocking for the DB If RCSI seems risky, consider enabling snapshot isolation level. If snapshot is active, row versioning can be requested as needed.

ROWLOCK Mostly harmless, more annoying than problematic Sometimes seen due to concern about lock escalation In cases where lock escalation is driven by amount of memory used by locks, may delay lock escalation With modern hardware, not likely to make a difference Can force use of row locks rather than page locks If MSSQL is using page lock is there a more interesting problem? Is it a heap? Adding clustered index may be good idea anyway

RECOMPILE Legitimate uses do exist “I think this is going to be hairy” isn’t necessarily one of them When used correctly, great way to handle parameter sensitivity Side effects Interferes with plan cache Increases compilation (CPU use) Did I mention … interferes with plan cache (query history) Less problematic at query level than stored procedure level

OPTIMIZE FOR Like RECOMPILE, helps with parameter sensitivity Plan will be cached (reused), but assumptions about parameters are changed Overuse is less problematic than overuse of RECOMPILE … but parameter sniffing is usually your friend Plan is like to be OK for most values, not optimal Possible no single plan will work for every set of parameters If optimizing for specific value, data distribution may change

Thank You