SlideShare a Scribd company logo
Киев 2017
Только реальные кейсы. Только актуальные тренды.
SQL Server Execution
Expectation - Reality
Денис Резник
Киев 2017
About me
• Denis Reznik
• Kyiv, Ukraine
• Data Architect at Intapp, Inc.
• Microsoft Data Platform MVP
• Co-Founder Ukrainian Data Community
• PASS Regional Mentor, Central, and Eastern
Europe
• Author of “SQL Server MVP Deep Dives 2”
Chapter 19
2 |
Киев 2017
Agenda
• Expectation - Reality 1
• Expectation - Reality 2
• Expectation - Reality 3
• Expectation - Reality 4
Parameter Sniffing
Expectation - Reality 1
Киев 2017
EXEC ReportCheckSecurityRequest
@UserId = 1
Query
Processor
SQL Server Cache
Procedure cache
Plan created and cached for the
@UserId = 1
Procedure cache
EXEC ReportCheckSecurityRequest
@UserId = 22
Query executes using the query plan created for
@UserId = 1
Parameter Sniffing - Stored Procedure
Киев 2017
SELECT * FROM Clients WHERE Id = @Id
Query
Processor
SQL Server Cache
Procedure cache
Plan created and cached for the
@Id = 1
Procedure cache
Query executes using the query plan created for
@Id = 1
SELECT * FROM Clients WHERE Id = @Id
sp_executesql N'SELECT * FROM Clients
WHERE Id = @Id', N'@Id int', 1
sp_executesql N'SELECT * FROM Clients
WHERE Id = @Id', N'@Id int', 22
Parameter Sniffing – Parametrized Query
Киев 2017
SELECT * FROM Clients WHERE Id = 1
Query
Processor
SQL Server Cache
Procedure cache
New query plan created and cached. Query
executed using newly created plan.
Procedure cache
SELECT * FROM Clients WHERE Id = 22
New query plan again created and cached. Query
executed using newly created plan.
SELECT * FROM Clients WHERE Id = 1
Query executed using the query plan, created for
the first query.
Procedure cache
Dynamic SQL – Multiple Plans
Index Seek
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K 1M-2K .. 1M
1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
SELECT * FROM Users
WHERE Id = 523
Index Scan
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K 1M-2K .. 1M
1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
SELECT * FROM Users
DEMO
Parameter Sniffing
Cardinality Estimation
Expectation - Reality 2
Киев 2017
500
1000
10
1200
800
1 800 2000 2800 4500 5400
SELECT * FROM Users
WHERE Id BETWEEN 2100 AND 2500Statistics
SELECT * FROM Users
WHERE Id BETWEEN 10 AND 5300
Киев 2017
500
1000
10
1200
800
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%'
10600
20000 10
1200 800
A G L S T ZZZZ
Users
Posts
DEMO
Cardinality Estimation
Deadlocks
Expectation - Reality 3
Киев 2017
Lock Types - Shared
S S
X
Киев 2017
Lock Types - Exclusive
X
X
S
Киев 2017
Lock Types - Update
U
U
s
SX
UX
UX
Киев 2017
ID City
1 Kyiv
2 Kharkiv
3 Dnipropetrovsk
BEGIN TRAN
UPDATE Users
SET CityId = 2
WHERE Id = 4
UPDATE City
SET Name = 'Dnipro'
WHERE Id = 3
ID User City_Id
1 John Smith 2
2 Dale Cooper 2
3 Oliver Thompson 2
4 John Dow 3X
BEGIN TRAN
UPDATE City
SET Name = 'Dnipro'
WHERE Id = 3
UPDATE Users
SET CityId = 2
WHERE Id = 4
X
DEADLOCK!
Classic Deadlock
DEMO
Deadlocks
Query Optimization
Expectation - Reality 4
Киев 2017
Query Processing
Parser Procedure
Cache
Algebraizer
Optimizer
Executor
Киев 2017
Optimizer Phase 2 (Last Phase)
Full Set of Optimization Rules
Returns Full Query Plan
• All planned checks were done
• Good Enough Plan was Found
• Timeout
• Not Enough Memory
Optimization
DEMO
Query Optimization
Киев 2017
Summary
• Parameter Sniffing
• Slow in Application. Fast in SSMS.
• Cardinality Estimation
• SQL Server Version Upgrade
• Deadlocks
• Non-Detectable Deadlocks
• Query Optimization
• Query Optimization Timeout
Киев 2017
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

More Related Content

PPTX
.NET Fest 2017. Андрей Антиликаторов. Проектирование и разработка приложений ...
PDF
.NET Fest 2017. Anton Moldovan. How do we cook highload microservices at SBTech?
PPTX
Intellias CQRS Framework
PDF
Digital Forensics and Incident Response in The Cloud
PDF
Velociraptor - SANS Summit 2019
PDF
Crikeycon 2019 Velociraptor Workshop
PDF
Nzitf Velociraptor Workshop
PDF
Microservices:
 The phantom menace
