SQL Server Execution Plans Second Edition pot

333 3.4K 1
SQL Server Execution Plans Second Edition pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 ___________________________________ 288 Parallelism in Execution Plans _________________________________________295 Max degree of parallelism ___________________________________________296 Cost threshold for parallelism ________________________________________297 Are parallel plans good or bad? _______________________________________298 Examining a parallel execution plan __________________________________ 299 How Forced Parameterization Affects Execution Plans _____________________305 Using Plan Guides to Modify Execution Plans _____________________________ 310 Object plan guides ________________________________________________ 311 SQL plan guides ___________________________________________________ 314 Template plan guides _______________________________________________ 315 Plan guide administration ___________________________________________ 316 Plan forcing ______________________________________________________ 317 Summary ___________________________________________________________ 321 ix About the Author Grant Fritchey is a SQL Server MVP with over 20 years' experience in IT including time spent in support, development, and database administration. Grant has worked with SQL Server since version 6.0, back in 1995. He has developed in VB, VB.Net, C#, and Java. Grant joined Red Gate as a Product Evangelist in January 2011. He writes articles for publication at SQL Server Central, Simple-Talk, and other community sites, and has published two books: the one you're reading now and SQL Server 2012 Query Performance Tuning Distilled, 3rd Edition (Apress, 2012). In the past, people have called him intimidating and scary. To which his usual reply is "Good." You can contact him through grant -at- scarydba dot kom (de-obfuscate as necessary). About the Technical Reviewer Brad M. McGehee is a MCTS, MCSE+I, MCSD, and MCT (former) with a Bachelor's degree in Economics and a Master's in Business Administration. Currently a DBA with a Top 10 accounting firm, Brad is an accomplished Microsoft SQL Server MVP with over 17 years' SQL Server experience, and over 8 years' training experience; he has been involved in IT since 1982. Brad is a frequent speaker at SQL PASS, European PASS, SQL Server Connections, SQLTeach, devLINK, SQLBits, SQL Saturdays, TechFests, Code Camps, SQL in the City, SQL Server user groups, webinars, and other industry seminars, where he shares his 17 years of cumulative knowledge and experience. x In 2009, 2010, and 2011, Brad has made 86 different public presentations to a total of 6,750 attendees in six different countries. Brad was the founder of the popular community site, .SQL--. , and operated it from 2000 through 2006, where he wrote over one million words on SQL Server topics. A well-respected and trusted name in SQL Server literature, Brad is the author or co-author of more than 15 technical books and over 300 published articles. His most recent books include How to Become an Exceptional DBA (2nd Edition), Brad's Sure Guide to SQL Server 2008: The Top Ten New Features for DBAs, Mastering SQL Server Profiler, and Brad's Sure Guide to SQL Server Maintenance Plans. These books are available, free, in PDF format at: :..B. His blog is at ... [...]... details of how SQL Server works While there are a few good books on the advanced use of SQL Server, there are still many areas that aren't well covered One of those areas of missing knowledge is a dedicated book on SQL Server execution plans That's where SQL Server Execution Plans comes into play It is the first book available anywhere that focuses entirely on what SQL Server execution plans are, how... order to boost the performance of your SQL Servers 11 This was not an easy book to write because SQL Server execution plans are not well documented Grant Fritchey spent a huge amount of time researching SQL Server execution plans, and conducting original research as necessary, in order to write the material in this book Once you understand the fundamentals of SQL Server, this book should be on top of... in this book: www.simple-talk.com/RedGateBooks/GrantFritchey_SQLServerExecutionPlans_ Code.zip I wrote and tested the examples on SQL 2008 Release 2 sample database, AdventureWorks2008R2 However, the majority of the code will run on all editions and versions of SQL Server, starting from SQL Server 2005 16 Some of the code may not work within SQL Azure, but a large amount of it will You can get hold of... investigate graphical, text and XML execution plans • Retrieve execution plans directly from the cache – Accessing the plan cache through Dynamic Management Objects (DMOs) • Automating execution plan capture – using SQL Server Trace Event 18 Chapter 1: Execution Plan Basics What Happens When a Query is Submitted? When you submit a query to SQL Server, a number of processes on the server go to work on that query... makes actual execution plans the one you use the most, but estimated plans are extremely important, especially because that's what you get from the plan cache 25 Chapter 1: Execution Plan Basics Execution Plan Reuse It is expensive for the server to go through all the processes described above that are required to generate execution plans While SQL Server can do all this in less than a millisecond, depending... optimize your SQL queries, improve your indexing strategy, and so on Specifically, I cover: • How to capture execution plans in graphical, as well as text and XML formats • A documented method for interpreting execution plans, so that you can create these plans from your own code and make sense of them in your own environment • How SQL Server represents and interprets the common SQL Server objects... details of the execution plans, and how these issues are manifest in these plans 14 If you are specifically looking for information on how to optimize SQL, or build efficient indexes, then you need a book dedicated to these topics However, if you want to understand how to interpret these issues within an execution plan, then this is the place for you Changes in This Second Edition This second edition is... chapter Estimated plans are the types of plans stored in the plan cache, so this means that we can access the data available in actual execution plans only by capturing the execution of a query Since estimated plans never access data, they are very useful for large, complex queries that could take a long time to run Actual execution plans are preferred because they show important execution statistics... so on, in execution plans • How to spot some common performance issues such as Bookmark Lookups or unused/missing indexes • How to control execution plans with hints, plan guides and so on, and why this is a double-edged sword • How XML code appears in execution plans • Advanced topics such as parallelism, forced parameterization and plan forcing Along the way, I tackle such topics as SQL Server internals,... millisecond, depending on the query it can take seconds or even minutes to create an execution plan, so SQL Server will keep and reuse plans wherever possible in order to reduce that overhead As they are created, plans are stored in a section of memory called the plan cache (prior to SQL Server 2005 this was called the procedure cache) When we submit a query to the server, the algebrizer process creates a . SQL Handbooks SQL Server Execution Plans Second Edition By Grant Fritchey SQL Server Execution Plans Second Edition By Grant Fritchey Published. speaker at SQL PASS, European PASS, SQL Server Connections, SQLTeach, devLINK, SQLBits, SQL Saturdays, TechFests, Code Camps, SQL in the City, SQL Server

Ngày đăng: 08/03/2014, 22:20

Từ khóa liên quan

Mục lục

  • Introduction

    • Changes in This Second Edition

      • Code Examples

      • Chapter 1: Execution Plan Basics

        • What Happens When a Query is Submitted?

          • Query parsing

          • Algebrizer

          • The query optimizer

          • Query execution

          • Estimated and Actual Execution Plans

          • Execution Plan Reuse

          • Clearing Plans from the Plan Cache

          • Execution Plan Formats

            • Graphical plans

            • Text plans

            • XML plans

            • Getting Started

              • Permissions required to view execution plans

              • Working with graphical execution plans

              • Working with text execution plans

              • Working with XML execution plans

              • Interpreting XML plans

              • Retrieving Plans from the Cache Using Dynamic Management Objects

              • Automating Plan Capture Using SQL Server Trace Events

                • Execution plan events

                • Capturing a Showplan XML trace

                • Why the actual and estimated execution plans might differ

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan