Query Execution Expectation-Reality Denis Reznik

Slides:



Advertisements
Similar presentations
5 Common SQL Server Performance Issues Jason Hall-SQL Sentry, Dir of Client Services Blog-jasonhall.blogs.sqlsentry.net.
Advertisements

Tempdb Parasites Jason Hall-Dir. of Client SQL Sentry Blog-jasonhall.blogs.sqlsentry.net.
IN-MEMORY OLTP By Manohar Punna SQL Server Geeks – Regional Mentor, Hyderabad Blogger, Speaker.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Pinal Dave Mentor | Solid Quality India |
Locking Internals By Amit R S Bansal Director, Principal Consultant & Trainer |
How to kill SQL Server Performance Håkan Winther.
What is Data Science and Who is Data Scientist
Deadlocks 3.0. Final Edition. Everything that developer needs to know Denis Reznik Microsoft SQL Server MVP Director of R&D at Intapp Kyiv.
SQL Server Deep Dive Denis Reznik Data Architect at Intapp.
Execution Plans Detail From Zero to Hero İsmail Adar.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL Server Performance Tuning
SQL Server Statistics and its relationship with Query Optimizer
Denis Reznik Data Architect, Intapp, Inc. Microsoft Data Platform MVP
Parameter Sniffing in SQL Server Stored Procedures
Thank You! #sqlsatdnipro Denis
Are You There, DBA? It’s Me, The App Developer.
Stored Procedures – Facts and Myths
Parameter Sniffing in SQL Server Stored Procedures
Reading execution plans successfully
Design Seamless Upgrades to SQL Server 2016 with Query Store
Summit Nashville /28/2018 8:42 PM
Please Support Our Sponsors
SQL Server Client Tools 2018
SSAS Tabular Toolbelt Sergiy Lunyakin.
Solving ETL Bottlenecks with SSIS Scale Out
Introduction to Execution Plans
Parallel Database Maintenance with 24/7 Systems and Huge DBs
Performance Monitoring Using Extended Events, DMVs & Query Store
A lightweight editor for SSAS Tabular Models built in .NET / WinForms
What Power BI users need to know about R
What is the Azure SQL Datawarehouse?
Administrating SSRS without boring web based clicks!
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
T-SQL Window Function Deep Dive part 1
Introduction to partitioning
It’s Always a Hard Choice
Deep Dive into Adaptive Query Processing
Everything you ever wanted to ask but were too shy
SQL Server 2014 Hidden Treasures Denis Reznik Microsoft SQL Server MVP
Hidden Gems of SQL Server 2014
Hidden gems of SQL Server 2016
Transactions, Locking and Query Optimisation
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
T-SQL Window function deep dive part 2
SQL Server Performance Tuning Nowadays
Transact SQL Performance Tips
Ascending Key Problem in SQL Server Large Tables
Hidden Gems of SQL Server 2016
Tips & Tricks Knowing the Web Client.
Hidden Gems of SQL Server 2014
Introduction to Execution Plans
Parameter Sniffing on SQL Server
Summit Nashville /3/2019 1:48 AM
Score a (row) goal and beat a query optimizer
Deadlocks Everything you ever wanted to ask but were too shy
Query Tuning Fundamentals
Hidden Gems of SQL Server 2014
The Ins and Outs of Indexes
Hidden Gems of SQL Server 2014
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
From adaptive to intelligent: query processing in SQL Server 2019
Denis Reznik SQL Server 2017 Hidden Gems.
Introduction to Execution Plans
Why should I care about SQL, if I have ORM?
From adaptive to intelligent:
Denis Reznik SQL Server 2017 Hidden Gems.
Presentation transcript:

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