. Istio Service Mesh: 
the new hope
.NET Fest 2017. Андрей Антиликаторов. Проектирование и разработка приложений ...
.NET Fest 2017. Anton Moldovan. How do we cook highload microservices at SBTech?
Intellias CQRS Framework
Digital Forensics and Incident Response in The Cloud
Velociraptor - SANS Summit 2019
Crikeycon 2019 Velociraptor Workshop
Nzitf Velociraptor Workshop
Microservices:
 The phantom menace
. Istio Service Mesh: 
the new hope

What's hot (20)

PDF
Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...
PDF
Digital Forensics and Incident Response in The Cloud Part 3
PDF
Digital Forensics and Incident Response in The Cloud
PDF
What’s new in WSO2 Enterprise Integrator 6.6
PDF
What Does Kubernetes Look Like?: Performance Monitoring & Visualization with ...
PPTX
From Monolithic to Microservices in 45 Minutes
PPTX
Introduction to MERN Stack
PPTX
Microservices in GO lang
PPTX
Managing Cloud Security Design and Implementation in a Ransomware World
PDF
Blockchain Technology Utilizationin Global Rakuten Ecosystem
PPTX
Getting started with Azure Event Grid - Webinar with Steef-Jan Wiggers
PDF
Microservices: a journey of an eternal improvement
PDF
Technical Debt - SOTR14 - Clarkie
PPTX
Using an API Gateway for Microservices
PDF
Micro Frontends
PDF
NGINX KubeCon Copenhagen 2018
PDF
Best Practices with WSO2 Developer Studio
PPTX
Cloud native policy enforcement with Open Policy Agent
PDF
Microservices with Spring
PPTX
MongoDB Days UK: Building Apps with the MEAN Stack
Monitoring Big Data Systems Done "The Simple Way" - Demi Ben-Ari - Codemotion...
Digital Forensics and Incident Response in The Cloud Part 3
Digital Forensics and Incident Response in The Cloud
What’s new in WSO2 Enterprise Integrator 6.6
What Does Kubernetes Look Like?: Performance Monitoring & Visualization with ...
From Monolithic to Microservices in 45 Minutes
Introduction to MERN Stack
Microservices in GO lang
Managing Cloud Security Design and Implementation in a Ransomware World
Blockchain Technology Utilizationin Global Rakuten Ecosystem
Getting started with Azure Event Grid - Webinar with Steef-Jan Wiggers
Microservices: a journey of an eternal improvement
Technical Debt - SOTR14 - Clarkie
Using an API Gateway for Microservices
Micro Frontends
NGINX KubeCon Copenhagen 2018
Best Practices with WSO2 Developer Studio
Cloud native policy enforcement with Open Policy Agent
Microservices with Spring
MongoDB Days UK: Building Apps with the MEAN Stack
Ad

Similar to .NET Fest 2017. Денис Резник. Исполнение Запроса в SQL Server. Ожидание - Реальность (20)

PPTX
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
PPTX
SQL Server Deep Dive, Denis Reznik
PPTX
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
PPTX
SQL Server Deep Drive
PDF
"What database can tell about application issues? What application can tell a...
PPTX
Growing in the Wild. The story by CUBRID Database Developers.
PPTX
Professional Services Insights into Improving Sitecore XP
PPTX
Tutorial: Building Your First App with MongoDB Stitch
PPTX
Sizing MongoDB Clusters
PPTX
Building Your First App with MongoDB Stitch
PPTX
.NET Fest 2019. Денис Резник. Когда SQL Server Best Practices не работают
PPTX
D3SF17- Improving Our China Clients Performance
PPTX
Time series Analytics - a deep dive into ADX Azure Data Explorer @Data Saturd...
PPTX
Se2017 query-optimizer
PDF
Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b...
PPTX
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
PPTX
Starting from Scratch with the MEAN Stack
PDF
Your App Deserves More – The Art of App Modernization
PPTX
Azure Stream Analytics : Analyse Data in Motion
PDF
Your App deserves more – The Art of App Modernization
"Query Execution: Expectation - Reality (Level 300)" Денис Резник
SQL Server Deep Dive, Denis Reznik
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
SQL Server Deep Drive
"What database can tell about application issues? What application can tell a...
Growing in the Wild. The story by CUBRID Database Developers.
Professional Services Insights into Improving Sitecore XP
Tutorial: Building Your First App with MongoDB Stitch
Sizing MongoDB Clusters
Building Your First App with MongoDB Stitch
.NET Fest 2019. Денис Резник. Когда SQL Server Best Practices не работают
D3SF17- Improving Our China Clients Performance
Time series Analytics - a deep dive into ADX Azure Data Explorer @Data Saturd...
Se2017 query-optimizer
Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b...
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Starting from Scratch with the MEAN Stack
Your App Deserves More – The Art of App Modernization
Azure Stream Analytics : Analyse Data in Motion
Your App deserves more – The Art of App Modernization
Ad

More from NETFest (20)

PDF
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET
PPTX
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...
PPTX
.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET
PPTX
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов
PPTX
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...
PPTX
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
PPTX
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex
PPTX
.NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A...
PPTX
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
PPTX
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
PPTX
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
PPTX
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
PPTX
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
PPTX
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
PDF
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
PDF
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
PPTX
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
PPTX
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
PPTX
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
PDF
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...
.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex
.NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A...
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...

