SQL Server DMVs in Action Better Queries with Dynamic Management Views IANW. STIRK II MANNING Shelter Island
contents preface xix acknowledgements about this book xxii xx Part 1 Starting the journey 1 J The Dynamic Management Views gold mine 3 1.1 What are Dynamic Management Views? 4 A glimpse into SQL Server's internal data 7 Aggregated results 8 * Impact ofrunning DMVs 8 Part ofsql Server 2005 onward 8 1.2 The problems DMVs can solve 9 Diagnosing problems 9* Performance tuning 10 Monitoring 10 1.3 DMV examples 13 Find your slowest queries 14* Find those missing indexes 15 Identify what SQL running now 17* Quickly find a cached plan 18 1.4 Preparing to use DMVs 22 Permissions 22 * ClearingDMVs 22 statements are ix
X CONTENTS 1.5 DMV companions 23 1.6 Working Catalog views 24 Cached plans 24 Indexes 25 Statistics 25 with DMVs 26 In context with other tools 26 Self-healing database 28" Reporting and transactional databases 29 1.7 Summary 30 f~) Common patterns 31 2.1 Reducing blocking 32 2.2 Using CROSS APPLY 32 2.3 Restricting output to a given database 32 2.4 Restricting output by using the TOP command 33 2.5 Creating an empty temporary table structure 34 2.6 Looping over the databases on a server 35 2.7 Retrieving a query's cached plan and SQL text 37 2.8 Extracting the Individual Query from the Parent Query 38 2.9 Identifying the database used by ad hoc queries 39 2.10 Calculating DMV changes 40 2.11 Reading cached plans 43 Targeting the area of concern 43 Things to look out for 44 2.12 Building dynamic SQL 47 2.13 Printing the content of large variables 48 2.14 Common terms and acronyms 49 2.15 Known problems that may affect the scripts 50 SQL Server compatibility An OFFLINE database 50 2.16 Summary 51 level set to below 2005 50 jpart 2 D3N^L\^»««*»a«*««»««**»»»»«««a«*»*i)%^ D Index DMVs 55 S 3.1 The importance of indexes 56 Types of index 56* Types of index access 57 Factors affecting index performance 58
3.2 Costly missing indexes 62 Finding the most important missing indexes 62 The impact of missing indexes 64 3.3 Unused indexes 65 Finding the most-costly unused indexes 66 The impact of unused indexes 68 3.4 High-maintenance indexes 69 Finding the top high-maintenance indexes 69 The impact of high-maintenance indexes 71 3.5 Most-frequently used indexes 72 Finding the most-used indexes 72 The importance of the most-used indexes 74 3.6 Fragmented indexes 75 Finding the most-fragmented indexes 75 The impact offragmented indexes 77 3.7 Indexes used by a given routine 78 Finding the indexes used by a given routine 78 The importance ofknowing which indexes are used 81 3.8 Databases with most missing indexes 83 Finding which databases have the most missing indexes 84 The importance of other databases 84 3.9 Completely unused indexes 85 Finding which indexes aren't used at all 85 " The importance of unused indexes 87 3.10 Your statistics 87 Finding the state ofyour statistics 88* The importance of statistics 89 3.11 A holistic approach to managing 3.12 Summary 91 indexes 90 Improving poor query performance 92 4.1 Understanding executed queries 93 Aggregated results 93 Clearing the cached plans 93 4.2 Finding a cached plan 94 How to find a cached plan 94
4.3 Finding where a query Identifying where a query 4.4 Long-running queries 98 is used 97 is used 97 Finding the queries that take the longest The impact of long-running queries 101 4.5 Queries that spend a long time being time to run 99 Finding the queries that spend the longest time being 4.6 CPU-intensive queries 106 Finding the queries 4.7 I/O-hungry queries 108 that use the most CPU 106 blocked 103 Finding the queries that use the most I/O 109 Reducing the impact of queries that use the most I/O 110 4.8 Frequently executed queries 111 blocked 104 Finding the queries that have been executed the most often 111 Reducing the impact of queries that are executed most often 113 4.9 The last run of a query 114 Determining when a query was last run 114 Variations on searchingfor queries 115 4.10 Summary 116 Further query improvements 118 5.1 Queries with missing statistics 119 Finding queries that have missing statistics 119* The importance of statistics 121 Default statistics properties 122 5.2 Finding queries that have missing indexes 123 5.3 Finding queries that have implicit data type conversions 124 Finding implicit data conversions 125 Finding disparate column data types 125 5.4 Finding queries that have table scans 127 5.5 Slower-than-normal queries 127 Finding queries that are running slower than normal 128 The importance of queries that are running slower than normal 131 5.6 Unused stored procedures (2008 only) 132 Finding unused stored procedures 133
xiii 5.7 Looking for SQL queries run during a specific interval 134 What runs over a given time period f 134 5.8 Relationships between DMV snapshot deltas 137 Amalgamated DMV snapshots 13 7 5.9 Currently running queries 142 What's running now? 142 5.10 Recompiled queries 144 Finding the most-decompiled queries 144 5.11 Summary 146 Operating system 6.1 Understanding 6.2 Identifying your DMVs 147 server waits 148 most common waits 148 Why are you waiting? 149* Common wait types 151 6.3 Identifying your most common waits snapshot version 152 Why are you waiting? (snapshot version) 153 6.4 Identifying why queries wait 154 Discovering why your queries are waiting 154 6.5 Queries that are waiting 157 6.6 Finding what's blocking running SQL 158 What's blocking my SQL query? 158 6.7 SQL Server performance counters 160 Important non-sql performance counters 162 6.8 Effect of running SQL queries on the performance counters 164 6.9 How performance counters and wait states relate 165 6.10 SQL queries and how they change the performance counters and wait states 168 6.11 Correlating wait states and performance counters 171 6.12 Capturing DMV data periodically 172 6.13 Summary 173
Common Language 7.1 Introducing Runtime DMVs 174 the CLR 175 7.2 A simple CLR example 176 Creating a simple CLR class 176 Using the SQL CLR regular expression functions 179 7.3.NET Framework performance 7.4 Time-consuming CLR queries 183 concerns 181 Finding the queries that spend Impact of time-consuming CLR queries 185 7.5 Queries spending (snapshot version) 186 Finding queries that spend (snapshot version) 186 7.6 Relationships the most time in the CLR 183 the most time in the CLR the most time in the CLR between CLR DMVs and other DMVs 188 7.7 Getting information about SQL Server CLR integration 192 7.8 Getting information about your SQL CLR assemblies 193 7.9 Summary 195 Resolving transaction issues 196 8.1 Transaction overview 197 8.2 A simple transaction-based case study 198 8.3 Locks, blocks, and deadlocks 200 Locks 200 ' Blocks 202 " Deadlocks 203 8.4 The ACID properties of transactions 205 8.5 Transaction isolation levels 205 8.6 Sessions, connections, and requests 206 8.7 Finding 8.8 Identifying locks 207 the contended resources 209 Contended resources basic version 209 Contended resources enhanced version 210 8.9 Identifying inactive sessions with open transactions 212 How idle sessions with open transactions arise 213 * How to find an idle session with an open transaction 213
xv 8.10 Waiting due to transaction locks 214 Waiting because of an idle session with an open transaction 215* Waiting because of active session transactions only 217 * Waiting because of both active and idle session transactions 218 8.11 Queries waiting 8.12 Lock escalation 220 8.13 How to reduce blocking 221 8.14 How to reduce deadlocks 224 8.15 Summary 225 for more than 30 seconds 219 Database-level DMVs 226 9.1 Space usage in tempdb 227 What is tempdbl 227 * Total, free, and used space in tempdb 228 * Tempdb total space usage by object type 230 9.2 Session usage in tempdb 231 Session usage of tempdb space 232 Space used and not reclaimed in tempdb by session 233 9.3 Task usage in tempdb 235 9.4 Tempdb Space used by running SQL queries 235 * Space reclaimed by active SQL queries 237 recommendations 239 9.5 Index contention 240 Indexes under row-locking pressure 241 Escalated indexes 243 * Unsuccessful index-lock promotions 244 Indexes with the most used and not page splits 245 * Indexes with most latch contention 247 Indexes with most page I/O-latch contention 248 * Indexes under row-locking pressure snapshot version 249 * How many rows are being inserted/deleted/updated/selected? 252 9.6 Summary 256 The self-healing 10.1 Self-healing 10.2 Recompiling database 257 database 258 slow routines 259 Recompiling routines that are running slower than usual 259
10.3 Automatically rebuild and reorganize Rebuilding and reorganizingfragmented 10.4 Intelligently update statistics 268 indexes 264 indexes 264 Simple intelligent statistics update 269 * Time-based intelligent statistics update 272 10.5 Automatically updating 10.6 Automatically implement missing Implementing missing indexes 2 79 a routine's statistics 276 indexes 279 10.7 Automatically disable or drop unused indexes 281 Disabling or dropping unused indexes 282 10.8 Summary 284 Useful scripts 285 11.1 Viewing everyone's last-run SQL query 286 Find the last-run queries 286 11.2 A generic performance test harness 287 Using the generic performance test harness 288 11.3 Determining the impact of a system upgrade 290 Quantifying system upgrade impact 290 11.4 Estimating the finishing time of system jobs 293 Estimating when a job will end 294 11.5 Get system information from within SQL Server 295 11.6 Viewing enabled Enterprise features (2008 only) 297 11.7 Who's doing what and when? 298 11.8 Finding where your query really spends its time 300 Locating where your queries are spending their time 300 11.9 Memory usage per database 303 Determining the memory used per database 303 11.10 Memory usage by table or index 304 Determining the memory used by tables and indexes 304 11.11 Finding I/O waits 307 I/O waits at the database level 307» I/O waits at the file level 308 * Average read/write times per file, per database 310
xvii 11.12 A simple lightweight trace utility 311 11.13 Some best practices 313 11.14 Where to start with performance problems 314 Starting with a slow server or database 314 * Starting with slow queries 315 11.15 Summary 316 index 317