pro sql server internals - springer978-1-4302-5963-3/1.pdf · v contents at a glance about the...

31
Pro SQL Server Internals Dmitri Korotkevitch

Upload: duongbao

Post on 04-Aug-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Pro SQL Server Internals

Dmitri Korotkevitch

Pro SQL Server Internals

Copyright © 2014 by Dmitri Korotkevitch

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. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4302-5962-6

ISBN-13 (electronic): 978-1-4302-5963-3

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.

President and Publisher: Paul ManningLead Editor: Jonathan HassellTechnical Reviewers: Victor Isakov and Thomas LaRockEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Jim DeWolf,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss

Coordinating Editor: Kevin SheaDevelopmental Editor: Gary SchwartzCopy Editor: Lori CavanaughCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

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 [email protected], or visit www.springeronline.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 www.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 www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

To my family. Thank you for keeping me going . . .

v

Contents at a Glance

About the Author ������������������������������������������������������������������������������������������������������������� xxiii

About the Technical Reviewers ���������������������������������������������������������������������������������������� xxv

Acknowledgments ���������������������������������������������������������������������������������������������������������� xxvii

Introduction ��������������������������������������������������������������������������������������������������������������������� xxix

Part 1: Tables and Indexes ■ ��������������������������������������������������������������������������� 1

Chapter 1: Data Storage Internals ■ �������������������������������������������������������������������������������������3

Chapter 2: Tables and Indexes: Internal Structure and Access Methods ■ ������������������������29

Chapter 3: Statistics ■ ��������������������������������������������������������������������������������������������������������53

Chapter 4: Special Indexing and Storage Features ■ ���������������������������������������������������������81

Chapter 5: Index Fragmentation ■ ������������������������������������������������������������������������������������113

Chapter 6: Designing and Tuning the Indexes ■ ���������������������������������������������������������������125

Part 2: Other things that matter ■ ��������������������������������������������������������������� 149

Chapter 7: Constraints ■ ���������������������������������������������������������������������������������������������������151

Chapter 8: Triggers ■ ��������������������������������������������������������������������������������������������������������165

Chapter 9: Views ■ �����������������������������������������������������������������������������������������������������������181

Chapter 10: User-Defined Functions ■ ������������������������������������������������������������������������������195

Chapter 11: XML ■ ������������������������������������������������������������������������������������������������������������209

Chapter 12: Temporary Tables ■ ���������������������������������������������������������������������������������������233

■ Contents at a GlanCe

vi

Chapter 13: CLR ■ �������������������������������������������������������������������������������������������������������������255

Chapter 14: CLR Types ■ ���������������������������������������������������������������������������������������������������275

Chapter 15: Data Partitioning ■ ����������������������������������������������������������������������������������������301

Chapter 16: System Design Considerations ■ �������������������������������������������������������������������349

Part 3: Locking, Blocking and Concurrency ■ ��������������������������������������������� 373

Chapter 17: Lock Types ■ �������������������������������������������������������������������������������������������������375

Chapter 18: Troubleshooting Blocking Issues ■ ���������������������������������������������������������������387

Chapter 19: Deadlocks ■ ��������������������������������������������������������������������������������������������������399

Chapter 20: Lock Escalation ■ ������������������������������������������������������������������������������������������417

Chapter 21: Optimistic Isolation Levels ■ �������������������������������������������������������������������������427

Chapter 22: Application Locks ■ ���������������������������������������������������������������������������������������439

Chapter 23: Schema Locks ■ ��������������������������������������������������������������������������������������������443

Chapter 24: Designing Transaction Strategies ■ ��������������������������������������������������������������455

Part 4: Query Life Cycle ■ ���������������������������������������������������������������������������� 459

Chapter 25: Query Optimization and Execution ■ �������������������������������������������������������������461

Chapter 26: Plan Caching ■ ����������������������������������������������������������������������������������������������489

Part 5: Practical Troubleshooting ■ ������������������������������������������������������������� 521

Chapter 27: System Troubleshooting ■ �����������������������������������������������������������������������������523

Chapter 28: Extended Events ■ �����������������������������������������������������������������������������������������553

Part 6: Inside the transaction log ■ ������������������������������������������������������������� 583

Chapter 29: Transaction Log Internals ■ ��������������������������������������������������������������������������585

Chapter 30: Designing a Backup Strategy ■ ���������������������������������������������������������������������603

Chapter 31: Designing a High Availability Strategy ■ �������������������������������������������������������625

■ Contents at a GlanCe

vii

Part 7: In-Memory OLTP Engine ■ ���������������������������������������������������������������� 647

Chapter 32: In-Memory OLTP Internals ■ ������������������������������������������������������������������������649

Part 8: Columnstore Indexes ■ �������������������������������������������������������������������� 689

Chapter 33: In-Memory OLTP Programmability ■ ������������������������������������������������������������691

Chapter 34: Introduction to Columnstore Indexes ■ ��������������������������������������������������������709

Chapter 35: Clustered Columnstore Indexes ■ �����������������������������������������������������������������737

Index ���������������������������������������������������������������������������������������������������������������������������������755

ix

Contents

About the Author ������������������������������������������������������������������������������������������������������������� xxiii

About the Technical Reviewers ���������������������������������������������������������������������������������������� xxv

Acknowledgments ���������������������������������������������������������������������������������������������������������� xxvii

