SlideShare una empresa de Scribd logo
1 de 29
Welcome to the nightmare of
locking, blocking and isolation levels!

November 9th, 2013

#sqlsat257
#sqlsatverona
So who am I?

@BorisHristov

November 9th, 2013

#sqlsat257
#sqlsatverona
Agenda…
Locks. What is there for us?
Troubleshooting locking problems
Transaction Isolation Levels

November 9th, 2013

#sqlsat257
#sqlsatverona
Locks. What is there for us?

November 9th, 2013

#sqlsat257
#sqlsatverona
Methods of Concurrency Control
1. Pessimistic
– SQL Server uses locks, causes blocks and who said deadlocks?
2. Optimistic
– SQL Server generates versions for everyone, but the updates…

November 9th, 2013

#sqlsat257
#sqlsatverona
What Are Locks and what is locking?
Lock – internal memory structure that “tells” us what we all do with the
resources inside the system

Locking – mechanism to protect the resources and guarantee consistent data

November 9th, 2013

#sqlsat257
#sqlsatverona
Common lock types
Shared (S)

Update (U)

Used for: Reading
Duration: Released almost immediately
(depends on the isolation level)

Used for: Preparing to modify
Duration: End of the transaction or until
converted to exclusive (X)

Exclusive (X)

Intent

Used for: Modifying
Duration: End of the transaction

Used for: Preventing incompatible locks
Duration: End of the transaction

November 9th, 2013

#sqlsat257
#sqlsatverona
Lock Compatibility
Not all locks are compatible with other locks.
Lock

Shared

Update

Exclusive



X

Update
(U)




X

X

Exclusive
(X)

X

X

X

Shared
(S)

November 9th, 2013

#sqlsat257
#sqlsatverona
Lock Hierarchy
Database

Table

Page

Row

November 9th, 2013

#sqlsat257
#sqlsatverona
Let’s update a row!
What do we need?

S

A query! 

USE AdventureWorks2012
GO
UPDATE [Person].[Address]
SET AddressLine1=’Verona,Italy'
WHERE AddressID=2

IX

IX

Header
Row

Row
Row
Row

X

Row

November 9th, 2013

#sqlsat257
#sqlsatverona
Methods to View Locking Information

Dynamic
Management
Views

November 9th, 2013

SQL Server
Profiler or
Extended
Events

Performance
monitor or Activity
Monitor

#sqlsat257
#sqlsatverona
Troubleshooting locking problems

November 9th, 2013

#sqlsat257
#sqlsatverona
Locking and blocking
Locking and blocking are often confused!

Locking
• The action of taking and potentially holding locks
• Used to implement concurrency control

Blocking is result of locking!
• One process needs to wait for another process to release locked
resources
• In a multiuser environment, there is always, always blocking!
• Only a problem if it lasts too long

November 9th, 2013

#sqlsat257
#sqlsatverona
Lock granularity and escalation
SQL Server decides (during compilation) the granularity of locks to be used:
•
•

Row
Page

Lock granularity can be controlled via LOCK HINTS (PAGLOCK, TABLOCKX…)
Escalation always happens this way:
Row -> table lock (or partition lock if possible)
Page -> table lock (or partition lock if possible)
Lock escalation can be disabled:
•
•

Trace flag 1211 – disables lock escalation on server level
Trace flag 1224 – disables lock escalation on server level until 40% of the
memory used is consumed

November 9th, 2013

#sqlsat257
#sqlsatverona
Lock escalation
S

S
IX

X
IX

Header
Row
Row

X
X

Row
Row

X

Row

>= 5000

X

November 9th, 2013

#sqlsat257
#sqlsatverona
Controlling Lock escalation
Switch the escalation level (per table)
SELECT lock_escalation_desc
FROM sys.tables
WHERE name = 'Person.Address'
ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO |
TABLE | DISABLE}
AUTO – Partition-level escalation if the table is partitioned
TABLE – Always table-level escalation
DISABLE – Do not escalate until absolutely necessary

