SQL Server Performance Tuning Old-School Practical Guide Denis Reznik Data Architect at Intapp Microsoft Data Platform MVP
Sponsors Gold sponsors: Silver sponsors: Bronze sponsors:
About me Denis Reznik Kyiv, Ukraine Data Architect at Intapp Microsoft Data Platform MVP Co-Founder of Ukrainian Data Community Kyiv Co-author of “SQL Server MVP Deep Dives 2”
Agenda Data Collection Data Analysis Query Optimization 4 |
SQL Server Management Studio Tools SQL Profiler Performance Monitor SQL Server Management Studio
Indexes and Statistics Quick Overview
Heap 1 .. 100 1K .. 5K 12K .. 15K 22K .. 41K 7K .. 8K 51K .. 71K 100 .. 1k 6K .. 7K 10K .. 11K 9K .. 10K 8K .. 9K 1M .. 2M 5K .. 6K 15K .. 21K 21K .. 22K 41K .. 51K 71K .. 1M 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 SELECT * FROM Users WHERE Id = 523 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M … 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K …
… … Index Scan SELECT * FROM Users 1 .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M … 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K …
… … Index Range Scan SELECT * FROM Users WHERE Id BETWEEN 700 AND 1700 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M … 1 .. 300 301 .. 800 801 .. 1,5K 1,5K+1 .. 2K …
… … Non-Clustered Index Non-Clustered Index (Name) SELECT * FROM Users A .. Z SELECT * FROM Users WHERE Name = 'John Dow' A .. C C .. K X .. Z … Clustered Index (Id) 1 .. 1M Heap 1 .. 2K 2K .. 4K 1M-2K .. 1M 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M …
Index Allocation Map (IAM) Allocation Order Scan Index Allocation Map (IAM) Extent 1 Extent 2 Clustered Index (Id) Extent N 1 .. 1M Extent 1 Extent N 1 .. 2K 2K+1 .. 4K 1M-2K .. 1M …
Statistics SELECT * FROM Users WHERE Id BETWEEN 2100 AND 2500 800 2000 2800 4500 5400
Parameter Sniffing and Dynamic SQL Quick Overview
Parameter Sniffing - Stored Procedure Query Processor EXEC AnnualSalesReport @UserId = 1 EXEC AnnualSalesReport @UserId = 22 SQL Server Cache Procedure cache Procedure cache Query executes using the query plan created for @UserId = 1 Plan created and cached for the @UserId = 1
Parameter Sniffing - Parametrized Query Query Processor sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@Id int', 1 SELECT * FROM Users WHERE Id = @Id SELECT * FROM Users WHERE Id = @Id sp_executesql N'SELECT * FROM Users WHERE Id = @Id', N'@Id int', 22 SQL Server Cache Procedure cache Procedure cache Plan created and cached for the @Id = 1 Query executes using the query plan created for @Id = 1
Dynamic SQL – Multiple Plans Query Processor SELECT * FROM Users WHERE Id = 1 SELECT * FROM Users WHERE Id = 22 SQL Server Cache SELECT * FROM Users WHERE Id = 1 Procedure cache Procedure cache Procedure cache Query executed using the query plan, created for the first query. New query plan created and cached. Query executed using newly created plan. New query plan again created and cached. Query executed using newly created plan.
Summary Data Collection Data Analysis Query Optimization 19 |
Sponsors Gold sponsors: Silver sponsors: Bronze sponsors:
Thank You! Denis Reznik @denisreznik denisreznik@live.ru