Introduction ��������������������������������������������������������������������������������������������������������������������� xxix

Part 1: Tables and Indexes ■ ��������������������������������������������������������������������������� 1Chapter 1: Data Storage Internals ■ �������������������������������������������������������������������������������������3

Database Files and Filegroups ������������������������������������������������������������������������������������������������������3

Data Pages and Data Rows �����������������������������������������������������������������������������������������������������������7

Large Objects Storage �����������������������������������������������������������������������������������������������������������������13Row-Overflow Storage����������������������������������������������������������������������������������������������������������������������������������������� 13

LOB Storage ��������������������������������������������������������������������������������������������������������������������������������������������������������� 15

SELECT * and I/O �������������������������������������������������������������������������������������������������������������������������17

Extents and Allocation Map Pages ����������������������������������������������������������������������������������������������18

Data Modifications ����������������������������������������������������������������������������������������������������������������������20

Much Ado About Data Row Size ��������������������������������������������������������������������������������������������������22

Table Alteration ����������������������������������������������������������������������������������������������������������������������������24

Summary �������������������������������������������������������������������������������������������������������������������������������������27

Chapter 2: Tables and Indexes: Internal Structure and Access Methods ■ ������������������������29

Heap Tables ���������������������������������������������������������������������������������������������������������������������������������29

Clustered Indexes ������������������������������������������������������������������������������������������������������������������������34

Composite Indexes ����������������������������������������������������������������������������������������������������������������������45

Nonclustered Indexes ������������������������������������������������������������������������������������������������������������������46

Summary �������������������������������������������������������������������������������������������������������������������������������������51

■ Contents

x

Chapter 3: Statistics ■ ��������������������������������������������������������������������������������������������������������53

Introduction to SQL Server Statistics ������������������������������������������������������������������������������������������53

Column-Level Statistics ��������������������������������������������������������������������������������������������������������������58

Statistics and Execution Plans ����������������������������������������������������������������������������������������������������62

Statistics and Query Memory Grants �������������������������������������������������������������������������������������������65

Statistics Maintenance ����������������������������������������������������������������������������������������������������������������68

SQL Server 2014 Cardinality Estimator ���������������������������������������������������������������������������������������70

Comparing Cardinality Estimators: Up-to-Date Statistics ������������������������������������������������������������������������������������ 71

Comparing Cardinality Estimators: Outdated Statistics ��������������������������������������������������������������������������������������� 75

Comparing Cardinality Estimators: Indexes with Ever-Increasing Key Values ����������������������������������������������������� 76

Comparing Cardinality Estimators: Joins ������������������������������������������������������������������������������������������������������������� 78

Comparing Cardinality Estimators: Multiple Predicates��������������������������������������������������������������������������������������� 79

Summary �������������������������������������������������������������������������������������������������������������������������������������80

Chapter 4: Special Indexing and Storage Features ■ ���������������������������������������������������������81

Indexes with Included Columns ��������������������������������������������������������������������������������������������������81

Filtered Indexes ���������������������������������������������������������������������������������������������������������������������������88

Filtered Statistics ������������������������������������������������������������������������������������������������������������������������91

Calculated Columns ���������������������������������������������������������������������������������������������������������������������95

Data Compression ���������������������������������������������������������������������������������������������������������������������100

Row Compression ���������������������������������������������������������������������������������������������������������������������������������������������� 100

Page Compression ��������������������������������������������������������������������������������������������������������������������������������������������� 104

Performance Considerations ����������������������������������������������������������������������������������������������������������������������������� 106

Sparse Columns ������������������������������������������������������������������������������������������������������������������������108

Summary �����������������������������������������������������������������������������������������������������������������������������������112

Chapter 5: Index Fragmentation ■ ������������������������������������������������������������������������������������113

Types of Fragmentation �������������������������������������������������������������������������������������������������������������113

FILLFACTOR and PAD_INDEX �����������������������������������������������������������������������������������������������������117

Index Maintenance ��������������������������������������������������������������������������������������������������������������������117

■ Contents

xi

Designing an Index Maintenance Strategy ��������������������������������������������������������������������������������118

Patterns that Increase Fragmentation ���������������������������������������������������������������������������������������119

Summary �����������������������������������������������������������������������������������������������������������������������������������124

Chapter 6: Designing and Tuning the Indexes ■ ���������������������������������������������������������������125

Clustered Index Design Considerations �������������������������������������������������������������������������������������125Design Guidelines ���������������������������������������������������������������������������������������������������������������������������������������������� 125

Identities, Sequences, and Uniqueidentifiers ���������������������������������������������������������������������������������������������������� 130

Nonclustered Indexes Design Considerations ���������������������������������������������������������������������������135

Optimizing and Tuning Indexes��������������������������������������������������������������������������������������������������138Detecting Unused and Inefficient Indexes �������������������������������������������������������������������������������������������������������� 138

Index Consolidation ������������������������������������������������������������������������������������������������������������������������������������������� 142

Detecting Suboptimal Queries ��������������������������������������������������������������������������������������������������������������������������� 144

Summary �����������������������������������������������������������������������������������������������������������������������������������148

Part 2: Other things that matter ■ ��������������������������������������������������������������� 149

Chapter 7: Constraints ■ ���������������������������������������������������������������������������������������������������151

Primary Key Constraints ������������������������������������������������������������������������������������������������������������151

Unique Constraints ��������������������������������������������������������������������������������������������������������������������153

