Presentation is loading. Please wait.

Presentation is loading. Please wait.

When I Use NOLOCK AND OTHER HINTS

Similar presentations


Presentation on theme: "When I Use NOLOCK AND OTHER HINTS"— Presentation transcript:

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

2 Rick Lowe rick@data-flowe.com DataFLowe

3 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

4 NOLOCK Is Not Turbo This Photo by Unknown Author is licensed under CC BY-SA

5 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) More like “running with scissors” mode

6 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

7 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

8 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

9 Demo 1: NOLOCK

10 Eliminating NOLOCK Stop using NOLOCK Consider query tuning instead
To mitigate locking in general Read Committed Snapshot Isolation (RCSI) - row versioning by default. SNAPSHOT isolation – row versioning on demand

11 INDEX Hint Issues Force use of a specific index
Why isn’t optimizer choosing index already? Breaks if index dropped or disabled

12 ROWLOCK Mostly harmless, more annoying than problematic
Is concern lock escalation? Not typically effective If escalation is driven by memory, may delay escalation slightly Treating symptom rather than problem Is it a heap? Add clustered index

13 RECOMPILE Legitimate uses do exist
“I think this is going to be hairy” isn’t one Parameter sensitivity is Side effects Interferes with plan cache Increases CPU use Did I mention … interferes with plan cache Query hint better than stored procedure keyword

14 OPTIMIZE FOR Does help with parameter sensitivity Plan will be cached
Change assumptions about parameters Overuse less problematic Equivalent to saying optimizer is wrong Will parameter sniffing give bad result? Will forcing value be better? What if distribution changes?

15 Thank You


Download ppt "When I Use NOLOCK AND OTHER HINTS"

Similar presentations


Ads by Google