Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SQL Server. Expectation – Reality
Denis Reznik
Intapp
Email: denisreznik@gmail.com
Skype: denisreznik
Blog: http://reznik....
Agenda
• Some Useful Stuff to Remind
• Expectation - Reality 1
• Expectation - Reality 2
• Expectation - Reality 3
• Expec...
Index (B-Tree) - Seek
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K
1M-2K .. 1M
1 .. 300 301..800 801..1,5K 1,5K+1..2K
SELECT * FROM User...
Index (B-Tree) - Scan
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K
1M-2K .. 1M
1 .. 300 301..800 801..1,5K 1,5K+1..2K
SELECT * FROM Users
Statistics
500
1000
10
1200
800
1 800 2000 2800 4500 5400
SELECT * FROM Users
WHERE Id BETWEEN 2100 AND 2500
SELECT * FROM...
Exec SomeProc @p
Optimizer
Buffer Pool
Query Plan
cache
@p
Plan is cached for the
first value of Query Plan
cache
Paramete...
Lock Types - Shared
S S
X
Lock Types - Exclusive
X
X
S
Lock Types - Update
U
U
S
SX
READ COMMITTED
ID City
1 Kiev
2 London
3 London
4 London
5 London
6 New York
7 New York
ID City
1 London
2 London
3 London...
READ COMMITTED SNAPSHOT
ID City
1 Kiev
2 London
3 London
4 London
5 London
6 New York
7 New York
ID City
1 London
2 London...
READ UNCOMMITTED
ID City
1 Kiev
2 London
3 London
4 London
5 London
6 New York
7 New York
ID City
1 London
2 London
3 Lond...
DEMO: Universal Query
DEMO: Conditional Logic
DEMO: Implicit Locks
DEMO: NOLOCK Locks
DEMO: The Most Confusing
Behavior
Summary
• Universal Query
• Conditional Logic
• Implicit Locks
• NOLOCK Locks
• The Most Confusing Behavior
Thank You!
@denisreznik
denisreznik@gmail.com
http://reznik.uneta.com.ua/
https://www.facebook.com/denis.reznik.5
https://...
Upcoming SlideShare
Loading in …5
×

0

Share

Download to read offline

"Query Execution: Expectation - Reality (Level 300)" Денис Резник

Download to read offline

Yes, this is very wide problem: you expect something, but reality is a bit different.
You expect that query will run 1 sec, but it runs (Oh, my God!) 1 hour. You expect that your query will perform index seek, but it performs index scan instead. You expect your query doesn't use locks, but it uses them.
So this session will be focused on understanding the internals of such situations and making our expectations more close to reality.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

"Query Execution: Expectation - Reality (Level 300)" Денис Резник

  1. 1. SQL Server. Expectation – Reality Denis Reznik Intapp Email: denisreznik@gmail.com Skype: denisreznik Blog: http://reznik.uneta.com.ua Twitter: @denisreznik
  2. 2. Agenda • Some Useful Stuff to Remind • Expectation - Reality 1 • Expectation - Reality 2 • Expectation - Reality 3 • Expectation - Reality 4 • Expectation - Reality 5
  3. 3. Index (B-Tree) - Seek … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301..800 801..1,5K 1,5K+1..2K SELECT * FROM Users WHERE Id = 523
  4. 4. Index (B-Tree) - Scan … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301..800 801..1,5K 1,5K+1..2K SELECT * FROM Users
  5. 5. Statistics 500 1000 10 1200 800 1 800 2000 2800 4500 5400 SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500 SELECT * FROM Users WHERE Id BETWEEN 1 AND 5300
  6. 6. Exec SomeProc @p Optimizer Buffer Pool Query Plan cache @p Plan is cached for the first value of Query Plan cache Parameter Sniffing – Intro
  7. 7. Lock Types - Shared S S X
  8. 8. Lock Types - Exclusive X X S
  9. 9. Lock Types - Update U U S SX
  10. 10. READ COMMITTED ID City 1 Kiev 2 London 3 London 4 London 5 London 6 New York 7 New York ID City 1 London 2 London 3 London 4 London 5 London 6 New York 7 New York SELECT * FROM Users WHERE City = 'Kiev' BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' Wait for Shared lock on the row X S
  11. 11. READ COMMITTED SNAPSHOT ID City 1 Kiev 2 London 3 London 4 London 5 London 6 New York 7 New York ID City 1 London 2 London 3 London 4 London 5 London 6 New York 7 New York BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' X tempdb ID City 1 Kiev Version Store
  12. 12. READ UNCOMMITTED ID City 1 Kiev 2 London 3 London 4 London 5 London 6 New York 7 New York ID City 1 London 2 London 3 London 4 London 5 London 6 New York 7 New York SELECT * FROM Users WHERE City = 'Kiev' BEGIN TRAN UPDATE Users SET City = 'London' WHERE City = 'Kiev' SELECT * FROM Users WHERE City = 'Kiev' 0 RecordsROLLBACK SELECT * FROM Users WHERE City = 'Kiev' X
  13. 13. DEMO: Universal Query
  14. 14. DEMO: Conditional Logic
  15. 15. DEMO: Implicit Locks
  16. 16. DEMO: NOLOCK Locks
  17. 17. DEMO: The Most Confusing Behavior
  18. 18. Summary • Universal Query • Conditional Logic • Implicit Locks • NOLOCK Locks • The Most Confusing Behavior
  19. 19. Thank You! @denisreznik denisreznik@gmail.com http://reznik.uneta.com.ua/ https://www.facebook.com/denis.reznik.5 https://www.linkedin.com/pub/denis-reznik/3/502/234

Yes, this is very wide problem: you expect something, but reality is a bit different. You expect that query will run 1 sec, but it runs (Oh, my God!) 1 hour. You expect that your query will perform index seek, but it performs index scan instead. You expect your query doesn't use locks, but it uses them. So this session will be focused on understanding the internals of such situations and making our expectations more close to reality.

Views

Total views

526

On Slideshare

0

From embeds

0

Number of embeds

206

Actions

Downloads

6

Shares

0

Comments

0

Likes

0

×