Foreign Key Constraints ������������������������������������������������������������������������������������������������������������154

Check Constraints ���������������������������������������������������������������������������������������������������������������������157

Wrapping Up ������������������������������������������������������������������������������������������������������������������������������162

Chapter 8: Triggers ■ ��������������������������������������������������������������������������������������������������������165

DML Triggers �����������������������������������������������������������������������������������������������������������������������������165

DDL Triggers ������������������������������������������������������������������������������������������������������������������������������173

Logon Triggers ���������������������������������������������������������������������������������������������������������������������������175

UPDATE( ) and COLUMNS_UPDATED( ) functions ������������������������������������������������������������������������176

Nested and Recursive Triggers ��������������������������������������������������������������������������������������������������178

First and Last Triggers ���������������������������������������������������������������������������������������������������������������178

CONTEXT_INFO ��������������������������������������������������������������������������������������������������������������������������179

Summary �����������������������������������������������������������������������������������������������������������������������������������179

■ Contents

xii

Chapter 9: Views ■ �����������������������������������������������������������������������������������������������������������181

Regular Views ����������������������������������������������������������������������������������������������������������������������������181

Indexed Views ���������������������������������������������������������������������������������������������������������������������������187

Partitioned Views �����������������������������������������������������������������������������������������������������������������������192

Updatable Views ������������������������������������������������������������������������������������������������������������������������192

Summary �����������������������������������������������������������������������������������������������������������������������������������193

Chapter 10: User-Defined Functions ■ ������������������������������������������������������������������������������195

Multi-Statement Functions ��������������������������������������������������������������������������������������������������������197

Inline Table-Valued Functions����������������������������������������������������������������������������������������������������204

Summary �����������������������������������������������������������������������������������������������������������������������������������208

Chapter 11: XML ■ ������������������������������������������������������������������������������������������������������������209

To Use or Not to Use XML? That Is the Question! ����������������������������������������������������������������������209

XML Data Type ���������������������������������������������������������������������������������������������������������������������������211

Working with XML Data �������������������������������������������������������������������������������������������������������������218

value() Method ��������������������������������������������������������������������������������������������������������������������������������������������������� 219

exists() Method �������������������������������������������������������������������������������������������������������������������������������������������������� 222

query() Method �������������������������������������������������������������������������������������������������������������������������������������������������� 225

nodes() Method �������������������������������������������������������������������������������������������������������������������������������������������������� 226

modify() Method ������������������������������������������������������������������������������������������������������������������������������������������������ 229

OPENXML ����������������������������������������������������������������������������������������������������������������������������������230

FOR XML ������������������������������������������������������������������������������������������������������������������������������������230

Summary �����������������������������������������������������������������������������������������������������������������������������������232

Chapter 12: Temporary Tables ■ ���������������������������������������������������������������������������������������233

Temporary Tables ����������������������������������������������������������������������������������������������������������������������233

Table Variables ��������������������������������������������������������������������������������������������������������������������������241

User-Defined Table Types and Table-Valued Parameters ����������������������������������������������������������245

Regular Tables in tempdb ����������������������������������������������������������������������������������������������������������250

Optimizing tempdb Performance ����������������������������������������������������������������������������������������������253

Summary �����������������������������������������������������������������������������������������������������������������������������������254

■ Contents

xiii

Chapter 13: CLR ■ �������������������������������������������������������������������������������������������������������������255

CLR Integration Overview ����������������������������������������������������������������������������������������������������������255

Security Considerations ������������������������������������������������������������������������������������������������������������257

Performance Considerations �����������������������������������������������������������������������������������������������������261

Summary �����������������������������������������������������������������������������������������������������������������������������������273

Chapter 14: CLR Types ■ ���������������������������������������������������������������������������������������������������275

User-Defined CLR Types ������������������������������������������������������������������������������������������������������������275

Spatial Data Types ���������������������������������������������������������������������������������������������������������������������284

HierarchyId ��������������������������������������������������������������������������������������������������������������������������������292

Summary �����������������������������������������������������������������������������������������������������������������������������������299

Chapter 15: Data Partitioning ■ ����������������������������������������������������������������������������������������301

Reasons to Partition Data ����������������������������������������������������������������������������������������������������������301

When to Partition? ���������������������������������������������������������������������������������������������������������������������303

Data Partitioning Techniques �����������������������������������������������������������������������������������������������������303

Partitioned Tables ���������������������������������������������������������������������������������������������������������������������������������������������� 304

Partitioned Views ����������������������������������������������������������������������������������������������������������������������������������������������� 308

Comparing Partitioned Tables and Partitioned Views ���������������������������������������������������������������������������������������� 315

Using Partitioned Tables and Views Together ���������������������������������������������������������������������������������������������������� 316

Tiered Storage ���������������������������������������������������������������������������������������������������������������������������320

Moving Non-Partitioned Tables Between Filegroups ����������������������������������������������������������������������������������������� 320

Moving Partitions Between Filegroups �������������������������������������������������������������������������������������������������������������� 323

Moving Data Files Between Disk Arrays ������������������������������������������������������������������������������������������������������������ 329

Tiered Storage in Action ������������������������������������������������������������������������������������������������������������������������������������ 333

Tiered Storage and High Availability Technologies �������������������������������������������������������������������������������������������� 337

Implementing Sliding Window Scenario and Data Purge ����������������������������������������������������������338

