SQL Server 2008 Query Performance Tuning Distilled Grant Fritchey and Sajal Dam SQL Server 2008 Query Performance Tuning Distilled Copyright © 2009 by Grant Fritchey and Sajal Dam All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1902-6 ISBN-13 (electronic): 978-1-4302-1903-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jonathan Gennick Development Editor: Douglas Pundick Technical Reviewer: Joseph Sack Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editor: Kim Wimpsett Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Patrick Cunningham Proofreader: April Eddy Indexer: John Collin Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)ju<olnejcan)o^i*_ki, or visit dppl6++sss*olnejcankjheja*_ki. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail ejbk<]lnaoo*_ki, or visit dppl6++sss* ]lnaoo*_ki. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at dppl6++sss*]lnaoo*_ki+ejbk+^qhgo]hao. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi- rectly by the information contained in this work. The source code for this book is available to readers at dppl6++sss*]lnaoo*_ki. iii Contents at a Glance About the Author .xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Acknowledgments .xxiii Introduction xxv CHAPTER 1 SQL Query Performance Tuning 1 CHAPTER 2 System Performance Analysis 17 CHAPTER 3 SQL Query Performance Analysis .61 CHAPTER 4 Index Analysis 101 CHAPTER 5 Database Engine Tuning Advisor .151 CHAPTER 6 Bookmark Lookup Analysis 163 CHAPTER 7 Statistics Analysis .175 CHAPTER 8 Fragmentation Analysis .209 CHAPTER 9 Execution Plan Cache Analysis .241 CHAPTER 10 Stored Procedure Recompilation .283 CHAPTER 11 Query Design Analysis .313 CHAPTER 12 Blocking Analysis .351 CHAPTER 13 Deadlock Analysis .401 CHAPTER 14 Cursor Cost Analysis 415 CHAPTER 15 Database Workload Optimization .439 CHAPTER 16 SQL Server Optimization Checklist .475 INDEX .497 v Contents About the Author .xix About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Acknowledgments .xxiii Introduction xxv CHAPTER 1 SQL Query Performance Tuning .1 The Performance-Tuning Process 2 The Core Process 2 Iterating the Process .4 Performance vs. Price .7 Performance Targets .7 “Good Enough” Tuning .7 Performance Baseline .8 Where to Focus Efforts 9 SQL Server Performance Killers .10 Poor Indexing 11 Inaccurate Statistics 11 Excessive Blocking and Deadlocks 11 Non-Set-Based Operations .12 Poor Query Design 12 Poor Database Design .12 Excessive Fragmentation 13 Nonreusable Execution Plans .13 Poor Execution Plans 13 Frequent Recompilation of Execution Plans 14 Improper Use of Cursors .14 Improper Configuration of the Database Log .14 Excessive Use or Improper Configuration of tempdb .14 Summary .15 N CONTENTS vi CHAPTER 2 System Performance Analysis .17 Performance Monitor Tool 17 Dynamic Management Views .19 Hardware Resource Bottlenecks .20 Identifying Bottlenecks 20 Bottleneck Resolution .21 Memory Bottleneck Analysis 21 SQL Server Memory Management 22 Available Bytes .25 Pages/sec and Page Faults/sec Counters 25 Buffer Cache Hit Ratio .26 Page Life Expectancy .26 Checkpoint Pages/sec .27 Lazy writes/sec .27 Memory Grants Pending .27 Target Server Memory (KB) and Total Server Memory (KB) .27 Memory Bottleneck Resolutions .27 Optimizing Application Workload 29 Allocating More Memory to SQL Server 29 Increasing System Memory 29 Changing from a 32-bit to a 64-bit Processor 29 Enabling 3GB of Process Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Using Memory Beyond 4GB Within SQL Server .30 Disk Bottleneck Analysis .32 Disk Counters 32 % Disk Time .33 Current Disk Queue Length 33 Disk Transfers/sec 34 Disk Bytes/sec .34 Avg. Disk Sec/Read and Avg. Disk Sec/Write .34 Disk Bottleneck Resolutions 35 Optimizing Application Workload 35 Using a Faster Disk Drive 35 Using a RAID Array 35 Using a SAN System 37 Aligning Disks Properly 38 Using a Battery-Backed Controller Cache 38 Adding System Memory .38 N CONTENTS vii Creating Multiple Files and Filegroups .39 Placing the Table and Index on Separate Disks .42 Saving Log Files to a Separate Physical Disk .42 Partitioning Tables 43 Processor Bottleneck Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 % Processor Time 44 % Privileged Time 44 Processor Queue Length .44 Context Switches/sec .44 Batch Requests/sec .45 SQL Compilations/sec .45 SQL Recompilations/sec .45 Processor Bottleneck Resolutions 45 Optimizing Application Workload 45 Eliminating Excessive Compiles/Recompiles .46 Using More or Faster Processors .46 Using a Large L2/L3 Cache 46 Running More Efficient Controllers/Drivers .47 Not Running Unnecessary Software .47 Network Bottleneck Analysis 47 Bytes Total/sec .48 % Net Utilization 48 Network Bottleneck Resolutions .48 Optimizing Application Workload 48 Adding Network Adapters 49 Moderating and Avoiding Interruptions 49 SQL Server Overall Performance .49 Missing Indexes 50 Database Blocking 51 Nonreusable Execution Plans .52 General Behavior .52 Creating a Baseline 53 Creating a Reusable List of Performance Counters 53 Creating a Counter Log Using the List of Performance Counters 56 Minimizing Performance Monitor Overhead 57 System Behavior Analysis Against Baseline .59 Summary .60 N CONTENTS viii CHAPTER 3 SQL Query Performance Analysis 61 The SQL Profiler Tool 61 Profiler Traces .62 Events 63 Data Columns 66 Filters .67 Trace Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Trace Data .69 Trace Automation .70 Capturing a Trace Using the GUI 70 Capturing a Trace Using Stored Procedures 71 Combining Trace and Performance Monitor Output 72 SQL Profiler Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Limiting the Number of Events and Data Columns 73 Discarding Start Events for Performance Analysis .74 Limiting the Trace Output Size .75 Avoiding Online Data Column Sorting 75 Running Profiler Remotely .75 Limiting the Use of Certain Events 75 Query Performance Metrics Without Profiler .76 Costly Queries 76 Identifying Costly Queries 77 Identifying Slow-Running Queries .82 Execution Plans .83 Analyzing a Query Execution Plan .84 Identifying the Costly Steps in an Execution Plan 87 Analyzing Index Effectiveness 87 Analyzing Join Effectiveness .89 Actual vs. Estimated Execution Plans 93 Plan Cache 95 Query Cost .95 Client Statistics .96 Execution Time .97 STATISTICS IO 98 Summary 100 CHAPTER 4 Index Analysis 101 What Is an Index? 101 The Benefit of Indexes 103 Index Overhead 105 N CONTENTS ix Index Design Recommendations 107 Examine the WHERE Clause and Join Criteria Columns .107 Use Narrow Indexes 109 Examine Column Uniqueness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Examine the Column Data Type .114 Consider Column Order .114 Consider the Type of Index 117 Clustered Indexes 117 Heap Tables 118 Relationship with Nonclustered Indexes 118 Clustered Index Recommendations 120 Nonclustered Indexes .126 Nonclustered Index Maintenance 127 Defining the Bookmark Lookup 127 Nonclustered Index Recommendations .127 Clustered vs. Nonclustered Indexes .128 Benefits of a Clustered Index over a Nonclustered Index 129 Benefits of a Nonclustered Index over a Clustered Index 131 Advanced Indexing Techniques .132 Covering Indexes 132 Index Intersections .135 Index Joins .137 Filtered Indexes .138 Indexed Views .141 Index Compression 145 Special Index Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Full-Text 147 Spatial .148 XML .148 Additional Characteristics of Indexes 148 Different Column Sort Order .148 Index on Computed Columns 148 Index on BIT Data Type Columns 149 CREATE INDEX Statement Processed As a Query .149 Parallel Index Creation .149 Online Index Creation 150 Considering the Database Engine Tuning Advisor 150 Summary 150 N CONTENTS x CHAPTER 5 Database Engine Tuning Advisor .151 Database Engine Tuning Advisor Mechanisms 151 Database Engine Tuning Advisor Examples 155 Tuning a Query 155 Tuning a Trace Workload .159 Database Engine Tuning Advisor Limitations .161 Summary 162 CHAPTER 6 Bookmark Lookup Analysis .163 Purpose of Bookmark Lookups .163 Drawbacks of Bookmark Lookups 165 Analyzing the Cause of a Bookmark Lookup 166 Resolving Bookmark Lookups 169 Using a Clustered Index 169 Using a Covering Index .169 Using an Index Join 173 Summary 174 CHAPTER 7 Statistics Analysis .175 The Role of Statistics in Query Optimization 175 Statistics on an Indexed Column 176 Benefits of Updated Statistics .177 Drawbacks of Outdated Statistics 179 Statistics on a Nonindexed Column .180 Benefits of Statistics on a Nonindexed Column 181 Drawback of Missing Statistics on a Nonindexed Column .185 Analyzing Statistics .187 Density .190 Statistics on a Multicolumn Index 191 Statistics on a Filtered Index 192 Statistics Maintenance 193 Automatic Maintenance 193 Manual Maintenance .195 Statistics Maintenance Status .198 Analyzing the Effectiveness of Statistics for a Query 199 Resolving a Missing Statistics Issue 199 Resolving an Outdated Statistics Issue .202 [...]... Field Engineering team Since 1997, he has been developing and supporting SQL Server environments for clients in the financial services, IT consulting, manufacturing, retail, and real estate industries He is the author of SQL Server 2008 Transact -SQL Recipes (Apress, 2008) , SQL Server 2005 T -SQL Recipes (Apress, 2005), and SQL Server 2000 Fast Answers for DBAs and Developers (Glasshaus, 2003) xxi Acknowledgments... Chapter 3 SQL Server Performance Killers Let’s now consider the major problem areas that can degrade SQL Server performance By being aware of the main performance killers in SQL Server in advance, you will be able to focus your tuning efforts on the likely causes Once you have optimized the hardware, operating system, and SQL Server settings, the ing first): CHAPTER 1 SQL QUERY PERFORMANCE TUNING Let’s... query performance tuning Hardware performance is constantly improving, which can lead to an attitude suggesting that other methods of performance tuning are no longer important Upgrades to SQL Server especially to the optimizer, which helps determine how a query is executed, and the query engine, which executes the query lead to better performance all on their own The beauty of query performance tuning. .. system performance Figure 1-1 Performance- tuning process 5 6 CHAPTER 1 SQ L QU ER Y P ER FOR MA NC E TU NING You can see that the steps to optimize the costliest query make for a complex process, which also requires multiple iterations to troubleshoot the performance issues within the optimization of the costliest query Figure 1-2 Optimization of the costliest query CHAPTER 1 SQL QUERY PERFORMANCE TUNING. .. efficiently, it is worthwhile to quana certain query, with no adverse effect anywhere else on the server) and then work toward them CHAPTER 1 SQL QUERY PERFORMANCE TUNING The performance of a SQL Server application is highly dependent on the amount and workload and data change over time, and differing data can cause SQL Server to execute SQL queries differently The performance resolution applicable for a... ) Most of the code is straight T -SQL stored in files, which can be opened and used in any SQL Server T -SQL editing tool There is one PowerShell script that will have to be run through a PowerShell command line Contacting the Author You can contact the author, Grant Fritchey, at You can visit his blog at xxvii CHAPTER 1 SQL Query Performance Tuning Q uery performance tuning is an important part of... least as well as they know T -SQL Inaccurate Statistics SQL Server relies heavily on cost-based optimization, so accurate data-distribution statisServer’s built-in query optimizer cannot accurately estimate the number of rows affected by a query Because the amount of data to be retrieved from a table is highly important in deciding how to optimize the query execution, the query optimizer is much less... depth in later chapters Poor Indexing usually one of the biggest performance killers in SQL Server In the absence of proper indexing for a query, SQL Server has to retrieve and process much more data while increasing the query execution time significantly Increased query execution time then leads to excessive blocking and deadlocks in SQL Server You will learn how to determine the indexing strategies... performance may suffer Let’s briefly examine these factors Having another resource-intensive application on the same server can limit the resources the system resources and limit the resources available to SQL Server For example, running CHAPTER 1 SQL QUERY PERFORMANCE TUNING , which runs at a higher priority than the SQL Server process Priority is the weight given to a resource that pushes the processor... depth in Non-Set-Based Operations Transact -SQL is a set-based scripting language, which means it operates on sets of data This forces you to think in terms of columns rather than in terms of rows Non-set-based thinking leads to excessive use of cursors and loops rather than exploring more efficient joins and subqueries The T -SQL language offers rich mechanisms for manipulating sets of data For performance . SQL Server 2008 Query Performance Tuning Distilled Grant Fritchey and Sajal Dam SQL Server 2008 Query Performance Tuning Distilled Copyright. industries. He is the author of SQL Server 2008 Transact -SQL Recipes (Apress, 2008) , SQL Server 2005 T -SQL Recipes (Apress, 2005), and SQL Server 2000 Fast Answers