SQL Server Execution Plans Second Edition by Grant Fritchey SQL Server Execution Plans
Total Page:16
File Type:pdf, Size:1020Kb
SQL Handbooks SQL Server Execution Plans Second Edition By Grant Fritchey SQL Server Execution Plans Second Edition By Grant Fritchey Published by Simple Talk Publishing September 2012 First Edition 2008 Copyright Grant Fritchey 2012 ISBN: 978-1-906434-92-2 The right of Grant Fritchey to be identified as the author of this work has been asserted by him in accordance with the Copyright, Designs and Patents Act 1988 All rights reserved. No part of this publication may be reproduced, stored or introduced into a retrieval system, or transmitted, in any form, or by any means (electronic, mechanical, photocopying, recording or otherwise) without the prior written consent of the publisher. Any person who does any unauthorized act in relation to this publication may be liable to criminal prosecution and civil claims for damages. This book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, re-sold, hired out, or otherwise circulated without the publisher's prior consent in any form other than which it is published and without a similar condition including this condition being imposed on the subsequent publisher. Technical Reviewer: Brad McGehee Editors: Tony Davis and Brad McGehee Cover Image by Andy Martin Typeset by Peter Woodhouse & Gower Associates Table of Contents Introduction 13 Changes in This ___________________________________________ Second Edition 15 _________________________________________ Code Examples 16 _______________________________________________________ Chapter 1: Execution Plan Basics 18 What Happens When a Query is Submitted? _________________________ 19 _______________________________ Query parsing 19 _____________________________________________________ Algebrizer 20 ________________________________________________________ The query optimizer 21 ________________________________________________ Query execution 24 ___________________________________________________ Estimated and Actual Execution Plans 25 ____________________________________ Execution Plan Reuse 26 _________________________________________________ Clearing Plans from the Plan Cache 28 ______________________________________ Execution Plan Formats 29 _______________________________________________ Graphical plans 29 ____________________________________________________ Text plans 29 _________________________________________________________ XML plans 30 ________________________________________________________ Getting Started 31 _______________________________________________________ Permissions required to view execution plans 31 ____________________________ Working with graphical execution plans 32 ________________________________ Working with text execution plans 42 _____________________________________ Working with XML execution plans 46 ___________________________________ Interpreting XML plans 47 ______________________________________________ Retrieving Plans from the Cache Using Dynamic Management Objects 51 ________ Automating Plan Capture Using SQL Server Trace Events 53 ___________________ Execution plan events 54 _______________________________________________ Capturing a Showplan XML trace 56 ______________________________________ Why the actual and estimated execution plans might differ 59 _________________ Summary 61 ____________________________________________________________ Chapter 2: Graphical Execution Plans for Basic Queries 62 The Language of Graphical Execution Plans _____ 62 ______________________________ Some Single Table Queries 65 ______________________________________________ Clustered Index Scan 65 ________________________________________________ Clustered Index Seek 68 _______________________________________________ NonClustered Index Seek 70 ___________________________________________ Key Lookup 73 _______________________________________________________ Table Scan 79 ________________________________________________________ RID Lookup 80 ______________________________________________________ Table Joins 83 ___________________________________________________________ Hash Match join 86 __________________________________________________ Nested Loops join 89 _________________________________________________ Compute Scalar 92 ____________________________________________________ Merge Join 93 ________________________________________________________ Filtering Data 96 _______________________________________________________ Execution Plans with GROUP BY and ORDER BY 99 _________________________ Sort 99 _____________________________________________________________ Hash Match (aggregate) 103 _____________________________________________ Filter 104 ____________________________________________________________ A brief aside on rebinds and rewinds 105 __________________________________ Execution Plans for INSERT, UPDATE and DELETE Statements 108 _____________ INSERT statements 109 ________________________________________________ UPDATE statements 112 _______________________________________________ DELETE statements 114 _______________________________________________ Summary 114 ___________________________________________________________ Chapter 3: Text and XML Execution Plans for Basic Queries 116 Text Execution Plans __ 117 _________________________________________________ A text plan for a simple query 117 ________________________________________ A text plan for a slightly more complex query 121 ___________________________ XML Execution Plans 126 _________________________________________________ An estimated XML plan 127 _____________________________________________ An actual XML plan 134 ________________________________________________ Querying the XML 135 _________________________________________________ Summary 137 ________________________________________________________ Chapter 4: Understanding More Complex Query Plans 138 Stored procedures ______ 138 _________________________________________________ Using a sub-select 141 _________________________________________________ Derived tables using APPLY 145 _________________________________________ Common table expressions 149 __________________________________________ MERGE 154 _________________________________________________________ Views 159 ___________________________________________________________ Indexes 164 __________________________________________________________ Summary 176 ________________________________________________________ Chapter 5: Controlling Execution Plans with Hints 177 Query Hints _________ 178 _________________________________________________________ HASH|ORDER GROUP 178 ____________________________________________ MERGE |HASH |CONCAT UNION 182 ___________________________________ LOOP|MERGE|HASH JOIN 185 _________________________________________ FAST n 190 __________________________________________________________ FORCE ORDER 191 ___________________________________________________ MAXDOP 196 ________________________________________________________ OPTIMIZE FOR 199 __________________________________________________ PARAMETERIZATION SIMPLE|FORCED 205 _____________________________ RECOMPILE 205 _____________________________________________________ ROBUST PLAN 208 __________________________________________________ KEEP PLAN 209 _____________________________________________________ KEEPFIXED PLAN 210 _________________________________________________ EXPAND VIEWS 210 __________________________________________________ MAXRECURSION 212 _________________________________________________ USE PLAN 212 _______________________________________________________ Join Hints 212 ___________________________________________________________ LOOP 213 ___________________________________________________________ MERGE 216 _________________________________________________________ Table Hints 218 _________________________________________________________ Table hint syntax 218 __________________________________________________ NOEXPAND 219 _____________________________________________________ INDEX() 221 _________________________________________________________ FASTFIRSTROW 223 _________________________________________________ Summary 226 ___________________________________________________________ Chapter 6: Cursor Operations 227 Simple cursors ___________________________ 227 _______________________________________________________ Logical operators 229 __________________________________________________ Physical operators 237 _________________________________________________ More cursor operations 238 _______________________________________________ Static cursor 238 ______________________________________________________ Keyset cursor 243 _____________________________________________________ READ_ONLY cursor 246 _______________________________________________ Cursors and performance 247 _____________________________________________ Summary 254 ___________________________________________________________ Chapter 7: Special Datatypes and Execution Plans 255 XML _________ 255 _______________________________________________________________ FOR XML 257 ________________________________________________________ OPENXML 266 _______________________________________________________ XQuery 271 __________________________________________________________ Hierarchical Data 279 ____________________________________________________ Spatial Data 282 _________________________________________________________ Summary 286 __________________________________________________________ Chapter 8: Advanced Topics 287 Reading Large-scale Execution Plans ___________________________