Potential Issues �������������������������������������������������������������������������������������������������������������������������340

Summary �����������������������������������������������������������������������������������������������������������������������������������347

■ Contents

xiv

Chapter 16: System Design Considerations ■ �������������������������������������������������������������������349

General System Architecture ����������������������������������������������������������������������������������������������������349

Data Access Layer Design ��������������������������������������������������������������������������������������������������������351

Connection Pooling ������������������������������������������������������������������������������������������������������������������������������������������� 352

Working with Database Tables Directly ������������������������������������������������������������������������������������������������������������� 353

Database Views ������������������������������������������������������������������������������������������������������������������������������������������������ 354

Stored Procedures ��������������������������������������������������������������������������������������������������������������������������������������������� 355

Code Generators and ORM Frameworks ������������������������������������������������������������������������������������������������������������ 358

Analyzing Microsoft Entity Framework 6 ����������������������������������������������������������������������������������359

Connections and Transactions ��������������������������������������������������������������������������������������������������������������������������� 361

Executing Stored Procedures and Queries �������������������������������������������������������������������������������������������������������� 363

Data Loading ������������������������������������������������������������������������������������������������������������������������������������������������������ 363

Parameterization ����������������������������������������������������������������������������������������������������������������������������������������������� 365

IN Lists ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 368

Deletions ������������������������������������������������������������������������������������������������������������������������������������������������������������ 369

Optimistic Concurrency ������������������������������������������������������������������������������������������������������������������������������������� 370

Conclusions ������������������������������������������������������������������������������������������������������������������������������������������������������� 371

Summary �����������������������������������������������������������������������������������������������������������������������������������372

Part 3: Locking, Blocking and Concurrency ■ ��������������������������������������������� 373

Chapter 17: Lock Types ■ �������������������������������������������������������������������������������������������������375

Chapter 18: Troubleshooting Blocking Issues ■ ���������������������������������������������������������������387

General Troubleshooting Approach ��������������������������������������������������������������������������������������������387

Troubleshooting Blocking Issues in Real Time ��������������������������������������������������������������������������388

Collecting Blocking Information for Further Analysis ����������������������������������������������������������������392

Summary �����������������������������������������������������������������������������������������������������������������������������������397

Chapter 19: Deadlocks ■ ��������������������������������������������������������������������������������������������������399

Classic Deadlock �����������������������������������������������������������������������������������������������������������������������399

Deadlock Due to Non-Optimized Queries ����������������������������������������������������������������������������������400

Deadlock Due to Simultaneous Read/Update Statements ��������������������������������������������������������403

■ Contents

xv

Deadlock Due to Multiple Updates of the Same Row ����������������������������������������������������������������405

Deadlock Troubleshooting ���������������������������������������������������������������������������������������������������������410

Reducing the Chance of Deadlocks �������������������������������������������������������������������������������������������415

Summary �����������������������������������������������������������������������������������������������������������������������������������416

Chapter 20: Lock Escalation ■ ������������������������������������������������������������������������������������������417

Lock Escalation Overview ���������������������������������������������������������������������������������������������������������417

Lock Escalation Troubleshooting �����������������������������������������������������������������������������������������������420

Summary �����������������������������������������������������������������������������������������������������������������������������������425

Chapter 21: Optimistic Isolation Levels ■ �������������������������������������������������������������������������427

Row Versioning Overview ����������������������������������������������������������������������������������������������������������427

Optimistic Transaction Isolation Levels �������������������������������������������������������������������������������������428

READ COMMITTED SNAPSHOT Isolation Level ��������������������������������������������������������������������������������������������������� 428

SNAPSHOT Isolation Level ��������������������������������������������������������������������������������������������������������������������������������� 429

Version Store Behavior ��������������������������������������������������������������������������������������������������������������436

Summary �����������������������������������������������������������������������������������������������������������������������������������437

Chapter 22: Application Locks ■ ���������������������������������������������������������������������������������������439

Chapter 23: Schema Locks ■ ��������������������������������������������������������������������������������������������443

Schema Modification Locks ������������������������������������������������������������������������������������������������������443

Multiple Sessions and Lock Compatibility ���������������������������������������������������������������������������������446

Lock Partitioning �����������������������������������������������������������������������������������������������������������������������448

Low-Priority Locks (SQL Server 2014) ��������������������������������������������������������������������������������������451

Summary �����������������������������������������������������������������������������������������������������������������������������������454

Chapter 24: Designing Transaction Strategies ■ ��������������������������������������������������������������455

Part 4: Query Life Cycle ■ ���������������������������������������������������������������������������� 459

Chapter 25: Query Optimization and Execution ■ �������������������������������������������������������������461

Query Life Cycle �����������������������������������������������������������������������������������������������������������������������461

Query Optimization ��������������������������������������������������������������������������������������������������������������������462

Query Execution ������������������������������������������������������������������������������������������������������������������������466

■ Contents

xvi

Operators �����������������������������������������������������������������������������������������������������������������������������������472

Joins ������������������������������������������������������������������������������������������������������������������������������������������������������������������ 472

Aggregates �������������������������������������������������������������������������������������������������������������������������������������������������������� 475

Spools ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 477

Parallelism ��������������������������������������������������������������������������������������������������������������������������������������������������������� 480

Query and Table Hints ���������������������������������������������������������������������������������������������������������������484

