SlideShare uma empresa Scribd logo
1 de 28
True SQL Server Detective
Denis Reznik
Intapp
About Me
• Denis Reznik
• Kyiv, Ukraine
• Data Architect at Intapp
• 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”
Agenda
• Detective Story 1
• Why my query is slow?
• Detective Story 2
• Where is my database, dude?
• Detective Story 3
• File size is not reducing.
• Detective Story 4
• My CPU is burning but no queries are executing!
• Detective Story 5 (?)
• How deep is the rabbit hole?
Detective Story 1
Why my query is slow?
Case Details
• User Experience
• Everything is slow
• System is not working
• Technical Details
• Query Timeout errors
• Connection Timeout Errors
• Long-Running Queries
Query Execution
Thread Pool
Running
Suspended
Runnable
Scheduler
Logical CPU
Worker (Thread)
Connection
LCK_M_* Waits
0
SchedulersRunnable Queue
1
2 1
Suspended Queue
2
SELECT * FROM Users
WHERE Name = 'Scott Nall'
UPDATE Users
SET Name = 'Optimus Prime'
WHERE Id = 3
Id Name
1 John Snow
2 Peter Partner
3 Scott Nall
4 Jane Dow
1
Users
2
3
2
Id Name
1 John Snow
2 Peter Partner
3 Optimus Prime
4 Jane Dow
LCK_M_X
LCK_M_X
2
X S
Demo
Why my query is slow?
Detective Story 2
Where is my database?
Case Details
• User Experience
• Database disappeared
• System is not working
• Technical Details
• No database on the server
Demo
Where is my database?
Detective Story 3
File size is not reducing
Case Details
• User Experience
• Disk space is running out
• Database file is not shrinking
• Preparing for disaster
• Technical Details
• Database Shrink operation stuck even on 1 MB Shrink
• Database is in FULL recovery mode
• Database is a part of availability group
• Database indexes are highly fragmented (50%-90%)
Clustered Index
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K 1M-2K .. 1M
1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
Demo
File size is not reducing
Allocation Units
UPDATE
IN_ROW_DATA (HOBT) ROW_OVERFLOW_DATA
(SLOB)
Allocation Units
IN_ROW_DATA (HOBT) LOB_DATA (BLOB)
Detective Story 4
My CPU is burning but no queries are executing!
Case Details
• User Experience
• No explanation for the consistent 10%-20% of CPU utilization
• During the peak load this additional utilization become critical
• Technical Details
• Let’s check them on Demo
Demo
My CPU is burning but no queries are executing!
Detective Story 5
How deep is the rabbit hole?
Case Details
• User Experience
• Application become slow apparently
• In a minute the same operation can be fast again
• Technical Details
• Query execution is really slow
• Running the same query in SSMS is almost instantaneous
Parameter Sniffing - Stored Procedure
EXEC ReportSecurityPermissions
@UserId = 1
Query
Processor
SQL Server Cache
Procedure cache
Plan created and cached for the
@UserId = 1
Procedure cache
EXEC ReportSecurityPermissions
@UserId = 22
Query executes using the query plan created for
@UserId = 1
SELECT * FROM Users WHERE Id = @Id
Parameter Sniffing - Parametrized Query
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 Users WHERE Id = @Id
sp_executesql N'SELECT * FROM Users
WHERE Id = @Id', N'@Id int', 1
sp_executesql N'SELECT * FROM Users
WHERE Id = @Id', N'@Id int', 22
Dynamic SQL – Multiple Plans
SELECT * FROM Users 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 Users WHERE Id = 22
New query plan again created and cached.
Query executed using newly created plan.
SELECT * FROM Users WHERE Id = 1
Query executed using the query plan, created for
the first query.
Procedure cache
Demo
How deep is the rabbit hole?
Summary
• Detective Story 1
• Why my query is slow?
• Detective Story 2
• Where is my database?
• Detective Story 3
• File size is not reducing.
• Detective Story 4
• My CPU is burning but no queries are executing!
• Detective Story 5 (?)
• How deep is the rabbit hole?
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

Mais conteĂşdo relacionado

Semelhante a Denis Reznik "True SQL Server Detective"

Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Fwdays
 
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
NETFest
 
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Ontico
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Kristofferson A
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
DATAVERSITY
 

Semelhante a Denis Reznik "True SQL Server Detective" (20)

Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
Денис Резник "Зачем мне знать SQL и Базы Данных, ведь у меня есть ORM?"
 
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
.NET Fest 2018. Денис Резник. Почему мой запрос тормозит и как это исправить
 
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E...
 
Growing in the Wild. The story by CUBRID Database Developers.
Growing in the Wild. The story by CUBRID Database Developers.Growing in the Wild. The story by CUBRID Database Developers.
Growing in the Wild. The story by CUBRID Database Developers.
 
"What database can tell about application issues? What application can tell a...
"What database can tell about application issues? What application can tell a..."What database can tell about application issues? What application can tell a...
"What database can tell about application issues? What application can tell a...
 
Using Riak for Events storage and analysis at Booking.com
Using Riak for Events storage and analysis at Booking.comUsing Riak for Events storage and analysis at Booking.com
Using Riak for Events storage and analysis at Booking.com
 
Dev nexus 2017
Dev nexus 2017Dev nexus 2017
Dev nexus 2017
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
 
Efficient Scalable Search in a Multi-Tenant Environment: Presented by Harry H...
Efficient Scalable Search in a Multi-Tenant Environment: Presented by Harry H...Efficient Scalable Search in a Multi-Tenant Environment: Presented by Harry H...
Efficient Scalable Search in a Multi-Tenant Environment: Presented by Harry H...
 
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
A Case Study of NoSQL Adoption: What Drove Wordnik Non-Relational?
 
