SQL Server Execution Plans Second Edition by Grant Fritchey SQL Server Execution Plans

Total Page:16

File Type:pdf, Size:1020Kb

SQL Server Execution Plans Second Edition by Grant Fritchey SQL Server Execution Plans 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 ___________________________
Recommended publications
  • SQL/MX Release 2.0 Best Practices
    SQL/MX Best Practices white paper Part number: 540372-003 Second edition: 01/2007 Legal notices © Copyright 2006 Hewlett-Packard Development Company, L.P. The information herein is subject to change without notice. The only warranties for HP products and services are set forth in the express warranty statements accompanying such products and services. Nothing herein should be construed as constituting an additional warranty. HP shall not be liable for technical or editorial errors or omissions contained herein. Printed in the US Part number: 540372-003 Second edition: 01/2007 2 Contents Objectives of This Document Project Planning HP Roles and Responsibilities...............................................................................................................................7 Roles.......................................................................................................................................................................7 Technical Lead................................................................................................................................................7 Database Consultant .....................................................................................................................................7 Extract, Transform, and Load (ETL) Specialist...............................................................................................8 Performance Consultant................................................................................................................................8
    [Show full text]
  • Sqlserver 2005, 2008, 2012
    Installation and Startup Guide Edition March 2020 Copyright © Atos IT Solutions and Services GmbH 2020 Microsoft, MS, MS-DOS and Windows are trademarks of Microsoft Corporation. The reproduction, transmission, translation or exploitation of this document or its content is not permitted without express written authority. Offenders will be liable for damages. All rights reserved, including rights created by patent grant or registration of a utility model or design. Right of technical modification reserved. Installation Guide TEMPPO V6.3 Edition March 2020 Contents Contents 1 Introduction ....................................................................................... 1 2 Installation ........................................................................................ 2 3 Startup & first steps ......................................................................... 27 4 Appendix .......................................................................................... 30 Installation Guide TEMPPO V6.3 Edition March 2020 Introduction 1 Introduction This manual describes the installation of TEMPPO (Test Execution, Management, Planning and rePorting Organizer) Version 6.3 as client. Managing the test process involves a large number of engineering disciplines like project management, risk management, high and low level design, development and metrics collection. In former times testers managed their tests and test management with Word table or Excel sheets which had the main disadvantages that tests were not organized, couldn’t be
    [Show full text]
  • Parallel and Append Hint in Insert Statement
    Parallel And Append Hint In Insert Statement Sunrise Gay subjoins his viridescence focalise pryingly. Is Odysseus preconscious when Marlin precool signally? Ocean-going Cole ensanguining unworthily. Insert statement with array inserts, then create an access method that parallel and hint in insert statement We can see which each source i was inserted into excel table only. Parallel statements that appends will append hint will propose an answer for a different sessions can. The table specified in tablespec is the probe table of the hash join. Oracle hint was an error logging table or merge statement itself is not possible. Before you can execute DML statements in parallel, you must display the parallel DML feature. The insert or insert and parallel hint in append hint is that page helpful in. Instructs the optimizer selectivity and parallel insert on the source records mark or azure storage requirements. Oracle choose among different options or may be i missed to understand. You do part of these large, update as cpus is genetated for pointing this hint and parallel in insert statement, the index partition, consider adding an error message is that your help us the. Doing all modifications to statement and parallel hint in append insert statement are too large database table were responsible for the plans and for the definition query? When a mortgage is overloaded and all input DOP is larger than the default DOP, the algorithm uses the default degree of input. Instructs the optimizer to use but full outer court, which prohibit a native execution method based on a hash join.
    [Show full text]
  • Reference Manual: Commands Content
    Reference Manual PUBLIC SAP Adaptive Server Enterprise 16.0 SP02 Document Version: 1.4 – 2016-12-20 Reference Manual: Commands Content 1 Commands................................................................14 1.1 alter database...............................................................14 alter database Restrictions...................................................26 Using alter database for Archive Databases.......................................27 Altering In-Memory and Relaxed Durability Databases............................... 27 Backing Up master After Allocating More Space....................................28 Placing the Log on a Separate Device........................................... 28 Altering Databases for Compression............................................28 In-row LOB Columns.......................................................29 Shrinking Log Space....................................................... 29 Getting Help on Space Usage.................................................30 The system and default Segments............................................. 30 Using alter database to Awaken Sleeping Processes.................................30 Using the alter database for proxy_update Parameter............................... 30 Fully Encrypting Databases...................................................31 1.2 alter encryption key...........................................................31 1.3 alter index.................................................................40 1.4 alter login..................................................................42
    [Show full text]
  • The Use of Hints in Object-Relational Query Optimization
    Comput Syst Sci & Eng (2004) 6: 337–345 International Journal of © 2004 CRL Publishing Ltd Computer Systems Science & Engineering The use of hints in object-relational query optimization David Taniar*, Hui Yee Khaw*. Haorianto Cokrowijoyo Tijoe* and Johanna Wenny Rahayu† *School of Business Systems, Monash University, Clayton, Victoria 3800, Australia. Email: {David.Taniar,Haorianto.Tjioe}@infotech.monash.edu.au †Department of Computer Science and Computer Engineering, La Trobe University, Bundoora, Victoria 3803, Australia Email: [email protected] Object-Relational queries are queries which can handle data objects feature on the existing relational data environment. REF is one of most impor- tant data structures in Object-Relational Databases can be explained as a logical pointer to define the link between two tables which similar func- tion to pointers in object oriented programming language. There are three ways of writing REF join queries which are: REF Join, Path Expression Join and DEREF/VALUE Join. In this paper, we study optimization technique for Object-Relational queries using hints. Hints are additional comments which are inserted into an SQL statement for the purpose of instructing or suggesting the optimizer to perform the specified operations. We utilize various hints including Optimizer hints, Table join and anti-join hints, and Access method hints. We analyse performance of various object-relation- al queries particularly in three ways of writing REF queries using the TRACE and TKPROF utilities which provide query execution statistics and exe- cution plans. Keywords: object-relational query optimization, ORBMS, REF 1. INTRODUCTION pointer to define the link between two tables (Taniar, Rahayu and Srivastava, 2003).
    [Show full text]
  • Mysql 5.7: Performance Improvements in Optimizer
    MySQL 5.7: Performance Improvements in Optimizer Olav Sandstå Senior Principal Engineer MySQL Optimizer Team, Oracle April 25, 2016 Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 2 Program Agenda 1 Improvements in optimizer • Cost model • New optimizations 2 Understanding query performance • Explain extensions • Optimizer trace 3 Tools for improving query plans • New hints • Query rewrite plugin Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 3 MySQL Optimizer MySQL Server SELECT a, b Cost based JOIN FROM t1 optimizations JOIN t2 Table JOIN ON t1.a = t2.b Cost Model scan JOIN t3 Range Ref t1 ON t2.b = t3.c Parser scan access Optimizer Heuristics WHERE t2.d > 20 AND t2.d < 30; t2 t3 Table/index info Statistics (data dictionary) (storage engines) Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 4 Optimizer Performance Improvements in MySQL 5.7 Cost model improvements: New optimizations: • Cost model for WHERE conditions • Merging of derived tables (condition filtering effect) • Optimization of IN queries – Improved JOIN order • Union ALL optimization • Improved index statistics – Better index selection, better join order • Configurable “cost constants” Copyright © 2016, Oracle and/or its affiliates.
    [Show full text]
  • N1ql: a Practical Guide N1ql = Sql + Json
    N1QL: A PRACTICAL GUIDE N1QL = SQL + JSON Gerald Sangudi Sitaram Vemulapalli Marco Greco Johan Larson Isha Kandaswamy Prasad Varakur Eben Haber Manuel Hurtado Keshav Murthy Tai Tran Foreword by Gerald Sangudi, father of N1QL To Couchbase & N1QL Customers & Community Foreword We are excited to present this eBook to you, our N1QL customers, users and readers. We started developing N1QL way back in 2013, when we realized that people were managing more of their data in JSON, but in doing so, they were having to give up the benefits of SQL querying. We appreciate both SQL and JSON, and we thought users should have the benefits of both. After a few developer previews and much helpful feedback from early users and customers, N1QL was released in 2015. As of this writing in March 2017, N1QL is being used by over 100 enterprise customers, and many times that number of community users and developers. N1QL has steadily grown in sophistication, functionality, and performance. This book is a compilation of some of our DZone articles through 2016. We have found DZone to be a great medium for N1QL blogs and articles. In addition to the initial reading, the articles serve as a continuous resource for questions, learning, reference, and discussion. A special thanks to our Couchbase and community colleagues, including those who contributed articles after 2016. We hope you enjoy this eBook. If you have feedback or suggestions for future topics, please share them on our forums, on Stack Overflow, and of course, on DZone. Gerald Sangudi For Team N1QL Mountain View, California th March, 14 ,​ 2017 ​ Enterprise Jobs To Be DONE Enterprises on Getting the Jobs Done Authors: Gerald Sangudi, Keshav Murthy ​ Every enterprise, from the biggest and most familiar brand to the up and coming innovator, has important jobs to get done.
    [Show full text]
  • Oracle® Rdb for Openvms Oracle® Rdb for Openvms Table of Contents
    Oracle® Rdb for OpenVMS Oracle® Rdb for OpenVMS Table of Contents Oracle® Rdb for OpenVMS..............................................................................................................................1 Release Notes.......................................................................................................................................................2 January 2006.......................................................................................................................................................3 Contents...............................................................................................................................................................4 Preface..................................................................................................................................................................5 Purpose of This Manual.....................................................................................................................................6 Intended Audience..............................................................................................................................................7 Document Structure............................................................................................................................................8 Chapter 1Installing Oracle Rdb Release 7.2....................................................................................................9 1.1 Oracle Rdb on HP OpenVMS Industry Standard 64..............................................................................10
    [Show full text]
  • Workbench-En.Pdf
    MySQL Workbench Abstract This is the MySQL Workbench Reference Manual. It documents the MySQL Workbench Community and MySQL Workbench Commercial releases for versions 8.0 through 8.0.26. If you have not yet installed the MySQL Workbench Community release, please download your free copy from the download site. The MySQL Workbench Community release is available for Microsoft Windows, macOS, and Linux. MySQL Workbench platform support evolves over time. For the latest platform support information, see https:// www.mysql.com/support/supportedplatforms/workbench.html. For notes detailing the changes in each release, see the MySQL Workbench Release Notes. For legal information, including licensing information, see the Preface and Legal Notices. For help with using MySQL, please visit the MySQL Forums, where you can discuss your issues with other MySQL users. Document generated on: 2021-10-01 (revision: 70942) Table of Contents Preface and Legal Notices ................................................................................................................ vii 1 General Information ......................................................................................................................... 1 1.1 What Is New in MySQL Workbench ...................................................................................... 1 1.1.1 New in MySQL Workbench 8.0 Release Series ........................................................... 1 1.1.2 New in MySQL Workbench 6.0 Release Series ..........................................................
    [Show full text]
  • The Wisconsin Benchmark: Past, Present, and Future
    The Wisconsin Benchmark: Past, Present, and Future David J. DeWitt Computer Sciences Department University of Wisconsin 1.0. Introduction In 1981 as we were completing the implementation of the DIRECT database machine [DEWI79, BORA82], attention turned to evaluating its performance. At that time no standard database benchmark existed. There were only a few application-specific benchmarks. While application-specific benchmarks measure which database system is best for a particular application, it was very difficult to understand them. We were interested in a benchmark to measure DIRECT's speedup characteristics. Thus, we set out to develop a benchmark that could be used to evaluate DIRECT both relative to itself and relative to the "university" version of Ingres. The result of this effort was a benchmark for relational database systems and machines that has become known as the Wisconsin Benchmark [BITT83]. The benchmark was designed with two objectives in mind. First, the queries in the benchmark should test the performance of the major components of a relational database system. Second, the semantics and statistics of the underlying relations should be well understood so that it is easy to add new queries and to their behavior. We never expected this benchmark to become as popular as it did. In retrospect, the reasons for this popularity were only partially due to its technical quality. The primary reason for its success was that it was the first evaluation containing impartial measures of real products. By actually identifying the products by name, the benchmark triggered a series of "benchmark wars" between commercial database products. With each new release, each vendor would produce a new set of numbers claiming superiority.
    [Show full text]
  • Architecture of a Database System
    Foundations and TrendsR in Databases Vol. 1, No. 2 (2007) 141–259 c 2007 J. M. Hellerstein, M. Stonebraker and J. Hamilton DOI: 10.1561/1900000002 Architecture of a Database System Joseph M. Hellerstein1, Michael Stonebraker2 and James Hamilton3 1 University of California, Berkeley, USA, [email protected] 2 Massachusetts Institute of Technology, USA 3 Microsoft Research, USA Abstract Database Management Systems (DBMSs) are a ubiquitous and critical component of modern computing, and the result of decades of research and development in both academia and industry. Historically, DBMSs were among the earliest multi-user server systems to be developed, and thus pioneered many systems design techniques for scalability and relia- bility now in use in many other contexts. While many of the algorithms and abstractions used by a DBMS are textbook material, there has been relatively sparse coverage in the literature of the systems design issues that make a DBMS work. This paper presents an architectural dis- cussion of DBMS design principles, including process models, parallel architecture, storage system design, transaction system implementa- tion, query processor and optimizer architectures, and typical shared components and utilities. Successful commercial and open-source sys- tems are used as points of reference, particularly when multiple alter- native designs have been adopted by different groups. 1 Introduction Database Management Systems (DBMSs) are complex, mission-critical software systems. Today’s DBMSs embody decades of academic and industrial research and intense corporate software development. Database systems were among the earliest widely deployed online server systems and, as such, have pioneered design solutions spanning not only data management, but also applications, operating systems, and net- worked services.
    [Show full text]
  • The Query Optimizer in Oracle Database 19C What's New?
    The Query Optimizer in Oracle Database 19c What’s New? Christian Antognini @ChrisAntognini antognini.ch Christian Antognini • Senior principal consultant and partner at Trivadis • Focus: get the most out of database engines • Logical and physical database design • Query optimizer • Application performance management • Author of Troubleshooting Oracle Performance @ChrisAntognini antognini.ch Agenda • All Environments • Reporting on Hint Usage • Execution Plan Comparison Refer to the Oracle • Online Statistics Gathering Enhancements Database SQL Tuning Guide • Enterprise Edition with Management Packs for technical information • Statistics Maintenance Enhancements about the new features • Real-Time SQL Monitoring for Developers • Enterprise Edition on Exadata The Database Licensing • Automatic Resolution of Plan Regressions Information User Manual • High-Frequency SPM Evolve Advisor Task provides information about • Real-Time Statistics their availability • High-Frequency Automatic Statistics Collection • Quarantine for Runaway SQL Statements • Automatic Indexing Disclaimer • This presentation covers the versions 19.[3-5] • This presentation does not cover the query optimizer features that are specific to the Autonomous Database services All Environments Reporting on Hint Usage • It is an issue to verify whether hints added to SQL statements are actually used • To make it easier, there is a new section in the output generated by the DBMS_XPLAN.DISPLAY* functions • The new section (Hint Report) reports hints into two categories • Valid (used)
    [Show full text]