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.

.NET Fest 2019. Денис Резник. Когда SQL Server Best Practices не работают

39 views

Published on

Best Practices это несомненно очень хорошо, и мы многое выигрываем от их использования. Другой вопрос, можем ли мы применять их неглядя? Просто потому что это Best Practice? SQL Server это довольно сложный продукт и даже обладая большим опытом бывает сложно просчитать последствия того или иного действия, а некоторые ситуации и вовсе бывают порой далеки от привычного понимания вещей. Применяя какую-либо Best Practice нужно понимать как это повлияет на сервер как можно более детально. В этой сесси мы рассмотрим ряд ситуаций, когда применение Best Practice ухудшает работу или производительность сервера и когда специфическое или даже не рекомендуемое решение может работать лучше самой лучшей Best Practice.

Published in: Education
  • Be the first to comment

  • Be the first to like this

.NET Fest 2019. Денис Резник. Когда SQL Server Best Practices не работают

  1. 1. Тема доклада Тема доклада Тема доклада KYIV 2019 When SQL Server Best Practices do not work .NET CONFERENCE #1 IN UKRAINE
  2. 2. Тема доклада Тема доклада Тема доклада .NET LEVEL UP About Me .NET CONFERENCE #1 IN UKRAINE KYIV 2019 • Denis Reznik • Kyiv, Ukraine • Data Architect at Intapp, Inc. • Microsoft Data Platform MVP • Co-Founder of Ukrainian Data Community Kyiv • PASS Regional Mentor, CEE • Co-author of “SQL Server MVP Deep Dives 2”
  3. 3. Тема доклада Тема доклада Тема доклада .NET LEVEL UP Agenda .NET CONFERENCE #1 IN UKRAINE KYIV 2019 • Database Should Be Normalized • Each Table Should Have a Clustered Index • Column Datatypes Should Be as Small as Possible • Cost Threshold for Parallelism Should be Higher Than Default • Max Degree of Parallelism Should Be Set to Best Practices Value • Bonus: NOLOCK Side Effect
  4. 4. Тема доклада Тема доклада Тема доклада KYIV 2019 Database Should Be Normalized .NET CONFERENCE #1 IN UKRAINE
  5. 5. .NET LEVEL UP First Normal Form (1NF) .NET CONFERENCE #1 IN UKRAINE KYIV 2019 Company User Phone Phone Type Microsoft John Dow +380969785732 NULL Microsoft John Dow +32345409123 NULL Microsoft Larry McGregor +45678904692 NULL Oracle Corp. John Snow +380988958371 NULL Amazon Jack Snack +23348902385 Home Amazon Jack Snack +69058763287 Work Each cell contains an atomic value Company User Phone Microsoft John Dow Tel1: +380969785732, Tel2: +32345409123 Microsoft Larry McGregor Tel: +45678904692 Oracle Corp. John Snow +380988958371 Amazon Jack Snack Home: +23348902385 Work: +69058763287 UsersUsers
  6. 6. Тема доклада Тема доклада Тема доклада KYIV 2019 Database Should Be Normalized .NET CONFERENCE #1 IN UKRAINE Demo
  7. 7. Тема доклада Тема доклада Тема доклада KYIV 2019 Database Should Be Normalized Conclusion .NET CONFERENCE #1 IN UKRAINE • Normalize when in makes sense • Denormalize when it makes sense • Do not know what to do? Normalize. • Do not afraid of Denormalization.
  8. 8. Тема доклада Тема доклада Тема доклада KYIV 2019 Each Table Should Have a Clustered Index .NET CONFERENCE #1 IN UKRAINE
  9. 9. Heap 1 .. 100 100 .. 1k 5K .. 6K 1K .. 5K 6K .. 7K 15K .. 21K 12K .. 15K 10K .. 11K 21K .. 22K 22K .. 41K 9K .. 10K 41K .. 51K 7K .. 8K 8K .. 9K 71K .. 1M 51K .. 71K 1M .. 2M 2M .. 3M
  10. 10. Clustered Index … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
  11. 11. 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
  12. 12. Non-Clustered Index … A .. Z A .. C C .. K X .. Z … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M SELECT * FROM Users WHERE Name = 'John Dow' 1 .. 2K 2K .. 4K 1M-2K .. 1M Clustered Index (Id) Non-Clustered Index (Name) Heap
  13. 13. Тема доклада Тема доклада Тема доклада KYIV 2019 Each Table Should Have a Clustered Index .NET CONFERENCE #1 IN UKRAINE Demo
  14. 14. Тема доклада Тема доклада Тема доклада KYIV 2019 Each Table Should Have a Clustered Index Conclusion .NET CONFERENCE #1 IN UKRAINE • Do not afraid Heaps • RID Lookup requires less IO than Key Lookup • Consider Heaps for: • High Key Lookup workloads • Big tables with high insert rate
  15. 15. Тема доклада Тема доклада Тема доклада KYIV 2019 Column Datatypes Should Be as Small as Possible .NET CONFERENCE #1 IN UKRAINE
  16. 16. Тема доклада Тема доклада Тема доклада .NET LEVEL UP Possible Size Reduction .NET CONFERENCE #1 IN UKRAINE KYIV 2019 • int (4 Bytes) -> smallint (2B) -> tinyint (1B) -> bit (up to 1 Byte) • float -> decimal • datetime -> smalldatetime(?) -> datetime2 • nchar (2 Bytes per character) -> char (1 Byte per character) • nvarchar (2 Bytes per character) -> varchar (1 Byte per character)
  17. 17. Тема доклада Тема доклада Тема доклада KYIV 2019 Column Datatypes Should Be as Small as Possible .NET CONFERENCE #1 IN UKRAINE Demo
  18. 18. .NET LEVEL UP Data Type Precedence .NET CONFERENCE #1 IN UKRAINE KYIV 2019 1. user-defined data types (highest) 2. sql_variant 3. xml 4. datetimeoffset 5. datetime2 6. datetime 7. smalldatetime 8. date 9. time 10. float 11. real 12. decimal 13. money 14. smallmoney 15. bigint 16. int 17. smallint 18. tinyint 19. bit
  19. 19. Тема доклада Тема доклада Тема доклада KYIV 2019 Column Datatypes Should Be as Small as Possible Conclusion .NET CONFERENCE #1 IN UKRAINE • Works for almost all datatypes • Use correct datatypes for each column • Prefer (by default) nvarchar over varchar
  20. 20. Тема доклада Тема доклада Тема доклада KYIV 2019 Cost Threshold for Parallelism Should be Higher Than Default .NET CONFERENCE #1 IN UKRAINE
  21. 21. • Goal – get the optimal execution plan • “Optimal” = Plan with the smallest cost • Cost = sum_of_all_operators_costs(I/O + CPU + memory_consumption) Optimizer Query Tree Pre-Optimization Search for trivial plan Load statistics (simple) Simplification – syntax transformation Optimization
  22. 22. Optimizer Phase 0 Load Statistics Exploration – Plan Alternatives Estimated cost < 0.2 Return Transaction Processing Plan Optimization
  23. 23. Query Plan Alternatives SELECT * FROM Users u INNER JOIN Posts p ON u.Id = p.OwnerUserId WHERE u.DisplayName = 'John Snow' Users Posts Posts Users
  24. 24. Query Plan Alternatives • 1 Table – 1 option • 2 Tables – 2 options • 3 Tables – 6 options • 4 Tables – 24 options • … • 10 Tables – 3628800 options • 1 Table – 1! • 2 Tables – 2! • 3 Tables – 3! • 4 Tables – 4! • … • 10 Tables – 10!
  25. 25. Dynamic Programming • JOIN(A,B,C,D) • JOIN(A,B,D,C) • JOIN(A,C,D,B) • JOIN(A,D,B,C) • JOIN(A,D,C,B) • JOIN(B,A,C,D) • JOIN(B,A,D,C) • … • O(N!) • JOIN(A,B,C,D) • A – Optimal Access Path • B – Optimal Access Path • … pruning • (A,B) – Optimal Access Path • … pruning • (A,B),(C) • … pruning • O(𝑁2 𝑛−1 )
  26. 26. Optimizer Phase 1 Next set of query rules Estimate parallel plan (estimation processed twice) • Max Degree of Parallelism • Cost Threshold for Parallelism Estimated cost < 1.0 Returns Quick Plan Optimization
  27. 27. Тема доклада Тема доклада Тема доклада KYIV 2019 Cost Threshold for Parallelism Should be Higher Than Default .NET CONFERENCE #1 IN UKRAINE Demo
  28. 28. Тема доклада Тема доклада Тема доклада KYIV 2019 Cost Threshold for Parallelism Should be Higher Than Default Conclusion .NET CONFERENCE #1 IN UKRAINE • Good to set it higher on a fresh server • Bad to set in higher for the working system • Setting it for a working system requires validation • Validate it for top 5-20 CPU intensive queries
  29. 29. Тема доклада Тема доклада Тема доклада KYIV 2019 Max Degree of Parallelism Should Be Set to Best Practices Value .NET CONFERENCE #1 IN UKRAINE
  30. 30. Parallel Query Execution • Amdal’s Law Thread 1 Thread 2 Thread 3 Thread 4 1s2s
  31. 31. Optimizer Phase 2 (Last Phase) 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 Optimization
  32. 32. Тема доклада Тема доклада Тема доклада KYIV 2019 Max Degree of Parallelism Should Be Set to Best Practices Value .NET CONFERENCE #1 IN UKRAINE Demo
  33. 33. Тема доклада Тема доклада Тема доклада KYIV 2019 Max Degree of Parallelism Should Be Set to Best Practices Value Conclusion .NET CONFERENCE #1 IN UKRAINE • High DOP usually force query to waste CPU • But not in every situation • MAXDOP 1 gives the Optimizer a bit more time
  34. 34. Тема доклада Тема доклада Тема доклада KYIV 2019 Bonus: NOLOCK Side Effect .NET CONFERENCE #1 IN UKRAINE
  35. 35. Тема доклада Тема доклада Тема доклада KYIV 2019 Bonus: NOLOCK Side Effect .NET CONFERENCE #1 IN UKRAINE Demo
  36. 36. Extent N Allocation Order Scan … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M Clustered Index (Id) Extent 1 Index Allocation Map (IAM) Extent 1 Extent 2 Extent N
  37. 37. Тема доклада Тема доклада Тема доклада .NET LEVEL UP Summary .NET CONFERENCE #1 IN UKRAINE KYIV 2019 • Database Should Be Normalized • Each Table Should Have a Clustered Index • Column Datatypes Should Be as Small as Possible • Cost Threshold for Parallelism Should be Higher Than Default • Max Degree of Parallelism Should Be Set to Best Practices Value • Add “If it does make sense” after each of the statements above • Bonus: NOLOCK and Cursor Threshold
  38. 38. Тема доклада Тема доклада Тема доклада KYIV 2019 When SQL Server Best Practices do not work .NET CONFERENCE #1 IN UKRAINE 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

×