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

N
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
When SQL Server Best Practices do not work
.NET CONFERENCE #1 IN UKRAINE
Тема доклада
Тема доклада
Тема доклада
.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”
Тема доклада
Тема доклада
Тема доклада
.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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Database Should Be Normalized
.NET CONFERENCE #1 IN UKRAINE
.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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Database Should Be Normalized
.NET CONFERENCE #1 IN UKRAINE
Demo
Тема доклада
Тема доклада
Тема доклада
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.
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Each Table Should Have a Clustered Index
.NET CONFERENCE #1 IN UKRAINE
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
Clustered Index
…
…
1 .. 1M
1 .. 2K 2K+1 .. 4K 1M-2K .. 1M
1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
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
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Each Table Should Have a Clustered Index
.NET CONFERENCE #1 IN UKRAINE
Demo
Тема доклада
Тема доклада
Тема доклада
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Column Datatypes Should Be as Small as Possible
.NET CONFERENCE #1 IN UKRAINE
Тема доклада
Тема доклада
Тема доклада
.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)
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Column Datatypes Should Be as Small as Possible
.NET CONFERENCE #1 IN UKRAINE
Demo
.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
Тема доклада
Тема доклада
Тема доклада
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Cost Threshold for Parallelism Should be Higher Than Default
.NET CONFERENCE #1 IN UKRAINE
• 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
Optimizer Phase 0
Load Statistics
Exploration – Plan Alternatives
Estimated cost < 0.2
Return Transaction Processing Plan
Optimization
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
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!
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
)
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Cost Threshold for Parallelism Should be Higher Than Default
.NET CONFERENCE #1 IN UKRAINE
Demo
Тема доклада
Тема доклада
Тема доклада
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Max Degree of Parallelism Should Be Set to Best Practices Value
.NET CONFERENCE #1 IN UKRAINE
Parallel Query Execution
• Amdal’s Law
Thread 1
Thread 2
Thread 3
Thread 4
1s2s
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Max Degree of Parallelism Should Be Set to Best Practices Value
.NET CONFERENCE #1 IN UKRAINE
Demo
Тема доклада
Тема доклада
Тема доклада
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
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Bonus: NOLOCK Side Effect
.NET CONFERENCE #1 IN UKRAINE
Тема доклада
Тема доклада
Тема доклада
KYIV 2019
Bonus: NOLOCK Side Effect
.NET CONFERENCE #1 IN UKRAINE
Demo
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
Тема доклада
Тема доклада
Тема доклада
.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
Тема доклада
Тема доклада
Тема доклада
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
1 of 38

Recommended

