Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Denis Reznik "True SQL Server Detective"

123 views

Published on

Так, ви не помилилися. Це саме детективна історія. Навіть кілька. Кожна з яких має заплутаний сюжет, кілька діючих (або бездіяльних) осіб, факти та докази. У доповіді ми розплутаємо всі ці справи і пройдемо шлях від отримання інформації (точної або неточної) до повного розуміння та вирішення проблеми, що дозволить вам добре орієнтуватися в схожих ситуаціях й ефективніше працювати з базою даних. Розкривати більше деталей зараз не можу (самі розумієте), але можу обіцяти, що буде дуже цікаво.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Denis Reznik "True SQL Server Detective"

  1. 1. True SQL Server Detective Denis Reznik Intapp
  2. 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. 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. 4. Detective Story 1 Why my query is slow?
  5. 5. Case Details • User Experience • Everything is slow • System is not working • Technical Details • Query Timeout errors • Connection Timeout Errors • Long-Running Queries
  6. 6. Query Execution Thread Pool Running Suspended Runnable Scheduler Logical CPU Worker (Thread) Connection
  7. 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. 8. Demo Why my query is slow?
  9. 9. Detective Story 2 Where is my database?
  10. 10. Case Details • User Experience • Database disappeared • System is not working • Technical Details • No database on the server
  11. 11. Demo Where is my database?
  12. 12. Detective Story 3 File size is not reducing
  13. 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. 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. 15. Demo File size is not reducing
  16. 16. Allocation Units UPDATE IN_ROW_DATA (HOBT) ROW_OVERFLOW_DATA (SLOB)
  17. 17. Allocation Units IN_ROW_DATA (HOBT) LOB_DATA (BLOB)
  18. 18. Detective Story 4 My CPU is burning but no queries are executing!
  19. 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. 20. Demo My CPU is burning but no queries are executing!
  21. 21. Detective Story 5 How deep is the rabbit hole?
  22. 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. 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. 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. 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. 26. Demo How deep is the rabbit hole?
  27. 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. 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

×