Query Execution Expectation-Reality Denis Reznik Data Architect at Intapp, Inc. Microsoft Data Platform MVP
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”
Just like Jimi Hendrix … We love to get feedback Please complete the session feedback forms
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.
Agenda Expectation - Reality 1 Expectation - Reality 2
Parameter Sniffing Expectation – Reality 1
Parameter Sniffing - Stored Procedure Query Processor EXEC ReportSecurityPermissions @UserId = 1 EXEC ReportSecurityPermissions @UserId = 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
Parameter Sniffing - Parametrized Query Query Processor sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@Id int', 1 SELECT * FROM Users WHERE Id = @Id SELECT * FROM Users WHERE Id = @Id sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@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
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.
… … Index Seek SELECT * FROM Users WHERE Id = 523 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M … 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K …
… … Index Scan SELECT * FROM Users 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M … 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K …
DEMO Parameter Sniffing
Cardinality Estimation Expectation – Reality 2
Statistics SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500 800 2000 2800 4500 5400
Out of Range Statistics SELECT * FROM Users WHERE Id > 5400 ? 1 800 2000 2800 4500 5400
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
Cardinality Estimation DEMO Cardinality Estimation
Statistics Update SQL Server 2014 and lower default behavior: 20% of rows + 500 SQL Server 2016 Dynamic threshold for tables with 25000+ rows TF 2371 since SQL Server 2008R2 SP1
Deadlocks Expectation – Reality 3
Lock Types - Shared X S S
Lock Types - Exclusive S X X
Lock Types - Update S U U X S U X X U
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
DEMO Deadlocks
Query Optimization Expectation – Reality 4
Query Processing Parser Algebraizer Optimizer Executor Plan Cache
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
DEMO Query Optimization
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
Thank You! Denis Reznik Twitter: @denisreznik Email: denisreznik@gmail.com Blog: http://reznik.uneta.com.ua Facebook: https://www.facebook.com/denis.reznik.5 LinkedIn: http://ua.linkedin.com/pub/denis-reznik/3/502/234