SQL Server Deep Dive Denis Reznik Data Architect at Intapp
About me Kiev, Ukraine Data Architect at Intapp Inc. Microsoft MVP Leader of Kyiv SQL Server User Group Co-author of “SQL Server MVP Deep Dives vol. 2” SoftServe IT-Awards judge 2 |
SQL Server Network Interface
SQL Server Protocols Shared Memory TCP/IP Named Pipes VIA (Deprecated)
TDS Endpoints TCP/IP Shared Memory Named Pipes VIA DAC TCP/IP Note: TDS – Tabular Data Stream
Query Processing
Parser Parse Query Text Check base SQL syntax Check if plan is already in cache Returns ParseTree
Algebrizer Check that objects are exist and available in current execution context Define types for every node in ParseTree Returns QueryTree (Logical representation of the query)
Optimizer Goal – get the optimal execution plan Optimizer Query Tree Pre-Optimization Search for trivial plan Load statistics (optional) Simplification – syntax transformation
Optimizer Phase 0 Exploration – plan alternatives Return Transaction Processing plan Load statistics
Optimizer Phase 1 Next set of query rules Returns Quick Plan Estimate parallel plan (executed twice)
Optimizer Phase 2 Full optimization Returns Full Plan Indexed views
Index (B-Tree) - Seek … … 1.. 1M 1.. 2K 2K+1.. 4K 1M-2K.. 1M ,5K 1,5K+1..2K SELECT * FROM Users WHERE Id = 523
Index (B-Tree) - Scan … … 1.. 1M 1.. 2K 2K+1.. 4K 1M-2K.. 1M ,5K 1,5K+1..2K SELECT * FROM Users
Index (B-Tree) – Range Scan … … 1.. 1M 1.. 2K 2K+1.. 4K 1M-2K.. 1M ,5K 1,5K+1..2K SELECT * FROM Users WHERE Id BETWEEN 700 AND 1700
Statistics SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500
Executor Execute query using its Query Plan Ask Storage Engine for data
Demo SQL Server, are you kidding me?
Storage Engine
Buffer Pool Data Cache Database File Access Methods Buffer Manager Select Data
Modify Data Buffer Pool Data Cache Database File Access Methods Buffer Manager Transaction Log Log Manager Log Buffer
Lock Types - Shared S S S S X X
Lock Types - Exclusive X X X X S S
Lock Types - Update U U U U S S S S X X
Lock Types – Intent locks S S IS
S S S S S S … S S n n = 5000 Table S S if lock can’t be put on table, process will try this every n rows 12 Buffer Pool Memory for locks S S > 24% Lock Escalation
Demo Use NOLOCK carefully
SQL OS
Execution model Session Thread Pool Running Suspended Runnable Task(s) SchedulerLogical CPU
Demo Worker Threads Starvation
@denisreznik Thank You!