November 9th, 2013

#sqlsat257
#sqlsatverona
What Are Deadlocks?
Who is victim?
• Cost for Rollback
• Deadlock priority – SET DEADLOCK_PRIOIRTY

Task A

Task B

November 9th, 2013

Resource 1

Resource 2

#sqlsat257
#sqlsatverona
Resolve blocking a.k.a live locking
1. Keep the transactions as short as possible

2. No user interactions required in the middle of the transaction
3. Use indexes (proper ones)
4. Consider a server to offload some of the workloads

5. Choose isolation level

November 9th, 2013

#sqlsat257
#sqlsatverona
DEMO
Monitor for locks with xEvents
Lock escalation – both to table and partition

Deadlock and the SET DEADLOCK_PRIORITY option

November 9th, 2013

#sqlsat257
#sqlsatverona
Transaction isolation levels

November 9th, 2013

#sqlsat257
#sqlsatverona
Read Uncommitted
(pessimistic concurrency control)

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?)

Transaction 1

eXclusive lock

Update
Select

Transaction 2

Dirty read

Suggestion: Better offload the reads or go with optimistic level concurrency!

November 9th, 2013

#sqlsat257
#sqlsatverona
Repeatable Read
(pessimistic concurrency control)

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Transaction 1

S(hared) lock

select

Transaction 2

Update

No non-repeatable reads possible (updates during Transaction 1)
Phantom records still possible (inserts during Transaction 1)

November 9th, 2013

#sqlsat257
#sqlsatverona
Serializable
(pessimistic concurrency control)

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

Transaction 1

S(hared) lock

select

Transaction 2

Insert

Even phantom records are not possible!
Highest pessimistic level of isolation, lowest level of concurrency

November 9th, 2013

#sqlsat257
#sqlsatverona
Optimistic Concurrency
Based on Row versioning (stored inside tempdb’s version store area)
• No dirty, non-repeatable reads or phantom records

• Every single modification is versioned even if not used
• Adds 14 bytes per row

Readers do not block writers and writers do not block readers
Writers can and will block writers, this can cause conflicts

November 9th, 2013

#sqlsat257
#sqlsatverona
RCSI and SI
(optimistic concurrency control)

V1

Transaction 1
Select

Transaction 2

V2

Select in RCSI

Select in SI

RCSI – Read Committed Snapshot Isolation Level
•

Statement level versioning

•

Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON

Snapshot Isolation Level
•

Transaction level versioning

•

Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON

•

Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT

November 9th, 2013

#sqlsat257
#sqlsatverona
DEMO
Playing around with the Isolation levels

November 9th, 2013

#sqlsat257
#sqlsatverona
Summary
1. Blocking is something normal when it’s not for long
2. There are numerous of ways to monitor locking and blocking

3. Be extremely careful for lock escalations
4. Choosing the Isolation level is also a business decision!

November 9th, 2013

#sqlsat257
#sqlsatverona
Resources
MCM Readiness videos on locking lecture and demo
MCM Readiness video on Snapshot Isolation Level
http://blogs.msdn.com/b/bartd/archive/tags/sql+locking

http://www.sqlskills.com/blogs/paul/category/locking/
Lock hints http://www.techrepublic.com/article/control-sql-serverlocking-with-hints/5181472

November 9th, 2013

#sqlsat257
#sqlsatverona
Thank you.

November 9th, 2013

#sqlsat257
#sqlsatverona

Más contenido relacionado

La actualidad más candente

Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Boris Hristov
 
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!Boris Hristov
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!Boris Hristov
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Boris Hristov
 
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!Boris Hristov
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Boris Hristov
 
Deep Into Isolation Levels
Deep Into Isolation LevelsDeep Into Isolation Levels
Deep Into Isolation LevelsBoris Hristov
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyBoris Hristov
 
The Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation LevelsThe Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation LevelsBoris Hristov
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction ManagementMark Ginnebaugh
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale:  an Intelligent Database ProxyMariaDB MaxScale:  an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMarkus Mäkelä
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Boris Hristov
 
Buytaert kris my_sql-pacemaker
Buytaert kris my_sql-pacemakerBuytaert kris my_sql-pacemaker
Buytaert kris my_sql-pacemakerkuchinskaya
 
Dynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value StoreDynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value StoreMiro Cupak
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Antonios Chatzipavlis
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMarkus Mäkelä
 
Distributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere MortalsDistributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere MortalsEnsar Basri Kahveci
 
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017Ensar Basri Kahveci
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 

La actualidad más candente (20)

Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one!
 
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!
 
The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!The nightmare of locking, blocking and isolation levels!
The nightmare of locking, blocking and isolation levels!
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one!
 
Deep Into Isolation Levels
Deep Into Isolation LevelsDeep Into Isolation Levels
Deep Into Isolation Levels
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
The Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation LevelsThe Nightmare of Locking, Blocking and Isolation Levels
The Nightmare of Locking, Blocking and Isolation Levels
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale:  an Intelligent Database ProxyMariaDB MaxScale:  an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
Replay your workload as it is your actual one!
Replay your workload as it is your actual one! Replay your workload as it is your actual one!
Replay your workload as it is your actual one!
 
Buytaert kris my_sql-pacemaker
Buytaert kris my_sql-pacemakerBuytaert kris my_sql-pacemaker
Buytaert kris my_sql-pacemaker
 
Dynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value StoreDynamo Amazon’s Highly Available Key-value Store
Dynamo Amazon’s Highly Available Key-value Store
 
Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)Concurrency in SQL Server (SQL Night #24)
Concurrency in SQL Server (SQL Night #24)
 
MariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database ProxyMariaDB MaxScale: an Intelligent Database Proxy
MariaDB MaxScale: an Intelligent Database Proxy
 
Locking And Concurrency
Locking And ConcurrencyLocking And Concurrency
Locking And Concurrency
 
Distributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere MortalsDistributed Systems Theory for Mere Mortals
Distributed Systems Theory for Mere Mortals
 
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017Distributed Systems Theory for Mere Mortals -  GeeCON Krakow May 2017
Distributed Systems Theory for Mere Mortals - GeeCON Krakow May 2017
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 

Similar a The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

1.8 Data Protection.pdf
1.8 Data Protection.pdf1.8 Data Protection.pdf
1.8 Data Protection.pdfssuser8b6c85
 
Database security
Database securityDatabase security
Database securityJaved Khan
 
Always on in sql server 2017
Always on in sql server 2017Always on in sql server 2017
Always on in sql server 2017Gianluca Hotz
 
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
MySQL Webinar 2/4 Performance tuning, hardware, optimisationMySQL Webinar 2/4 Performance tuning, hardware, optimisation
MySQL Webinar 2/4 Performance tuning, hardware, optimisationMark Swarbrick
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsTuyen Vuong
 
Introduction to XtraDB Cluster
Introduction to XtraDB ClusterIntroduction to XtraDB Cluster
Introduction to XtraDB Clusteryoku0825
 
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and LockingGeek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and LockingIDERA Software
 
M|18 How Facebook Migrated to MyRocks
M|18 How Facebook Migrated to MyRocksM|18 How Facebook Migrated to MyRocks
M|18 How Facebook Migrated to MyRocksMariaDB plc
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsAlexey Furmanov
 
Using Release(deallocate) and Painful Lessons to be learned on DB2 locking
Using Release(deallocate) and Painful Lessons to be learned on DB2 lockingUsing Release(deallocate) and Painful Lessons to be learned on DB2 locking
Using Release(deallocate) and Painful Lessons to be learned on DB2 lockingJohn Campbell
 
When is MyRocks good?
When is MyRocks good? When is MyRocks good?
When is MyRocks good? Alkin Tezuysal
 
Migrating to XtraDB Cluster
Migrating to XtraDB ClusterMigrating to XtraDB Cluster
Migrating to XtraDB Clusterpercona2013
 
SAOUG - Connect 2014 - Flex Cluster and Flex ASM
SAOUG - Connect 2014 - Flex Cluster and Flex ASMSAOUG - Connect 2014 - Flex Cluster and Flex ASM
SAOUG - Connect 2014 - Flex Cluster and Flex ASMAlex Zaballa
 
2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma
2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma
2014.11.22 Azure for Sql Server Developer - SQLSAT355 ParmaMarco Parenzan
 
Encryption oracle
Encryption oracleEncryption oracle
Encryption oraclemanong007
 
Oracle Database on ACFS: a perfect marriage?
Oracle Database on ACFS: a perfect marriage?Oracle Database on ACFS: a perfect marriage?
Oracle Database on ACFS: a perfect marriage?Ludovico Caldara
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Oliersqlserver.co.il
 

Similar a The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona (20)

1.8 Data Protection.pdf
1.8 Data Protection.pdf1.8 Data Protection.pdf
1.8 Data Protection.pdf
 
Database security
Database securityDatabase security
Database security
 
Alwayson in sqlserver2017
Alwayson in sqlserver2017Alwayson in sqlserver2017
Alwayson in sqlserver2017
 
Always on in sql server 2017
Always on in sql server 2017Always on in sql server 2017
Always on in sql server 2017
 
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
MySQL Webinar 2/4 Performance tuning, hardware, optimisationMySQL Webinar 2/4 Performance tuning, hardware, optimisation
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
 
MySQL Atchitecture and Concepts
MySQL Atchitecture and ConceptsMySQL Atchitecture and Concepts
MySQL Atchitecture and Concepts
 
Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
Introduction to XtraDB Cluster
Introduction to XtraDB ClusterIntroduction to XtraDB Cluster
Introduction to XtraDB Cluster
 
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and LockingGeek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
Geek Sync | How to Detect, Analyze, and Minimize SQL Server Blocking and Locking
 
M|18 How Facebook Migrated to MyRocks
M|18 How Facebook Migrated to MyRocksM|18 How Facebook Migrated to MyRocks
M|18 How Facebook Migrated to MyRocks
 
Lecture 5. MS SQL. Transactions
Lecture 5. MS SQL. TransactionsLecture 5. MS SQL. Transactions
Lecture 5. MS SQL. Transactions
 
Using Release(deallocate) and Painful Lessons to be learned on DB2 locking
Using Release(deallocate) and Painful Lessons to be learned on DB2 lockingUsing Release(deallocate) and Painful Lessons to be learned on DB2 locking
Using Release(deallocate) and Painful Lessons to be learned on DB2 locking
 
When is MyRocks good?
When is MyRocks good? When is MyRocks good?
When is MyRocks good?
 
DBCC - Dubi Lebel
DBCC - Dubi LebelDBCC - Dubi Lebel
DBCC - Dubi Lebel
 
Migrating to XtraDB Cluster
Migrating to XtraDB ClusterMigrating to XtraDB Cluster
Migrating to XtraDB Cluster
 
SAOUG - Connect 2014 - Flex Cluster and Flex ASM
SAOUG - Connect 2014 - Flex Cluster and Flex ASMSAOUG - Connect 2014 - Flex Cluster and Flex ASM
SAOUG - Connect 2014 - Flex Cluster and Flex ASM
 
2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma
2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma
2014.11.22 Azure for Sql Server Developer - SQLSAT355 Parma
 
Encryption oracle
Encryption oracleEncryption oracle
Encryption oracle
 
Oracle Database on ACFS: a perfect marriage?
Oracle Database on ACFS: a perfect marriage?Oracle Database on ACFS: a perfect marriage?
Oracle Database on ACFS: a perfect marriage?
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 

Más de Boris Hristov

The Secret to Engaging Presentations
The Secret to Engaging PresentationsThe Secret to Engaging Presentations
The Secret to Engaging PresentationsBoris Hristov
 
Presentation Design Fundamentals
Presentation Design FundamentalsPresentation Design Fundamentals
Presentation Design FundamentalsBoris Hristov
 
The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business IntelligenceBoris Hristov
 
The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014Boris Hristov
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Boris Hristov
 
How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!Boris Hristov
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Boris Hristov
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Boris Hristov
 
Presentation Skills: The Next Level
Presentation Skills: The Next LevelPresentation Skills: The Next Level
Presentation Skills: The Next LevelBoris Hristov
 
SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!Boris Hristov
 
BI PoC for the Telco Industry
BI PoC for the Telco IndustryBI PoC for the Telco Industry
BI PoC for the Telco IndustryBoris Hristov
 
Presentation Design Basics
Presentation Design BasicsPresentation Design Basics
Presentation Design BasicsBoris Hristov
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Boris Hristov
 
Database Performance
Database PerformanceDatabase Performance
Database PerformanceBoris Hristov
 
You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!Boris Hristov
 
First Steps with Microsoft SQL Server
First Steps with Microsoft SQL ServerFirst Steps with Microsoft SQL Server
First Steps with Microsoft SQL ServerBoris Hristov
 
Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014Boris Hristov
 

Más de Boris Hristov (17)

The Secret to Engaging Presentations
The Secret to Engaging PresentationsThe Secret to Engaging Presentations
The Secret to Engaging Presentations
 
Presentation Design Fundamentals
Presentation Design FundamentalsPresentation Design Fundamentals
Presentation Design Fundamentals
 
The World of Business Intelligence
The World of Business IntelligenceThe World of Business Intelligence
The World of Business Intelligence
 
The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014The 5 Hidden Performance Gems of SQL Server 2014
The 5 Hidden Performance Gems of SQL Server 2014
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?
 
How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!How to Deliver Technical Presentations: The Right Way!
How to Deliver Technical Presentations: The Right Way!
 
Securing SQL Azure DB? How?
Securing SQL Azure DB? How?Securing SQL Azure DB? How?
Securing SQL Azure DB? How?
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014
 
Presentation Skills: The Next Level
Presentation Skills: The Next LevelPresentation Skills: The Next Level
Presentation Skills: The Next Level
 
SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!SQL Server 2014: Ready. Steady. Go!
SQL Server 2014: Ready. Steady. Go!
 
BI PoC for the Telco Industry
BI PoC for the Telco IndustryBI PoC for the Telco Industry
BI PoC for the Telco Industry
 
Presentation Design Basics
Presentation Design BasicsPresentation Design Basics
Presentation Design Basics
 
Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014Top 5 T-SQL Improvements in SQL Server 2014
Top 5 T-SQL Improvements in SQL Server 2014
 
Database Performance
Database PerformanceDatabase Performance
Database Performance
 
You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!You want rules? You need Policy-Based Management!
You want rules? You need Policy-Based Management!
 
First Steps with Microsoft SQL Server
First Steps with Microsoft SQL ServerFirst Steps with Microsoft SQL Server
First Steps with Microsoft SQL Server
 
Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014Top 5 TSQL Improvements in SQL Server 2014
Top 5 TSQL Improvements in SQL Server 2014
 

Último

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 

Último (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 

The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

  • 1. Welcome to the nightmare of locking, blocking and isolation levels! November 9th, 2013 #sqlsat257 #sqlsatverona
  • 2. So who am I? @BorisHristov November 9th, 2013 #sqlsat257 #sqlsatverona
  • 3. Agenda… Locks. What is there for us? Troubleshooting locking problems Transaction Isolation Levels November 9th, 2013 #sqlsat257 #sqlsatverona
  • 4. Locks. What is there for us? November 9th, 2013 #sqlsat257 #sqlsatverona
  • 5. Methods of Concurrency Control 1. Pessimistic – SQL Server uses locks, causes blocks and who said deadlocks? 2. Optimistic – SQL Server generates versions for everyone, but the updates… November 9th, 2013 #sqlsat257 #sqlsatverona
  • 6. What Are Locks and what is locking? Lock – internal memory structure that “tells” us what we all do with the resources inside the system Locking – mechanism to protect the resources and guarantee consistent data November 9th, 2013 #sqlsat257 #sqlsatverona
  • 7. Common lock types Shared (S) Update (U) Used for: Reading Duration: Released almost immediately (depends on the isolation level) Used for: Preparing to modify Duration: End of the transaction or until converted to exclusive (X) Exclusive (X) Intent Used for: Modifying Duration: End of the transaction Used for: Preventing incompatible locks Duration: End of the transaction November 9th, 2013 #sqlsat257 #sqlsatverona
  • 8. Lock Compatibility Not all locks are compatible with other locks. Lock Shared Update Exclusive  X Update (U)   X X Exclusive (X) X X X Shared (S) November 9th, 2013 #sqlsat257 #sqlsatverona
  • 10. Let’s update a row! What do we need? S A query!  USE AdventureWorks2012 GO UPDATE [Person].[Address] SET AddressLine1=’Verona,Italy' WHERE AddressID=2 IX IX Header Row Row Row Row X Row November 9th, 2013 #sqlsat257 #sqlsatverona
  • 11. Methods to View Locking Information Dynamic Management Views November 9th, 2013 SQL Server Profiler or Extended Events Performance monitor or Activity Monitor #sqlsat257 #sqlsatverona
  • 12. Troubleshooting locking problems November 9th, 2013 #sqlsat257 #sqlsatverona
  • 13. Locking and blocking Locking and blocking are often confused! Locking • The action of taking and potentially holding locks • Used to implement concurrency control Blocking is result of locking! • One process needs to wait for another process to release locked resources • In a multiuser environment, there is always, always blocking! • Only a problem if it lasts too long November 9th, 2013 #sqlsat257 #sqlsatverona
  • 14. Lock granularity and escalation SQL Server decides (during compilation) the granularity of locks to be used: • • Row Page Lock granularity can be controlled via LOCK HINTS (PAGLOCK, TABLOCKX…) Escalation always happens this way: Row -> table lock (or partition lock if possible) Page -> table lock (or partition lock if possible) Lock escalation can be disabled: • • Trace flag 1211 – disables lock escalation on server level Trace flag 1224 – disables lock escalation on server level until 40% of the memory used is consumed November 9th, 2013 #sqlsat257 #sqlsatverona
  • 16. Controlling Lock escalation Switch the escalation level (per table) SELECT lock_escalation_desc FROM sys.tables WHERE name = 'Person.Address' ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE} AUTO – Partition-level escalation if the table is partitioned TABLE – Always table-level escalation DISABLE – Do not escalate until absolutely necessary November 9th, 2013 #sqlsat257 #sqlsatverona
  • 17. What Are Deadlocks? Who is victim? • Cost for Rollback • Deadlock priority – SET DEADLOCK_PRIOIRTY Task A Task B November 9th, 2013 Resource 1 Resource 2 #sqlsat257 #sqlsatverona
  • 18. Resolve blocking a.k.a live locking 1. Keep the transactions as short as possible 2. No user interactions required in the middle of the transaction 3. Use indexes (proper ones) 4. Consider a server to offload some of the workloads 5. Choose isolation level November 9th, 2013 #sqlsat257 #sqlsatverona
  • 19. DEMO Monitor for locks with xEvents Lock escalation – both to table and partition Deadlock and the SET DEADLOCK_PRIORITY option November 9th, 2013 #sqlsat257 #sqlsatverona
  • 20. Transaction isolation levels November 9th, 2013 #sqlsat257 #sqlsatverona
  • 21. Read Uncommitted (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?) Transaction 1 eXclusive lock Update Select Transaction 2 Dirty read Suggestion: Better offload the reads or go with optimistic level concurrency! November 9th, 2013 #sqlsat257 #sqlsatverona
  • 22. Repeatable Read (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL REPEATABLE READ Transaction 1 S(hared) lock select Transaction 2 Update No non-repeatable reads possible (updates during Transaction 1) Phantom records still possible (inserts during Transaction 1) November 9th, 2013 #sqlsat257 #sqlsatverona
  • 23. Serializable (pessimistic concurrency control) SET TRANSACTION ISOLATION LEVEL SERIALIZABLE Transaction 1 S(hared) lock select Transaction 2 Insert Even phantom records are not possible! Highest pessimistic level of isolation, lowest level of concurrency November 9th, 2013 #sqlsat257 #sqlsatverona
  • 24. Optimistic Concurrency Based on Row versioning (stored inside tempdb’s version store area) • No dirty, non-repeatable reads or phantom records • Every single modification is versioned even if not used • Adds 14 bytes per row Readers do not block writers and writers do not block readers Writers can and will block writers, this can cause conflicts November 9th, 2013 #sqlsat257 #sqlsatverona
  • 25. RCSI and SI (optimistic concurrency control) V1 Transaction 1 Select Transaction 2 V2 Select in RCSI Select in SI RCSI – Read Committed Snapshot Isolation Level • Statement level versioning • Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON Snapshot Isolation Level • Transaction level versioning • Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON • Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT November 9th, 2013 #sqlsat257 #sqlsatverona
  • 26. DEMO Playing around with the Isolation levels November 9th, 2013 #sqlsat257 #sqlsatverona
  • 27. Summary 1. Blocking is something normal when it’s not for long 2. There are numerous of ways to monitor locking and blocking 3. Be extremely careful for lock escalations 4. Choosing the Isolation level is also a business decision! November 9th, 2013 #sqlsat257 #sqlsatverona
  • 28. Resources MCM Readiness videos on locking lecture and demo MCM Readiness video on Snapshot Isolation Level http://blogs.msdn.com/b/bartd/archive/tags/sql+locking http://www.sqlskills.com/blogs/paul/category/locking/ Lock hints http://www.techrepublic.com/article/control-sql-serverlocking-with-hints/5181472 November 9th, 2013 #sqlsat257 #sqlsatverona
  • 29. Thank you. November 9th, 2013 #sqlsat257 #sqlsatverona

Notas del editor

  1. Stop ClipX, Start ZoomIt, Profiler, Extended Events Session - Live Bonjourno! - Today we’re going to talk about a problem that you already had or you are about to face very soon! We are all busy, but understand that isolation levels are important. How many of you had the time to play with all of them?Very demo intensive
  2. Senior DBA at HP Enterprise Services in Bulgaria, working for Fortune 500Regular speaker at conferences / webcasts / podcastsTrainer for external companiesTeacher at the 2 biggest universities in BulgariaVery active on twitter / blogging / hangoutsMore on LinkedInEasyest way to reach me – twitterA lot of SQL Server in my world
  3. How SQL Server controls the access between your users to the resources inside the databaseIn pessimistic you are worried that someone will modify the data you work with and that’s why SQL helps with locks. Thus ensuring the consistency of the data between the usersIn Optimistic you have your own version of the data and quite less locks, but you hit tempdb and some update conflicts which we will talk later for
  4. Once we know the concurrency levels and that they are using locks to guarantee data consistency what are locks?Locks – Am I reading, Am I writing?Locks – up to 60% of the buffer poolEach is taking around 100 bytes per lock (32 bit – 64, 64 bit – 128)How much memory is used – locks on server level (decommissioned) – 2500 or 2% of the memory
  5. Shared – reading / depends how long will be held and when will be releasedExclusive – modify / always till the end – why? Example with rollback!Update – preparing to modify – while searching which rows needs to be updated it puts update lock. Always convert to exclusiveIntent lock – used for letting now the Lock Manager that someone works with the DB in some way in the higher level in the hierarchyAnd there are a lot more locks – schema locks, Bulk update locks and more, but we definitely do not have time for that! (combinations too)
  6. Not all are compatible with one another and that’s why we wait sometimes (there’s blocking)Update is friendly enough with the shared – it will not make sense otherwiseExclusive – not that muchMore on the linkSIX - convert lock. Example – shared lock on a row now has to become X lock. However, for the exclusive to occur, we ed IX in the upper levels – that is the moment when we convert our S and IS locks to X and SIX locks
  7. Every transaction you issue has to put locks on all of the levels Why? Because otherwise SQL Server and Lock manager will not be aware what’s going on in this databaseWhat will stop me, what will tell me that someone is modifying a row if I don’t place a locks above in the hierarchy?Shared lock always on the database – tell the exclusions – mater and tempdb
  8. Various, various ways… sp_whoisactiveDynamic Management ViewsSystem stored proceduresSQL ProfilerPerformance Monitor countersBlocked Process ReportDeadlock events and trace flag 1222SQL Server 2008 addsPerformance Data CollectionExtended Events
  9. LOCK_TIMEOUT -> next statement continues!!! Session level option! Throws 1222(READPAST) hint – reads what it is able to and moves forward
  10. Not exactly – it always decides during compilation ROW or PAGE level – tow options only (when standard DML operations, not altering object)After that it could escalate to partition/table levelDo not forget to mention what is escalation before explaining what are the levels
  11. - Or 5000 locks on one object / index / table scan!If escalation fails because of incompatible lock – sql server waits for another 1250 locks (partitioning) – auto to partition from 2008 + enterpriseEscalating in case of 40% of the memory used for locks if the locks option is not set to 0Escalating in case of 24% of the memory used for locks if the locks option is set to 0
  12. Disable – in case of serialization isolation level and table scan – table lock will be acquiredNot the way to resolve your problems, just a workaround!Auto not default in partitioning because can cause deadlocks
  13. Priorities -> cost -> randomLOCK_MONITOR checks every 5 seconds for deadlock and every 100ms if one is detected till no others are detectedSome operations cannot be chosen for deadlock victims (rollbacks) Error 1205 should be handled, 1204 and 1222 Cycle and conversion deadlocks
  14. Cursors – when it takes lock and on what and when it releases them
  15. - Going down the concurrency first – seeing data that is not committed- Dirty read – when you read uncommitted data
  16. - Go higher – more locks! that’s the prize- No nonrepeatable reads- S lock is held until the end of the transaction in order to read the same data- Every transaction that has to update rows has to wait until T1 completes
  17. - Go higher - Repeatable Read + Holding the locks for a zone of rows- + 1 and – 1 in the range, but you have to have a good index for this to happen otherwise table scan! - Cannot insert more data in the rangeEntity Framework and LYNC – EF does it automatically without you stating it
  18. Old version goes to tempdb – per row 14 bytes for the new writes only (not KB, bytes!)- consist the XSN of the transation that generated the version + the pointer to the old version in tempdbCommitted versions go into databaseEvery single modification is versioned! (with the exception of inserts)AlwaysOn readable secondary turns on Snapshot isolation level
  19. Statement level versionsRCSI – Read Committed, but with versionsNo code changes, but you have to throw everyone from the dbSI – Repeatable read – a lot more tempdb usageTransaction level versionsRequires code changeCan be used together Only SI ON – using still locks, but with versions generated you have to REQUEST it with Set transaction…