SQL Server Deep Dive, Denis Reznik by
SQL Server Deep Dive, Denis ReznikSQL Server Deep Dive, Denis Reznik
SQL Server Deep Dive, Denis ReznikSigma Software
226 views40 slides
Growing in the Wild. The story by CUBRID Database Developers. by
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.CUBRID
6.4K views54 slides
Growing in the wild. The story by cubrid database developers (Esen Sagynov, E... by
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
2.6K views53 slides
SQL Server Deep Drive by
SQL Server Deep Drive SQL Server Deep Drive
SQL Server Deep Drive DataArt
155 views51 slides
Se2017 query-optimizer by
Se2017 query-optimizerSe2017 query-optimizer
Se2017 query-optimizerMary Prokhorova
63 views33 slides
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре... by
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре....NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...NETFest
212 views46 slides

More Related Content

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

DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ... by
DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...
DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...Karel Zikmund
373 views20 slides
Denis Reznik Data driven future by
Denis Reznik Data driven futureDenis Reznik Data driven future
Denis Reznik Data driven futureАліна Шепшелей
209 views20 slides
SE2016 BigData Denis Reznik "Data driven future" by
SE2016 BigData Denis Reznik "Data driven future"SE2016 BigData Denis Reznik "Data driven future"
SE2016 BigData Denis Reznik "Data driven future"Inhacking
203 views20 slides
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with... by
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...Spark Summit
4.5K views43 slides
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali... by
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali....NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...NETFest
182 views27 slides
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf... by
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf....NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf...
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf...Karel Zikmund
227 views21 slides

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

DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ... by Karel Zikmund
DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...
DotNext 2017 in Moscow - .NET Core Networking stack and Performance -- Karel ...
Karel Zikmund373 views
SE2016 BigData Denis Reznik "Data driven future" by Inhacking
SE2016 BigData Denis Reznik "Data driven future"SE2016 BigData Denis Reznik "Data driven future"
SE2016 BigData Denis Reznik "Data driven future"
Inhacking203 views
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with... by Spark Summit
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...
Apache Carbondata: An Indexed Columnar File Format for Interactive Query with...
Spark Summit4.5K views
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali... by NETFest
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali....NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
NETFest182 views
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf... by Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf....NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf...
.NET Core Summer event 2019 in Brno, CZ - .NET Core Networking stack and perf...
Karel Zikmund227 views
Scale by the Bay 2019 Reprogramming the Programmer by Paul Cleary
Scale by the Bay 2019 Reprogramming the ProgrammerScale by the Bay 2019 Reprogramming the Programmer
Scale by the Bay 2019 Reprogramming the Programmer
Paul Cleary218 views
An early look at the LDBC Social Network Benchmark's Business Intelligence wo... by Gábor Szárnyas
An early look at the LDBC Social Network Benchmark's Business Intelligence wo...An early look at the LDBC Social Network Benchmark's Business Intelligence wo...
An early look at the LDBC Social Network Benchmark's Business Intelligence wo...
Gábor Szárnyas641 views
Open Source North - MongoDB Advanced Schema Design Patterns by Matthew Kalan
Open Source North - MongoDB Advanced Schema Design PatternsOpen Source North - MongoDB Advanced Schema Design Patterns
Open Source North - MongoDB Advanced Schema Design Patterns
Matthew Kalan409 views
.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET by NETFest
.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET
.NET Fest 2019. Оля Гавриш. .NET Core 3.0 и будущее .NET
NETFest616 views
Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b... by Trivadis
Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b...Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b...
Trivadis TechEvent 2017 Querying distributed data with SQL and Apache Drill b...
Trivadis150 views
N1QL: What's new in Couchbase 5.0 by Keshav Murthy
N1QL: What's new in Couchbase 5.0N1QL: What's new in Couchbase 5.0
N1QL: What's new in Couchbase 5.0
Keshav Murthy1.2K views
Moving Toward Deep Learning Algorithms on HPCC Systems by HPCC Systems
Moving Toward Deep Learning Algorithms on HPCC SystemsMoving Toward Deep Learning Algorithms on HPCC Systems
Moving Toward Deep Learning Algorithms on HPCC Systems
HPCC Systems269 views
ScyllaDB V Developer Deep Dive Series: Resiliency and Strong Consistency via ... by ScyllaDB
ScyllaDB V Developer Deep Dive Series: Resiliency and Strong Consistency via ...ScyllaDB V Developer Deep Dive Series: Resiliency and Strong Consistency via ...
ScyllaDB V Developer Deep Dive Series: Resiliency and Strong Consistency via ...
ScyllaDB393 views
GraphTour - Neo4j Database Overview by Neo4j
GraphTour - Neo4j Database OverviewGraphTour - Neo4j Database Overview
GraphTour - Neo4j Database Overview
Neo4j547 views
Automated Slow Query Analysis: Dex the Index Robot by MongoDB
Automated Slow Query Analysis: Dex the Index RobotAutomated Slow Query Analysis: Dex the Index Robot
Automated Slow Query Analysis: Dex the Index Robot
MongoDB2.1K views
Conceptos básicos. Seminario web 1: Introducción a NoSQL by MongoDB
Conceptos básicos. Seminario web 1: Introducción a NoSQLConceptos básicos. Seminario web 1: Introducción a NoSQL
Conceptos básicos. Seminario web 1: Introducción a NoSQL
MongoDB10.7K views
Rockwell Automation TechED 2017 - AP05 - Enbridge Energy by Rockwell Automation
Rockwell Automation TechED 2017 - AP05 - Enbridge EnergyRockwell Automation TechED 2017 - AP05 - Enbridge Energy
Rockwell Automation TechED 2017 - AP05 - Enbridge Energy

More from NETFest

.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET by
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NETNETFest
705 views74 slides
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE... by
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE....NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...NETFest
341 views41 slides
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов by
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистовNETFest
516 views40 slides
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem... by
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem....NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...NETFest
254 views7 slides
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design by
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven DesignNETFest
1.5K views55 slides
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex by
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at WirexNETFest
356 views35 slides

More from NETFest(20)

.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET by NETFest
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET
.NET Fest 2019. Николай Балакин. Микрооптимизации в мире .NET
NETFest705 views
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE... by NETFest
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE....NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...
.NET Fest 2019. Сергей Калинец. Efficient Microservice Communication with .NE...
NETFest341 views
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов by NETFest
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов
.NET Fest 2019. Оля Гавриш. Машинное обучение для .NET программистов
NETFest516 views
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem... by NETFest
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem....NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...
.NET Fest 2019. Roberto Freato. Provisioning Azure PaaS fluently with Managem...
NETFest254 views
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design by NETFest
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
NETFest1.5K views
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex by NETFest
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex
.NET Fest 2019. Сергій Бута. Feature Toggles: Dynamic Configuration at Wirex
NETFest356 views
.NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A... by NETFest
.NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A....NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A...
.NET Fest 2019. Michael Staib. Hot Chocolate: GraphQL Schema Stitching with A...
NETFest1.6K views
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture by NETFest
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
NETFest326 views
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests by NETFest
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
NETFest224 views
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос... by NETFest
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос....NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
NETFest275 views
.NET Fest 2019. Roberto Freato. Azure App Service deep dive by NETFest
.NET Fest 2019. Roberto Freato. Azure App Service deep dive.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
NETFest197 views
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production by NETFest
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
NETFest250 views
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com... by NETFest
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com....NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
NETFest204 views
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real... by NETFest
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real....NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
NETFest453 views
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem by NETFest
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
NETFest263 views
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ... by NETFest
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ....NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
NETFest170 views
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET by NETFest
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
NETFest388 views
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur... by NETFest
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur....NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
NETFest243 views
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith... by NETFest
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith....NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
NETFest215 views
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI by NETFest
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
NETFest255 views

Recently uploaded

231112 (WR) v1 ChatGPT OEB 2023.pdf by
231112 (WR) v1  ChatGPT OEB 2023.pdf231112 (WR) v1  ChatGPT OEB 2023.pdf
231112 (WR) v1 ChatGPT OEB 2023.pdfWilfredRubens.com
137 views21 slides
STERILITY TEST.pptx by
STERILITY TEST.pptxSTERILITY TEST.pptx
STERILITY TEST.pptxAnupkumar Sharma
114 views9 slides
Student Voice by
Student Voice Student Voice
Student Voice Pooky Knightsmith
148 views33 slides
Google solution challenge..pptx by
Google solution challenge..pptxGoogle solution challenge..pptx
Google solution challenge..pptxChitreshGyanani1
82 views18 slides
Education and Diversity.pptx by
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptxDrHafizKosar
107 views16 slides
Narration ppt.pptx by
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptxTARIQ KHAN
110 views24 slides

Recently uploaded(20)

Education and Diversity.pptx by DrHafizKosar
Education and Diversity.pptxEducation and Diversity.pptx
Education and Diversity.pptx
DrHafizKosar107 views
Narration ppt.pptx by TARIQ KHAN
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
TARIQ KHAN110 views
Scope of Biochemistry.pptx by shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba121 views
Structure and Functions of Cell.pdf by Nithya Murugan
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan317 views
Class 10 English notes 23-24.pptx by TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN95 views
Psychology KS5 by WestHatch
Psychology KS5Psychology KS5
Psychology KS5
WestHatch68 views
Are we onboard yet University of Sussex.pptx by Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc71 views
Universe revised.pdf by DrHafizKosar
Universe revised.pdfUniverse revised.pdf
Universe revised.pdf
DrHafizKosar108 views
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx by Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard165 views
Community-led Open Access Publishing webinar.pptx by Jisc
Community-led Open Access Publishing webinar.pptxCommunity-led Open Access Publishing webinar.pptx
Community-led Open Access Publishing webinar.pptx
Jisc69 views
UWP OA Week Presentation (1).pptx by Jisc
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptx
Jisc68 views
Class 10 English lesson plans by TARIQ KHAN
Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plans
TARIQ KHAN239 views
11.28.23 Social Capital and Social Exclusion.pptx by mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239112 views

.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
  • 4. Тема доклада Тема доклада Тема доклада KYIV 2019 Database Should Be Normalized .NET CONFERENCE #1 IN UKRAINE
  • 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. Тема доклада Тема доклада Тема доклада KYIV 2019 Database Should Be Normalized .NET CONFERENCE #1 IN UKRAINE Demo
  • 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. Тема доклада Тема доклада Тема доклада KYIV 2019 Each Table Should Have a Clustered Index .NET CONFERENCE #1 IN UKRAINE
  • 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. Clustered Index … … 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K
  • 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
  • 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. 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. 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
  • 30. Parallel Query Execution • Amdal’s Law Thread 1 Thread 2 Thread 3 Thread 4 1s2s
  • 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
  • 34. Тема доклада Тема доклада Тема доклада KYIV 2019 Bonus: NOLOCK Side Effect .NET CONFERENCE #1 IN UKRAINE
  • 35. Тема доклада Тема доклада Тема доклада KYIV 2019 Bonus: NOLOCK Side Effect .NET CONFERENCE #1 IN UKRAINE Demo
  • 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