Download presentation
Presentation is loading. Please wait.
Published byIndra Setiabudi Modified over 6 years ago
1
Query Execution Expectation-Reality Denis Reznik
Data Architect at Intapp, Inc. Microsoft Data Platform MVP
2
About Me Denis Reznik Kyiv, Ukraine Data Architect at Intapp, Inc.
Microsoft Data Platform MVP Co-Founder of Ukrainian Data Community Kyiv (PASS Chapter) PASS Regional Mentor, Central and Eastern Europe Co-author of “SQL Server MVP Deep Dives vol. 2”
3
Just like Jimi Hendrix …
We love to get feedback Please complete the session feedback forms
4
SQLBits - It's all about the community...
Please visit Community Corner, we are trying this year to get more people to learn about the SQL Community, equally if you would be happy to visit the community corner we’d really appreciate it.
5
Agenda Expectation - Reality 1 Expectation - Reality 2
6
Parameter Sniffing Expectation – Reality 1
7
Parameter Sniffing - Stored Procedure
Query Processor EXEC = 1 EXEC = 22 SQL Server Cache Procedure cache Procedure cache Query executes using the query plan created for @UserId = 1 Plan created and cached for the @UserId = 1
8
Parameter Sniffing - Parametrized Query
Query Processor sp_executesql N'SELECT * FROM Users WHERE Id int', 1 SELECT * FROM Users WHERE Id SELECT * FROM Users WHERE Id sp_executesql N'SELECT * FROM Users WHERE Id int', 22 SQL Server Cache Procedure cache Procedure cache Plan created and cached for the @Id = 1 Query executes using the query plan created for @Id = 1
9
Dynamic SQL – Multiple Plans
Query Processor SELECT * FROM Users WHERE Id = 1 SELECT * FROM Users WHERE Id = 22 SQL Server Cache SELECT * FROM Users WHERE Id = 1 Procedure cache Procedure cache Procedure cache Query executed using the query plan, created for the first query. New query plan created and cached. Query executed using newly created plan. New query plan again created and cached. Query executed using newly created plan.
10
… … Index Seek SELECT * FROM Users WHERE Id = 523 1 .. 1M 1 .. 2K
2K K 1M-2K .. 1M … ,5K 1,5K K …
11
… … Index Scan SELECT * FROM Users 1 .. 1M 1 .. 2K 2K+1 .. 4K
1M-2K .. 1M … ,5K 1,5K K …
12
DEMO Parameter Sniffing
13
Cardinality Estimation
Expectation – Reality 2
14
Statistics SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500
800 2000 2800 4500 5400
15
Out of Range Statistics
SELECT * FROM Users WHERE Id > 5400 ? 1 800 2000 2800 4500 5400
16
Statistics SELECT * FROM Users u INNER JOIN Posts p ON u.Id = p.OwnerUserId WHERE u.DisplayName LIKE 'Jeff%' Users A G L S T ZZZZ Posts 1 800 2000 2800 4500 5400
17
Cardinality Estimation
DEMO Cardinality Estimation
18
Statistics Update SQL Server 2014 and lower default behavior:
20% of rows + 500 SQL Server 2016 Dynamic threshold for tables with rows TF 2371 since SQL Server 2008R2 SP1
19
Deadlocks Expectation – Reality 3
20
Lock Types - Shared X S S
21
Lock Types - Exclusive S X X
22
Lock Types - Update S U U X S U X X U
23
Classic Deadlock DEADLOCK! wait wait ID City 1 Kyiv 2 Kharkiv 3
BEGIN TRAN UPDATE Users SET CityId = 2 WHERE Id = 4 UPDATE City SET Name = 'Dnipro' WHERE Id = 3 BEGIN TRAN UPDATE City SET Name = 'Dnipro' WHERE Id = 3 UPDATE Users SET CityId = 2 WHERE Id = 4 ID City 1 Kyiv 2 Kharkiv 3 Dnipropetrovsk X wait wait ID User City_Id 1 Tony Stark 2 John Smith 3 Clark Kent 4 Ivan Vanko X
24
DEMO Deadlocks
25
Query Optimization Expectation – Reality 4
26
Query Processing Parser Algebraizer Optimizer Executor Plan Cache
27
Optimization Phase 2 Optimizer Full Set of Optimization Rules
Indexed views Returns Full Query Plan All planned checks were done Good Enough Plan was Found Timeout Not Enough Memory
28
DEMO Query Optimization
29
Summary Parameter Sniffing Cardinality Estimation Deadlocks
Slow in Application. Fast in SSMS. Cardinality Estimation SQL Server Version Upgrade Deadlocks Non-Detectable Deadlocks Query Optimization Query Optimization Timeout
30
Thank You! Denis Reznik Twitter: @denisreznik
Blog: Facebook: LinkedIn:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.