S Q C L o S v erer v s e r 2 0 1 6 THE EXPERT’S VOICE® IN SQL High Performance SQL Server The Go Faster Book — Bringing consistent response time to mission-critical applications — Benjamin Nevarez High Performance SQL Server The Go Faster Book Benjamin Nevarez High Performance SQL Server: The Go Faster Book Benjamin Nevarez Santa Clarita, California, USA ISBN-13 (pbk): 978-1-4842-2270-6 ISBN-13 (electronic): 978-1-4842-2271-3 DOI 10.1007/978-1-4842-2271-3 Library of Congress Control Number: 2016959790 Copyright © 2016 by Benjamin Nevarez This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Jonathan Gennick Development Editor: Laura Berendson Technical Reviewer: Richard Douglas, Mike Walsh, George Anderson Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan, Jonathan Gennick, Todd Green, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal, James Markham, Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing Coordinating Editor: Jill Balzano Copy Editor: Ann Dickson Compositor: SPi Global Indexer: Spi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail o [email protected] , or visit w ww.springer.com . Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected] , or visit w ww.apress.com . 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 w ww.apress.com/bulk-sales . Any source code or other supplementary materials referenced by the author in this text are available to readers at w ww.apress.com . For detailed information about how to locate your book’s source code, go to w ww.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper Th is book is dedicated to my sons, Diego, Benjamin, and David Contents at a Glance About the Author .....................................................................................................xv About the Technical Reviewers .............................................................................xvii Acknowledgments ..................................................................................................xix Introduction ............................................................................................................xxi ■ Chapter 1: How SQL Server Works ........................................................................1 ■ Chapter 2: Analyzing Wait Statistics ...................................................................21 ■ Chapter 3: The Query Store ..................................................................................45 ■ Chapter 4: SQL Server Confi guration ...................................................................71 ■ Chapter 5: tempdb Troubleshooting and Confi guration .......................................85 ■ Chapter 6: SQL Server In-Memory Technologies ...............................................101 ■ Chapter 7: Performance Troubleshooting ..........................................................131 ■ Chapter 8: Indexing ...........................................................................................155 ■ Chapter 9: SQL Server Storage ..........................................................................181 Index .....................................................................................................................197 v Contents About the Author .....................................................................................................xv About the Technical Reviewers .............................................................................xvii Acknowledgments ..................................................................................................xix Introduction ............................................................................................................xxi ■ Chapter 1: How SQL Server Works ........................................................................1 TDS/Network Protocols ....................................................................................................1 How Work Is Performed ....................................................................................................3 SQLOS .....................................................................................................................................................3 Schedulers ..............................................................................................................................................3 Workers ..................................................................................................................................................5 Query Optimization ...........................................................................................................8 Parsing and Binding ...............................................................................................................................8 Query Optimization .................................................................................................................................9 Plan Caching .........................................................................................................................................12 Query Execution .............................................................................................................13 Operators ..............................................................................................................................................13 Data Access Operators .........................................................................................................................14 Aggregations ........................................................................................................................................15 Joins .....................................................................................................................................................15 Parallelism ............................................................................................................................................16 Updates.................................................................................................................................................16 vii ■ CONTENTS Memory Grants ...............................................................................................................17 Locks and Latches..........................................................................................................18 Summary ........................................................................................................................19 ■ Chapter 2: Analyzing Wait Statistics ...................................................................21 Introduction ....................................................................................................................22 Waits Information ...........................................................................................................26 sys.dm_os_wait_stats ..........................................................................................................................26 sys.dm_exec_session_wait_stats ........................................................................................................30 Extended Events ...................................................................................................................................30 system_health Extended Event Session ...............................................................................................32 Example: Analyzing CXPACKET Waits ............................................................................34 Latches and Spinlocks ...................................................................................................37 Waits ..............................................................................................................................39 CX_PACKET ...........................................................................................................................................39 PAGELATCH_ and PAGEIOLATCH_ .........................................................................................................39 ASYNC_NETWORK_IO ...........................................................................................................................39 SOS_SCHEDULER_YIELD ......................................................................................................................39 THREADPOOL ........................................................................................................................................39 PREEMPTIVE_* ......................................................................................................................................40 OLEDB ...................................................................................................................................................40 IO_COMPLETION ...................................................................................................................................40 WRITELOG .............................................................................................................................................40 Timer Wait Types ...................................................................................................................................40 Blocking .........................................................................................................................42 In-Memory OLTP .............................................................................................................43 Summary ........................................................................................................................43 viii ■ CONTENTS ■ Chapter 3: The Query Store ..................................................................................45 Why Is a Query Slow? .....................................................................................................45 Plan Changes .................................................................................................................45 How the Query Store Can Help .......................................................................................47 Plan Regressions .................................................................................................................................47 SQL Server Upgrades ...........................................................................................................................47 Application/Hardware Changes ............................................................................................................47 Identify Expensive Queries ..................................................................................................................47 Identify Ad Hoc Workloads ....................................................................................................................48 Architecture ....................................................................................................................48 Enabling, Purging, and Disabling the Query Store ..........................................................50 Using the Query Store ....................................................................................................55 Performance Troubleshooting ........................................................................................59 Incomplete Queries ........................................................................................................61 Force Failure ..................................................................................................................64 Catalog Views .................................................................................................................65 Live Query Statistics.......................................................................................................67 Summary ........................................................................................................................69 ■ Chapter 4: SQL Server Confi guration ...................................................................71 Statistics Update ............................................................................................................71 Standard Automatic Statistics Update ..................................................................................................73 Trace Flag 2371 and SQL Server 2016 .................................................................................................73 tempdb Confi guration .....................................................................................................73 Query Optimizer Hotfi xes and Trace Flag 4199 ..............................................................74 Max Degree of Parallelism .............................................................................................75 Cost Threshold for Parallelism .......................................................................................77 ix ■ CONTENTS Instant File Initialization .................................................................................................77 Cardinality Estimator ......................................................................................................78 Optimize for Ad Hoc Workloads ......................................................................................79 SQL Server Editions ........................................................................................................80 Memory Confi guration ....................................................................................................80 Lock Pages in Memory ...................................................................................................81 Backup Compression Default .........................................................................................82 Query Governor Cost Limit .............................................................................................82 Blocked Process Threshold ............................................................................................82 Advanced Trace Flags .....................................................................................................83 Summary ........................................................................................................................84 ■ Chapter 5: tempdb Troubleshooting and Confi guration .......................................85 DML Contention ..............................................................................................................85 Describing tempdb Latch Contention ...................................................................................................88 Fixing tempdb Latch Contention ...........................................................................................................89 Using Multiple Data Files ................................................................................................89 Trace Flags 1117 and 1118 ............................................................................................89 What Is New in SQL Server 2016 ...................................................................................90 tempdb Events ...............................................................................................................92 DDL Contention ...............................................................................................................97 tempdb Spill Warnings ...................................................................................................97 Sort Warning .........................................................................................................................................97 Hash Warning .......................................................................................................................................98 Exchange Warning ................................................................................................................................98 Monitoring Disk Space ...................................................................................................98 Summary ........................................................................................................................99 x ■ CONTENTS ■ Chapter 6: SQL Server In-Memory Technologies ...............................................101 In-Memory OLTP ...........................................................................................................102 What Is New in SQL Server 2016 ........................................................................................................103 Memory-Optimized Tables ..................................................................................................................103 Indexes ...............................................................................................................................................106 Natively Compiled Modules ................................................................................................................108 Changing Tables and Natively Compiled Modules ..............................................................................110 Native Compilation .............................................................................................................................112 Memory-Optimized Table Variables ....................................................................................................113 Current Limitations .............................................................................................................................114 Columnstore Indexes ....................................................................................................114 Examples ............................................................................................................................................116 Operational Analytics ....................................................................................................121 Using Disk-Based Tables ....................................................................................................................123 Using Memory-Optimized Tables ........................................................................................................126 Summary ......................................................................................................................129 ■ Chapter 7: Performance Troubleshooting ..........................................................131 Performance Counters .................................................................................................132 Comparing Batches and Transactions ................................................................................................133 Log Growths........................................................................................................................................135 Data File(s) Size (KB) ..........................................................................................................................137 Page Reads/Sec ..................................................................................................................................137 Page Writes/Sec .................................................................................................................................137 Page Life Expectancy..........................................................................................................................137 Buffer Cache Hit Ratio ........................................................................................................................138 % Processor Time ...............................................................................................................................138 Processor Queue Length ....................................................................................................................139 Latches ...............................................................................................................................................140 Locks ..................................................................................................................................................140 LogicalDisk and PhysicalDisk .............................................................................................................140 xi