Dissecting SQL Server Execution Plans - SQLServerCentral.com

3 downloads 65 Views 162KB Size Report
The Art of High Performance. SQL Code, Volume 1: Dissecting SQL Server Execution Plans by Grant Fritchey. First published 2008 by Simple-Talk Publishing ...
The Art of High Performance SQL Code, Volume 1:

Dissecting SQL Server Execution Plans

by Grant Fritchey First published 2008 by Simple-Talk Publishing

iii

CONTENTS Contents .................................................................................................................................................... 3 About the author...................................................................................................................................... 7 acknowledgements................................................................................................................................... 8 Introduction.............................................................................................................................................. 9 Foreword .................................................................................................................................................11 Chapter 1: Execution Plan Basics .......................................................................................................12 What Happens When a Query is Submitted? .........................................................................12 Query Parsing...........................................................................................................................12 The Query Optimizer .............................................................................................................13 Query Execution......................................................................................................................14 Estimated and Actual Execution Plans................................................................................14 Execution Plan Reuse .............................................................................................................14 Why the Actual and Estimated Execution Plans Might Differ........................................16 Execution Plan Formats .........................................................................................................17 Graphical Plans ........................................................................................................................17 Text Plans..................................................................................................................................17 XML Plans................................................................................................................................17 Getting Started .............................................................................................................................17 Sample Code.............................................................................................................................18 Permissions Required to View Execution Plans.................................................................18 Working with Graphical Execution Plans................................................................................18 Getting the Estimated Plan....................................................................................................19 Getting the Actual Plan ..........................................................................................................19 Interpreting Graphical Execution Plans ..............................................................................20 Working with Text Execution Plans .........................................................................................23 Getting the Estimated Text Plan...........................................................................................24 Getting the Actual Text Plan .................................................................................................24 Interpreting Text Plans ...........................................................................................................24 Working with XML Execution Plans........................................................................................25 Getting the Actual and Estimated XML Plans...................................................................25 Interpreting XML Plans..............................................................................................................26 Saving XML Plans as Graphical Plans .................................................................................28 Automating Plan Capture Using SQL Server Profiler...........................................................28 Execution Plan events.............................................................................................................29 Capturing a Showplan XML Trace .......................................................................................29

iv Summary........................................................................................................................................32 Chapter 2: Reading Graphical Execution Plans for Basic Queries ................................................33 The Language of Graphical Execution Plans .........................................................................33 Some Single table Queries ..........................................................................................................34 Clustered Index Scan...............................................................................................................34 Clustered Index Seek...............................................................................................................35 Non-clustered Index Seek ......................................................................................................37 Key LookUp .............................................................................................................................38 Table Scan .................................................................................................................................42 RID LookUp ............................................................................................................................44 Table Joins.....................................................................................................................................46 Hash Match (Join)....................................................................................................................48 Clustered Index Seek...............................................................................................................50 Nested Loops Join...................................................................................................................51 Compute Scalar ........................................................................................................................51 Merge Join.................................................................................................................................52 Adding a WHERE Clause ..........................................................................................................54 Execution Plans with GROUP BY and ORDER BY ...........................................................56 Sort.............................................................................................................................................56 Hash Match (Aggregate).........................................................................................................58 Filter...........................................................................................................................................59 Rebinds and Rewinds Explained ...............................................................................................60 Insert, Update and Delete Execution Plans.............................................................................62 Insert Statements .....................................................................................................................62 Update Statements...................................................................................................................63 Delete Statements ....................................................................................................................64 Summary........................................................................................................................................66 Chapter 3: Text and XML Execution Plans for Basic Queries.......................................................67 Text Execution Plans...................................................................................................................67 A Text Plan for a Simple Query ............................................................................................67 A Slightly more Complex Query...........................................................................................69 XML Execution Plans .................................................................................................................73 An Estimated XML Plan ........................................................................................................74 An Actual XML Plan ..............................................................................................................77 Summary........................................................................................................................................77 Chapter 4: Understanding More Complex Query Plans..................................................................79 Stored Procedures........................................................................................................................79 Derived Tables..............................................................................................................................80

v A Subselect without a Derived Table ...................................................................................80 A Derived Table using APPLY .............................................................................................83 Common Table Expressions......................................................................................................86 Views..............................................................................................................................................88 Standard Views ........................................................................................................................88 Indexed Views..........................................................................................................................89 Indexes...........................................................................................................................................91 Included Indexes: Avoiding Bookmark Lookups...............................................................91 Index Selectivity.......................................................................................................................94 Statistics and Indexes ..............................................................................................................96 Summary .......................................................................................................................................97 Chapter 5: Controlling Execution Plans with Hints.........................................................................99 Query Hints ..................................................................................................................................99 HASH|ORDER GROUP.....................................................................................................99 MERGE |HASH |CONCAT UNION ...........................................................................101 LOOP|MERGE|HASH JOIN .........................................................................................102 FAST n ....................................................................................................................................105 FORCE ORDER ..................................................................................................................106 MAXDOP ..............................................................................................................................107 OPTIMIZE FOR..................................................................................................................109 PARAMETERIZATION SIMPLE|FORCED ..............................................................112 RECOMPILE ........................................................................................................................112 ROBUST PLAN....................................................................................................................114 KEEP PLAN .........................................................................................................................114 KEEPFIXED PLAN ...........................................................................................................114 EXPAND VIEWS ................................................................................................................114 MAXRECURSION ..............................................................................................................115 USE PLAN.............................................................................................................................115 Join Hints ....................................................................................................................................116 Table Hints..................................................................................................................................118 Table Hint Syntax ..................................................................................................................119 NOEXPAND ........................................................................................................................119 INDEX().................................................................................................................................120 FASTFIRSTROW .................................................................................................................121 Summary .....................................................................................................................................123 Chapter 6: Cursor Operations ...........................................................................................................124 Simple Cursors ...........................................................................................................................124

vi Logical Operators ..................................................................................................................125 Physical Operators.................................................................................................................130 More Cursor Operations...........................................................................................................130 STATIC Cursor......................................................................................................................130 KEYSET Cursor....................................................................................................................133 READ_ONLY Cursor..........................................................................................................135 Cursors and Performance.........................................................................................................135 Summary......................................................................................................................................140 Chapter 7: XML in Execution Plans.................................................................................................141 FOR XML...................................................................................................................................141 OPENXML ................................................................................................................................146 XQuery ........................................................................................................................................149 Using the exist method .........................................................................................................149 Using the query method .......................................................................................................151 Summary......................................................................................................................................152 Chapter 8: Advanced Topics ..............................................................................................................154 Reading Large Scale Execution Plans .....................................................................................154 Parallelism in Execution Plans .................................................................................................158 Max Degree of Parallelism...................................................................................................158 Cost Threshold for Parallelism............................................................................................159 Are Parallel Plans Good or Bad?.........................................................................................160 Examining a Parallel Execution Plan..................................................................................160 How Forced Parameterization affects Execution Plans.......................................................163 Using Plan Guides to Modify Execution Plans.....................................................................166 Object Plan Guides ...............................................................................................................166 SQL Plan Guides ...................................................................................................................167 Template Plan Guides ...........................................................................................................168 Plan Guide Administration ..................................................................................................169 Summary .................................................................................................................................169 Using Plan Forcing to Modify Execution Plans....................................................................169 Summary......................................................................................................................................171 Index ......................................................................................................................................................172