Id Parameter Sniffing - Parametrized Query

SQLDay 2017
Query Execution. Expectation – Reality.
SQLDay 2017
Denis Reznik
Data Architect, Intapp, Inc.
Microsoft Data Platform MVP
About Me
•
•
•
•
•
•
•
Denis Reznik
Kyiv, Ukraine
Data Architect at Intapp, Inc.
Microsoft Data Platform MVP
PASS Regional Mentor, CEE
Ukrainian Data Community Kyiv Co-Founder
Co-author of “SQL Server MVP Deep Dives vol. 2”
SQLDay 2017
Agenda
•
•
•
•
•
Expectation - Reality 1
Expectation - Reality 2
Expectation - Reality 3
Expectation - Reality 4
Expectation - Reality 5
SQLDay 2017
SWISS ARMY KNIFE QUERY
“Universal* queries work universally bad.”
(c) Dmitry Kostylev, Microsoft Data Platform MVP, Russia
* Universal = Multipurpose
SQLDay 2017
SQLDay 2017
Index (B-Tree) - Seek
SELECT * FROM Users
WHERE Id = 523
1 .. 1M
1M-2K .. 1M
1 .. 2K
2K+1 .. 4K
…
1 .. 300
301..800
801..1,5K
1,5K+1..2K
…
SQLDay 2017
Index (B-Tree) - Scan
SELECT * FROM Users
WHERE Id < 1000000
1 .. 1M
1M-2K .. 1M
1 .. 2K
2K+1 .. 4K
…
1 .. 300
301..800
801..1,5K
1,5K+1..2K
…
SQLDay 2017
Statistics
SELECT * FROM Users
WHERE Id BETWEEN 1
2100
ANDAND
5300
2500
1200
1000
800
500
10
1
800
2000
2800
SQLDay 2017
4500
5400
DEMO
SWISS ARMY KNIFE QUERY
PHYSICAL JOINS
Joins – Nested Loops
Users
Id = 1
Id = 2
Id = 3
Id = 4
Badges
UserId = 1
UserId = 4
UserId = 5
UserId = 1
UserId = 3
UserId = 4
Joins – Merge Join
Users
Id = 1
Id = 2
Id = 3
Id = 4
Badges
UserId = 1
UserId = 1
UserId = 3
UserId = 4
UserId = 4
UserId = 5
Hashtable
0
1
John Dow
Adam
John Smith
Dow
2
3
Adam Smith
Hash Function
4
02
Joins – Hash Join
Users
0
Id = 1
1
Id = 2
2
Id = 3
3
Id = 4
Badges
UserId = 1
0
UserId = 4
UserId = 5
3
10
UserId = 1
0
UserId = 3
2
UserId = 4
3
DEMO
PHYSICAL JOINS
Joins – Merge Join (Many to Many)
Users
Id = 1
Id = 1
Id = 3
Id = 4
Badges
UserId = 1
UserId = 1
UserId = 3
UserId = 4
UserId = 4
UserId = 5
DEADLOCKS
Lock Types - Shared
X
S
S
SQLDay 2017
Lock Types - Exclusive
S
X
X
SQLDay 2017
Lock Types - Update
S
U
X
U
S
X
U
X
U
SQLDay 2017
READ COMMITTED
BEGIN TRAN
UPDATE Users
SET City = 'London'
WHERE City = N'Wrocław'
ID
City
X 1
Wrocław
London
2
London
3
London
4
London
5
London
6
New York
7
New York
SQLDay 2017
SELECT * FROM Users
WHERE City = N'Wrocław'
SELECT * FROM Users
WHERE City = N'Wrocław'
Wait to put Shared
lock on the row
Classic Deadlock
DEADLOCK!
BEGIN TRAN
UPDATE Users
SET CityId = 2
WHERE Id = 4
UPDATE City
SET Name = 'Dnipro'
WHERE Id = 3
ID
City
1
Kyiv
2
Wrocław
3
BEGIN TRAN
UPDATE City
SET Name = 'Dnipro'
WHERE Id = 3
Dnipropetrovsk X
ID
User
City_Id
1
Marcin Szeliga
2
2
Tobiasz Koprowski
2
3
Damian Widera
2
John Smith
3
X 4
SQLDay 2017
UPDATE Users
SET CityId = 2
WHERE Id = 4
DEMO
DEADLOCK
PARAMETER SNIFFING
Parameter Sniffing - Stored Procedure
Query Processor
EXEC ReportSecurityDashboard
@UserId = 1
EXEC ReportSecurityDashboard
@UserId = 22
SQL Server Cache
Procedure cache
Query
Plan created
executes
andusing
cached
thefor
query
the plan created for
@UserId = 1
Parameter Sniffing - Parametrized Query
Query Processor
sp_executesql N'SELECT * FROM Users
SELECT * FROM Users WHERE Id = @Id
WHERE Id = @Id', N'@Id int', 1
sp_executesql N'SELECT * FROM Users
SELECT * FROM Users WHERE Id = @Id
WHERE Id = @Id', N'@Id int', 22
SQL Server Cache
Procedure cache
Plan created
andusing
cached
the plan created for
Query
executes
thefor
query
@Id = 1
Dynamic SQL – Multiple Plans
Query Processor
SELECT * FROM Clients WHERE Id = 1
SELECT * FROM Clients WHERE Id = 22
SQL Server Cache
SELECT * FROM Clients WHERE Id = 1
Procedure cache
Queryquery
New
executed
plan again
created
using created
the
and
query
cached.
and
plan,
cached.
Query
created for
executed
Query
the
firstexecuted
query.
using newly
using created
newly created
plan. plan.
“This is one of these things that makes me believe that
SQL Server is design for maximum level of confusion.”
(c) Erland Sommarskog, Microsoft Data Platform MVP, Sweden
SQLDay 2017
SQLDay 2017
DEMO
PARAMETER SNIFFING
QUERY OPTIMIZATION
Query Processing
Parser
Algebraizer
Optimizer
Plan Cache
Executor
DEMO
QUERY OPTIMIZATION
Summary
•
•
•
•
•
Swiss Army Knife Query
Physical Join
Deadlocks
Parameter Sniffing
Query Optimization
SQLDay 2017
Thank You!
@denisreznik
[email protected]
http://reznik.uneta.com.ua/
https://www.facebook.com/denis.reznik.5
https://www.linkedin.com/pub/denis-reznik/3/502/234
SQLDay 2017
SQLDay 2017