Recently uploaded (20)

PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Empowerment Technology for Senior High School Guide
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Trump Administration's workforce development strategy
PDF
Hazard Identification & Risk Assessment .pdf
PDF
1_English_Language_Set_2.pdf probationary
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
PDF
Computing-Curriculum for Schools in Ghana
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Unit 4 Skeletal System.ppt.pptxopresentatiom
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Cell Types and Its function , kingdom of life
A systematic review of self-coping strategies used by university students to ...
LDMMIA Reiki Yoga Finals Review Spring Summer
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Empowerment Technology for Senior High School Guide
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Trump Administration's workforce development strategy
Hazard Identification & Risk Assessment .pdf
1_English_Language_Set_2.pdf probationary
A powerpoint presentation on the Revised K-10 Science Shaping Paper
SOIL: Factor, Horizon, Process, Classification, Degradation, Conservation
Computing-Curriculum for Schools in Ghana

.NET Fest 2017. Денис Резник. Исполнение Запроса в SQL Server. Ожидание - Реальность

  • 1. Киев 2017 Только реальные кейсы. Только актуальные тренды. SQL Server Execution Expectation - Reality Денис Резник
  • 2. Киев 2017 About me • Denis Reznik • Kyiv, Ukraine • Data Architect at Intapp, Inc. • Microsoft Data Platform MVP • Co-Founder Ukrainian Data Community • PASS Regional Mentor, Central, and Eastern Europe • Author of “SQL Server MVP Deep Dives 2” Chapter 19 2 |
  • 3. Киев 2017 Agenda • Expectation - Reality 1 • Expectation - Reality 2 • Expectation - Reality 3 • Expectation - Reality 4
  • 5. Киев 2017 EXEC ReportCheckSecurityRequest @UserId = 1 Query Processor SQL Server Cache Procedure cache Plan created and cached for the @UserId = 1 Procedure cache EXEC ReportCheckSecurityRequest @UserId = 22 Query executes using the query plan created for @UserId = 1 Parameter Sniffing - Stored Procedure
  • 6. Киев 2017 SELECT * FROM Clients WHERE Id = @Id Query Processor SQL Server Cache Procedure cache Plan created and cached for the @Id = 1 Procedure cache Query executes using the query plan created for @Id = 1 SELECT * FROM Clients WHERE Id = @Id sp_executesql N'SELECT * FROM Clients WHERE Id = @Id', N'@Id int', 1 sp_executesql N'SELECT * FROM Clients WHERE Id = @Id', N'@Id int', 22 Parameter Sniffing – Parametrized Query
  • 7. Киев 2017 SELECT * FROM Clients WHERE Id = 1 Query Processor SQL Server Cache Procedure cache New query plan created and cached. Query executed using newly created plan. Procedure cache SELECT * FROM Clients WHERE Id = 22 New query plan again created and cached. Query executed using newly created plan. SELECT * FROM Clients WHERE Id = 1 Query executed using the query plan, created for the first query. Procedure cache Dynamic SQL – Multiple Plans
  • 8. Index Seek … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K SELECT * FROM Users WHERE Id = 523
  • 9. Index Scan … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K SELECT * FROM Users
  • 12. Киев 2017 500 1000 10 1200 800 1 800 2000 2800 4500 5400 SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500Statistics SELECT * FROM Users WHERE Id BETWEEN 10 AND 5300
  • 13. Киев 2017 500 1000 10 1200 800 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%' 10600 20000 10 1200 800 A G L S T ZZZZ Users Posts
  • 16. Киев 2017 Lock Types - Shared S S X
  • 17. Киев 2017 Lock Types - Exclusive X X S
  • 18. Киев 2017 Lock Types - Update U U s SX UX UX
  • 19. Киев 2017 ID City 1 Kyiv 2 Kharkiv 3 Dnipropetrovsk BEGIN TRAN UPDATE Users SET CityId = 2 WHERE Id = 4 UPDATE City SET Name = 'Dnipro' WHERE Id = 3 ID User City_Id 1 John Smith 2 2 Dale Cooper 2 3 Oliver Thompson 2 4 John Dow 3X BEGIN TRAN UPDATE City SET Name = 'Dnipro' WHERE Id = 3 UPDATE Users SET CityId = 2 WHERE Id = 4 X DEADLOCK! Classic Deadlock
  • 22. Киев 2017 Query Processing Parser Procedure Cache Algebraizer Optimizer Executor
  • 23. Киев 2017 Optimizer Phase 2 (Last Phase) Full Set of Optimization Rules Returns Full Query Plan • All planned checks were done • Good Enough Plan was Found • Timeout • Not Enough Memory Optimization
  • 25. Киев 2017 Summary • Parameter Sniffing • Slow in Application. Fast in SSMS. • Cardinality Estimation • SQL Server Version Upgrade • Deadlocks • Non-Detectable Deadlocks • Query Optimization • Query Optimization Timeout
  • 26. Киев 2017 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