INDEX Query Hint ����������������������������������������������������������������������������������������������������������������������������������������������� 484

FORCE ORDER Hint �������������������������������������������������������������������������������������������������������������������������������������������� 486

LOOP, MERGE, and HASH JOIN Hints ������������������������������������������������������������������������������������������������������������������ 486

FORCESEEK/FORCESCAN Hints �������������������������������������������������������������������������������������������������������������������������� 486

NOEXPAND/EXPAND VIEWS Hints ����������������������������������������������������������������������������������������������������������������������� 487

FAST N Hints ������������������������������������������������������������������������������������������������������������������������������������������������������ 487

Summary �����������������������������������������������������������������������������������������������������������������������������������488

Chapter 26: Plan Caching ■ ����������������������������������������������������������������������������������������������489

Plan Caching Overview ��������������������������������������������������������������������������������������������������������������489

Parameter Sniffing �������������������������������������������������������������������������������������������������������������������491

Plan Reuse ��������������������������������������������������������������������������������������������������������������������������������498

Plan Caching for Ad-Hoc Queries ���������������������������������������������������������������������������������������������503

Auto-Parameterization ��������������������������������������������������������������������������������������������������������������506

Plan Guides �������������������������������������������������������������������������������������������������������������������������������509

Plan Cache Internals �����������������������������������������������������������������������������������������������������������������514

Examining Plan Cache ���������������������������������������������������������������������������������������������������������������516

Summary �����������������������������������������������������������������������������������������������������������������������������������519

Part 5: Practical Troubleshooting ■ ������������������������������������������������������������� 521

Chapter 27: System Troubleshooting ■ �����������������������������������������������������������������������������523

Looking at the Big Picture ���������������������������������������������������������������������������������������������������������523

Hardware and Network ������������������������������������������������������������������������������������������������������������������������������������� 524

Operating System Configuration ������������������������������������������������������������������������������������������������������������������������ 524

■ Contents

xvii

SQL Server Configuration ���������������������������������������������������������������������������������������������������������������������������������� 525

Database Options ���������������������������������������������������������������������������������������������������������������������������������������������� 527

SQL Server Execution Model �����������������������������������������������������������������������������������������������������527

Wait Statistics Analysis and Troubleshooting ����������������������������������������������������������������������������531

I/O Subsystem and Non-Optimized Queries ������������������������������������������������������������������������������������������������������ 533

Memory-Related Wait Types ������������������������������������������������������������������������������������������������������������������������������ 540

High CPU Load ��������������������������������������������������������������������������������������������������������������������������������������������������� 541

Parallelism ��������������������������������������������������������������������������������������������������������������������������������������������������������� 542

Locking and Blocking ���������������������������������������������������������������������������������������������������������������������������������������� 543

Worker Thread Starvation ���������������������������������������������������������������������������������������������������������������������������������� 543

ASYNC_NETWORK_IO Waits ������������������������������������������������������������������������������������������������������������������������������ 545

Allocation Map Contention and Tempdb load ���������������������������������������������������������������������������������������������������� 547

Wrapping Up ������������������������������������������������������������������������������������������������������������������������������������������������������ 547

What to Do When the Server Is Not Responding �����������������������������������������������������������������������549

Working with Baseline���������������������������������������������������������������������������������������������������������������551

Summary �����������������������������������������������������������������������������������������������������������������������������������551

Chapter 28: Extended Events ■ �����������������������������������������������������������������������������������������553

Extended Events Overview ��������������������������������������������������������������������������������������������������������553

Extended Events Objects �����������������������������������������������������������������������������������������������������������555

Packages ����������������������������������������������������������������������������������������������������������������������������������������������������������� 555

Events ���������������������������������������������������������������������������������������������������������������������������������������������������������������� 556

Predicates ���������������������������������������������������������������������������������������������������������������������������������������������������������� 558

Actions ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 561

Types and Maps ������������������������������������������������������������������������������������������������������������������������������������������������� 562

Targets ��������������������������������������������������������������������������������������������������������������������������������������������������������������� 564

Creating Events Sessions ����������������������������������������������������������������������������������������������������������567

Working with Event Data �����������������������������������������������������������������������������������������������������������569

Working with the ring_buffer Target ����������������������������������������������������������������������������������������������������������������� 570

Working with event_file and asynchronous_file_target Targets ���������������������������������������������������������������������� 571

■ Contents

xviii

Working with event_counter and synchronous_event_counter Targets ���������������������������������������������������������� 573

Working with histogram, synchronous_bucketizer, and asynchronous_bucketizer Targets ���������������������������� 575

Working with the pair_matching Target ������������������������������������������������������������������������������������������������������������ 578

System_health and AlwaysOn_Health Sessions �����������������������������������������������������������������������579

Detecting Expensive Queries �����������������������������������������������������������������������������������������������������580

Summary �����������������������������������������������������������������������������������������������������������������������������������582

Part 6: Inside the transaction log ■ ������������������������������������������������������������� 583

Chapter 29: Transaction Log Internals ■ ��������������������������������������������������������������������������585

Data Modifications, Logging, and Recovery ������������������������������������������������������������������������������585

Delayed Durability (SQL Server 2014) ���������������������������������������������������������������������������������������591

Virtual Log Files ������������������������������������������������������������������������������������������������������������������������592

Database Recovery Models �������������������������������������������������������������������������������������������������������594

TempDB Logging �����������������������������������������������������������������������������������������������������������������������597