Lecture 01 Evolution of Decision Support Systems
Lecture 01 Evolution of Decision Support SystemsLecture 01 Evolution of Decision Support Systems
Lecture 01 Evolution of Decision Support Systems
 
Benchmarking at Parse
Benchmarking at ParseBenchmarking at Parse
Benchmarking at Parse
 
Advanced Benchmarking at Parse
Advanced Benchmarking at ParseAdvanced Benchmarking at Parse
Advanced Benchmarking at Parse
 
Internals of Presto Service
Internals of Presto ServiceInternals of Presto Service
Internals of Presto Service
 
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
[Srijan Wednesday Webinar] Easy Performance Wins for Your Rails App
 
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelSilicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
 
The inherent complexity of stream processing
The inherent complexity of stream processingThe inherent complexity of stream processing
The inherent complexity of stream processing
 
Breaking data
Breaking dataBreaking data
Breaking data
 
MySQL Optimization from a Developer's point of view
MySQL Optimization from a Developer's point of viewMySQL Optimization from a Developer's point of view
MySQL Optimization from a Developer's point of view
 
Leveraging Apache Spark and Delta Lake for Efficient Data Encryption at Scale
Leveraging Apache Spark and Delta Lake for Efficient Data Encryption at ScaleLeveraging Apache Spark and Delta Lake for Efficient Data Encryption at Scale
Leveraging Apache Spark and Delta Lake for Efficient Data Encryption at Scale
 

Mais de Fwdays

Mais de Fwdays (20)

"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y..."How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
 
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
 
"Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl..."Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl...
 
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T..."How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
 
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ..."The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
 
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu..."[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
 
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care..."[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
 
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"..."4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
 
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast..."Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
 
"Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others..."Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others...
 
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
 
"Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv..."Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv...
 
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin..."How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

Denis Reznik "True SQL Server Detective"

  • 1. True SQL Server Detective Denis Reznik Intapp
  • 2. About Me • Denis Reznik • Kyiv, Ukraine • Data Architect at Intapp • 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. Agenda • Detective Story 1 • Why my query is slow? • Detective Story 2 • Where is my database, dude? • Detective Story 3 • File size is not reducing. • Detective Story 4 • My CPU is burning but no queries are executing! • Detective Story 5 (?) • How deep is the rabbit hole?
  • 4. Detective Story 1 Why my query is slow?
  • 5. Case Details • User Experience • Everything is slow • System is not working • Technical Details • Query Timeout errors • Connection Timeout Errors • Long-Running Queries
  • 7. LCK_M_* Waits 0 SchedulersRunnable Queue 1 2 1 Suspended Queue 2 SELECT * FROM Users WHERE Name = 'Scott Nall' UPDATE Users SET Name = 'Optimus Prime' WHERE Id = 3 Id Name 1 John Snow 2 Peter Partner 3 Scott Nall 4 Jane Dow 1 Users 2 3 2 Id Name 1 John Snow 2 Peter Partner 3 Optimus Prime 4 Jane Dow LCK_M_X LCK_M_X 2 X S
  • 8. Demo Why my query is slow?
  • 9. Detective Story 2 Where is my database?
  • 10. Case Details • User Experience • Database disappeared • System is not working • Technical Details • No database on the server
  • 11. Demo Where is my database?
  • 12. Detective Story 3 File size is not reducing
  • 13. Case Details • User Experience • Disk space is running out • Database file is not shrinking • Preparing for disaster • Technical Details • Database Shrink operation stuck even on 1 MB Shrink • Database is in FULL recovery mode • Database is a part of availability group • Database indexes are highly fragmented (50%-90%)
  • 14. Clustered Index … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
  • 15. Demo File size is not reducing
  • 16. Allocation Units UPDATE IN_ROW_DATA (HOBT) ROW_OVERFLOW_DATA (SLOB)
  • 18. Detective Story 4 My CPU is burning but no queries are executing!
  • 19. Case Details • User Experience • No explanation for the consistent 10%-20% of CPU utilization • During the peak load this additional utilization become critical • Technical Details • Let’s check them on Demo
  • 20. Demo My CPU is burning but no queries are executing!
  • 21. Detective Story 5 How deep is the rabbit hole?
  • 22. Case Details • User Experience • Application become slow apparently • In a minute the same operation can be fast again • Technical Details • Query execution is really slow • Running the same query in SSMS is almost instantaneous
  • 23. Parameter Sniffing - Stored Procedure EXEC ReportSecurityPermissions @UserId = 1 Query Processor SQL Server Cache Procedure cache Plan created and cached for the @UserId = 1 Procedure cache EXEC ReportSecurityPermissions @UserId = 22 Query executes using the query plan created for @UserId = 1
  • 24. SELECT * FROM Users WHERE Id = @Id Parameter Sniffing - Parametrized Query 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 Users WHERE Id = @Id sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@Id int', 1 sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@Id int', 22
  • 25. Dynamic SQL – Multiple Plans SELECT * FROM Users 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 Users WHERE Id = 22 New query plan again created and cached. Query executed using newly created plan. SELECT * FROM Users WHERE Id = 1 Query executed using the query plan, created for the first query. Procedure cache
  • 26. Demo How deep is the rabbit hole?
  • 27. Summary • Detective Story 1 • Why my query is slow? • Detective Story 2 • Where is my database? • Detective Story 3 • File size is not reducing. • Detective Story 4 • My CPU is burning but no queries are executing! • Detective Story 5 (?) • How deep is the rabbit hole?
  • 28. 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