300 likes | 313 Views
Explore the expectations and realities of query execution in SQL Server, including parameter sniffing, dynamic SQL, cardinality estimation, and query optimization.
E N D
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 • Expectation - Reality 3 • Expectation - Reality 4
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 SELECT * FROM Users WHERE Id = @Id • sp_executesqlN'SELECT * FROM Users WHERE Id = @Id',N'@Idint', 1 • sp_executesqlN'SELECT * FROM Users WHERE Id = @Id',N'@Idint', 22 SELECT * FROM Users WHERE Id = @Id 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*FROMUsers WHEREId= 523 1 .. 1M 1M-2K .. 1M 2K+1 .. 4K 1 .. 2K … 1,5K+1 .. 2K 301 .. 800 801 .. 1,5K 1 .. 300 …
Index Scan SELECT*FROMUsers 1 .. 1M 1M-2K .. 1M 2K+1 .. 4K 1 .. 2K … 1,5K+1 .. 2K 301 .. 800 801 .. 1,5K 1 .. 300 …
DEMO Parameter Sniffing
Cardinality Estimation Expectation – Reality 2
Statistics SELECT*FROMUsers WHEREIdBETWEEN2100 AND2500 SELECT*FROMUsers WHEREIdBETWEEN 200 AND 5000 1 5400 4500 800 2000 2800
Out of Range Statistics SELECT*FROMUsers WHEREId> 5400 ? 1 5400 4500 800 2000 2800
Statistics SELECT*FROM Users u INNERJOIN Posts p ONu.Id=p.OwnerUserId WHEREu.DisplayNameLIKE'Jeff%' Users L S T ZZZZ A G Posts 2000 2800 4500 5400 1 800
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 S X U X U X U
Classic Deadlock DEADLOCK! BEGINTRAN UPDATE Users SETCityId= 2 WHERE Id = 4 UPDATE City SETName='Dnipro' WHERE Id = 3 BEGINTRAN UPDATE City SETName='Dnipro' WHERE Id = 3 UPDATE Users SETCityId= 2 WHERE Id = 4 X wait wait 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 • 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