Excessive Transaction Log Growth ��������������������������������������������������������������������������������������������597

Transaction Log Management ��������������������������������������������������������������������������������������������������600

Summary �����������������������������������������������������������������������������������������������������������������������������������601

Chapter 30: Designing a Backup Strategy ■ ���������������������������������������������������������������������603

Database Backup Types ������������������������������������������������������������������������������������������������������������603

Backing Up the Database ����������������������������������������������������������������������������������������������������������605

Restoring the Database �������������������������������������������������������������������������������������������������������������606

Restore to a Point in Time ���������������������������������������������������������������������������������������������������������������������������������� 608

Restore with StandBy ���������������������������������������������������������������������������������������������������������������������������������������� 611

Designing a Backup Strategy ����������������������������������������������������������������������������������������������������611

Partial Database Availability and Piecemeal Restore ����������������������������������������������������������������616

Partial Database Backup �����������������������������������������������������������������������������������������������������������620

Backup to Windows Azure ���������������������������������������������������������������������������������������������������������621

Managed Backup to Windows Azure �����������������������������������������������������������������������������������������623

Summary �����������������������������������������������������������������������������������������������������������������������������������624

■ Contents

xix

Chapter 31: Designing a High Availability Strategy ■ �������������������������������������������������������625

SQL Server Failover Cluster�������������������������������������������������������������������������������������������������������625

Database Mirroring��������������������������������������������������������������������������������������������������������������������630

AlwaysOn Availability Groups ����������������������������������������������������������������������������������������������������636

Log Shipping �����������������������������������������������������������������������������������������������������������������������������638

Replication ��������������������������������������������������������������������������������������������������������������������������������640

Designing a High Availability Strategy ���������������������������������������������������������������������������������������643

Summary �����������������������������������������������������������������������������������������������������������������������������������645

Part 7: In-Memory OLTP Engine ■ ���������������������������������������������������������������� 647

Chapter 32: In-Memory OLTP Internals ■ ������������������������������������������������������������������������649

Why Hekaton? ��������������������������������������������������������������������������������������������������������������������������649

In-Memory OLTP Engine Architecture and Data Structures �������������������������������������������������������650

Memory-Optimized Tables ��������������������������������������������������������������������������������������������������������������������������������� 652

High-Availability Technology Support ���������������������������������������������������������������������������������������������������������������� 653

Data Row Structure ������������������������������������������������������������������������������������������������������������������������������������������� 654

Hash Indexes ����������������������������������������������������������������������������������������������������������������������������������������������������� 655

Range Indexes ��������������������������������������������������������������������������������������������������������������������������������������������������� 660

Statistics on Memory-Optimized Tables ������������������������������������������������������������������������������������������������������������ 667

Garbage Collection �������������������������������������������������������������������������������������������������������������������������������������������� 668

Transactions and Concurrency ��������������������������������������������������������������������������������������������������669

Transaction Isolation Levels and Data Consistency ������������������������������������������������������������������������������������������� 669

In-Memory OLTP Transaction Isolation Levels ��������������������������������������������������������������������������������������������������� 670

Cross-Container Transactions ���������������������������������������������������������������������������������������������������������������������������� 674

Data Access, Modifications, and Transaction Lifetime ��������������������������������������������������������������������������������������� 676

Transaction Logging ������������������������������������������������������������������������������������������������������������������������������������������ 679

Data Durability and Recovery ����������������������������������������������������������������������������������������������������681

Memory Usage Considerations��������������������������������������������������������������������������������������������������686

Summary �����������������������������������������������������������������������������������������������������������������������������������687

■ Contents

xx

Part 8: Columnstore Indexes ■ �������������������������������������������������������������������� 689

Chapter 33: In-Memory OLTP Programmability ■ ������������������������������������������������������������691

Native Compilation ��������������������������������������������������������������������������������������������������������������������691

Natively-Compiled Stored Procedures ��������������������������������������������������������������������������������������695

Optimization of Natively-Compiled Stored Procedures �������������������������������������������������������������������������������������� 696

Creating Natively-Compiled Stored Procedures ������������������������������������������������������������������������������������������������ 696

Supported T-SQL Features ��������������������������������������������������������������������������������������������������������������������������������� 699

Execution Statistics ������������������������������������������������������������������������������������������������������������������������������������������� 700

Interpreted T-SQL and Memory-Optimized Tables ���������������������������������������������������������������������702

Memory-Optimized Table Types and Variables ��������������������������������������������������������������������������703

In-Memory OLTP: Implementation Considerations ��������������������������������������������������������������������705

Summary �����������������������������������������������������������������������������������������������������������������������������������707

Chapter 34: Introduction to Columnstore Indexes ■ ��������������������������������������������������������709

Data Warehouse Systems Overview ������������������������������������������������������������������������������������������709

Columnstore Indexes and Batch-Mode Processing Overview ���������������������������������������������������712

Column-Based Storage and Batch-Mode Execution ������������������������������������������������������������������������������������������ 713

Columnstore Indexes and Batch-Mode Execution in Action ������������������������������������������������������������������������������ 716

Columnstore Index Internals �����������������������������������������������������������������������������������������������������723

Nonclustered Columnstore Indexes ������������������������������������������������������������������������������������������������������������������� 723

Data Storage ������������������������������������������������������������������������������������������������������������������������������������������������������ 727

Metadata ����������������������������������������������������������������������������������������������������������������������������������������������������������� 730

Design Considerations and Best Practices for Columnstore Indexes ����������������������������������������733

Reducing Data Row Size������������������������������������������������������������������������������������������������������������������������������������ 733

Giving SQL Server as Much Information as Possible ����������������������������������������������������������������������������������������� 733

Maintaining Statistics ���������������������������������������������������������������������������������������������������������������������������������������� 734

Avoiding String Columns in Fact Tables ������������������������������������������������������������������������������������������������������������� 734

Summary �����������������������������������������������������������������������������������������������������������������������������������736

■ Contents

xxi

Chapter 35: Clustered Columnstore Indexes ■ �����������������������������������������������������������������737

Internal Structure of Clustered Columnstore Indexes ���������������������������������������������������������������737

Internal Structure ���������������������������������������������������������������������������������������������������������������������������������������������� 737

Data Load ���������������������������������������������������������������������������������������������������������������������������������������������������������� 739

Delta Store and Delete Bitmap ������������������������������������������������������������������������������������������������������������������������� 740

Archival Compression ���������������������������������������������������������������������������������������������������������������������������������������� 745

Clustered Columnstore Index Maintenance ������������������������������������������������������������������������������747

Excessive Number of Partially Populated Row Groups �������������������������������������������������������������������������������������� 747

Large Delta Stores ��������������������������������������������������������������������������������������������������������������������������������������������� 748

Large Delete Bitmap ������������������������������������������������������������������������������������������������������������������������������������������ 748

Index Maintenance Options ������������������������������������������������������������������������������������������������������������������������������� 749

SQL Server 2014 Batch-Mode Execution Enhancements ����������������������������������������������������������750

Design Considerations ���������������������������������������������������������������������������������������������������������������752

Summary �����������������������������������������������������������������������������������������������������������������������������������753

Index ���������������������������������������������������������������������������������������������������������������������������������755

xxiii

About the Author

Dmitri Korotkevitch is a Microsoft SQL Server MVP and Microsoft Certified Master (SQL Server 2008) with almost 20 years of IT experience including over 14 years of experience working with Microsoft SQL Server as an Application and Database Developer, Database Administrator, and Database Architect. Dmitri specializes in the design, development, and performance tuning of complex OLTP systems that handle thousands of transactions per second around the clock.

Dmitri regularly speaks at various Microsoft and SQL PASS events, and he provides SQL Server training to clients around the world. He blogs at: http://aboutsqlserver.com, and he can be reached at [email protected].

xxv

About the Technical Reviewers

Thomas LaRock is a Microsoft Certified Master, a SQL Server MVP, a VMWare vExpert, and a Microsoft Certified Trainer. He has over 15 years’ experience in the IT industry in various roles, including programmer, developer, analyst, and database administrator. Thomas is currently serving as the Head Geek for SolarWinds, a role that allows him to solve problems for a variety of customers regarding database performance tuning and virtualization.

Victor Isakov is a Microsoft Certified Architect, Microsoft Certified Master, Microsoft Certified Trainer, and Microsoft MVP with over 20 years’ experience with SQL Server. Victor regularly speaks at conferences internationally, including IT/Dev Connections, Microsoft TechEd, and the PASS Summit. He has written a number of books on SQL Server, and he has worked on numerous projects for Microsoft, developing SQL Server courseware, certification, and exams. In 2007, Victor was invited by Microsoft to attend the SQL Ranger program in Redmond. Consequently, he became one of the first IT professionals to achieve the Microsoft Certified Master and Microsoft Certified Architect certifications globally.

xxvii

Acknowledgments

IT projects are never done on time, and this book is no exception. It is hard to believe now, but back in March of 2013, I expected it to be small, 450-page book completed within 7 months. Now, 14 months and almost 900 pages of manuscript later, I can say that it was a very hard but exciting journey, which I would not have been able to travel alone without the help of others.

I am very grateful to my technical reviewers—Tom LaRock and Victor Isakov—for their enormous contribution to the project. Their suggestions and advice helped me shape the book into its current form and dramatically improve its quality.

The same applies to the entire Apress team and especially to Kevin Shea, Jon Hassell, and Gary Schwartz who put a lot of effort into the project. At the very least, they succeeded in the challenging task of keeping me organized and making my English sound plausible.

I would like to thank my friend Maxim Alexeyev for his help with Chapter 16. Maxim lives in Tampa, FL and works as a System Architect in Actsoft, Inc. He blogs at: http://discoveringdotnet.alexeyev.org.

Of course, I would not be able to finish this book without the help of my family and friends who supported me throughout the process, patiently waiting for the work to be completed.

Thank you very much! It was a pleasure to work with all of you!

xxix

Introduction

Several people asked me the same question during the time I worked on this book. “Why have you decided to write yet another book on SQL Server Internals? There are plenty of books on this subject out there, including an excellent one by Kalen Delaney et al., the latest version being entitled, Microsoft SQL Server 2012 Internals, Developer Reference series (Microsoft Press 2013).

To be absolutely honest, I asked myself the same question while I toyed with the idea of writing that book. In the end, I defined two goals:

1� I wanted to write a book that explains how SQL Server works while keeping the content as practical as possible.

2� I wanted the book to be useful to both database administrators and developers.

