The document discusses best practices for SQL Server, including normalizing databases, adding clustered indexes to tables, using optimal column datatypes, adjusting parallelism settings, and understanding the effects of NOLOCK hints. It provides examples of when these typically recommended practices may not be applicable, emphasizing that guidelines should be evaluated on a case-by-case basis depending on the specific database and workload.
4.16.24 21st Century Movements for Black Lives.pptx
.NET Fest 2019. Денис Резник. Когда SQL Server Best Practices не работают
1. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
When SQL Server Best Practices do not work
.NET CONFERENCE #1 IN UKRAINE
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. Тема доклада
Тема доклада
Тема доклада
.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
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
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.
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. 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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Each Table Should Have a Clustered Index
.NET CONFERENCE #1 IN UKRAINE
Demo
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Column Datatypes Should Be as Small as Possible
.NET CONFERENCE #1 IN UKRAINE
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Column Datatypes Should Be as Small as Possible
.NET CONFERENCE #1 IN UKRAINE
Demo
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. Тема доклада
Тема доклада
Тема доклада
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Cost Threshold for Parallelism Should be Higher Than Default
.NET CONFERENCE #1 IN UKRAINE
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. Optimizer Phase 0
Load Statistics
Exploration – Plan Alternatives
Estimated cost < 0.2
Return Transaction Processing Plan
Optimization
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
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Cost Threshold for Parallelism Should be Higher Than Default
.NET CONFERENCE #1 IN UKRAINE
Demo
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Max Degree of Parallelism Should Be Set to Best Practices Value
.NET CONFERENCE #1 IN UKRAINE
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. Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Max Degree of Parallelism Should Be Set to Best Practices Value
.NET CONFERENCE #1 IN UKRAINE
Demo
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
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. Тема доклада
Тема доклада
Тема доклада
.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. Тема доклада
Тема доклада
Тема доклада
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