There is a joke in SQL Server community: “How do you distinguish between junior- and senior-level database professionals? Just ask them any question about SQL Server. The junior-level person gives you the straight answer. The senior-level person, on the other hand, always answers, “It depends.”

As strange as it sounds, that is correct. SQL Server is a very complex product with a large number of components that depend on each other. You can rarely give a straight yes or no answer to any question. Every decision comes with its own set of strengths and weaknesses and leads to consequences that affect other parts of the system.

This book talks about on what “it depends”. My goal is to give you enough information about how SQL Server works and to show you various examples of how specific database designs and code patterns affect SQL Server behavior. I tried to avoid generic suggestions based on best practices. Even though those suggestions are great and work in a large number of cases, there are always exceptions. I hope that, after you read this book, you will be able to recognize those exceptions and make decisions that benefit your particular systems.

My second goal is based on the strong belief that the line between database administration and development is very thin. It is impossible to be a successful database developer without knowledge of SQL Server Internals. Similarly, it is impossible to be a successful database administrator without the ability to design efficient database schema and to write good T-SQL code. That knowledge also helps both developers and administrators to better understand and collaborate with each other, which is especially important nowadays in the age of agile development and multi-terabyte databases.

I have worn both hats in my life. I started my career in IT as an application developer, slowly moving to backend and database development over the years. At some point, I found that it was impossible to write good T-SQL code unless I understood how SQL Server executes it. That discovery forced me to learn SQL Server Internals, and it led to a new life where I design, develop, and tune various database solutions. I do not write client applications anymore; however, I perfectly understand the challenges that application developers face when they deal with SQL Server. I have “been there and done that.”

I still remember how hard it was to find good learning materials. There were plenty of good books; however, all of them had a clear separation in their content. They expected the reader either to be developer or database administrator — never both. I tried to avoid that separation in this book. Obviously, some of the chapters are more DBA-oriented, while others lean more towards developers. Nevertheless, I hope that anyone who is working with SQL Server will find the content useful.

Nevertheless, do not consider this book a SQL Server tutorial. I expect you to have previous experience working with relational databases — preferably with SQL Server. You need to know RDBMS concepts, be familiar with different types of database objects, and be able to understand SQL code if you want to get the most out of this book.

■ IntroduCtIon

xxx

Finally, I would like to thank you for choosing this book and for your trust in me. I hope that you will enjoy reading it as much as I enjoyed writing it.

How This Book Is StructuredThe book is logically separated into eight different parts. Even though all of these parts are relatively independent of each other, I would encourage you to start with Part 1, “Tables and Indexes” anyway. This part explains how SQL Server stores and works with data, which is the key point in understanding SQL Server Internals. The other parts of the book rely on this understanding.

The Parts of the book are as follows:

Part 1: Tables and Indexes covers how SQL Server works with data. It explains the internal structure of database tables; discusses how and when SQL Server uses indexes, and provides you with the basic guidelines about how to design and maintain them.

Part 2: Other Things That Matter provides an overview of different T-SQL objects, and it outlines their strengths and weaknesses along with use-cases when they should or should not be used. Finally, this part discusses data partitioning, and provides general system design considerations for systems that utilize SQL Server as a database backend.

Part 3: Locking, Blocking, and Concurrency talks about the SQL Server concurrency model. It explains the root-causes of various blocking issues in SQL Server, and it shows you how to troubleshoot and address them in your systems. Finally, this part provides you with a set of guidelines on how to design transaction strategies in a way that improves concurrency in systems.

Part 4: Query Life Cycle discusses the optimization and execution of queries in SQL Server. Moreover, it explains how SQL Server caches execution plans and it demonstrates several plan-caching–related issues commonly encountered in systems.

Part 5: Practical Troubleshooting provides an overview of the SQL Server Execution Model, and it explains how you can quickly diagnose systems and pinpoint the root-causes of the problems.

Part 6: Inside the Transaction Log explains how SQL Server works with the transaction log, and it gives you a set of guidelines on how to design Backup and High Availability strategies in systems.

Part 7: In-Memory OLTP Engine (Hekaton) talks about the new in-memory OLTP engine introduced in SQL Server 2014. It explains how Hekaton works internally and how you can work with memory-optimized data in your systems.

Part 8: Columnstore Indexes provides an overview of columnstore indexes, which can dramatically improve the performance of Data Warehouse solutions. It covers nonclustered columnstore indexes, which were introduced in SQL Server 2012, along with clustered columnstore indexes, introduced in SQL Server 2014.

As you may have already noticed, this book covers multiple SQL Server versions including the recently released SQL Server 2014. I have noted version-specific features whenever necessary; however, most of the content is applicable to any SQL Server version, starting with SQL Server 2005.

It is also worth noting that most of the figures and examples in this book were created in the Enterprise Edition of SQL Server 2012 with parallelism disabled on the server level in order to simplify the resulting execution plans. In some cases, you may get slightly different results when you run scripts in your environment using different versions of SQL Server.

■ IntroduCtIon

xxxi

Downloading the CodeYou can download the code used in this book from the Source Code section of the Apress web site (www.apress.com) or from the Publications section of my blog (http://aboutsqlserver.com). The source code consists of SQL Server Management Studio solutions, which include a set of the projects (one per chapter). Moreover, it includes several .Net C# projects, which provide the client application code used in the examples in Chapters 12, 13, 14, and 16.

Contacting the AuthorYou can visit my blog at: http://aboutsqlserver.com or email me at: [email protected].