Pro SQL server internals

776 102 0
Pro SQL server internals

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

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

Thông tin tài liệu

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index Please use the Bookmarks and Contents at a Glance links to access them www.it-ebooks.info Contents at a Glance About the Author������������������������������������������������������������������������������������������������������������� xxiii About the Technical Reviewers���������������������������������������������������������������������������������������� xxv Acknowledgments���������������������������������������������������������������������������������������������������������� xxvii Introduction��������������������������������������������������������������������������������������������������������������������� xxix ■■Part 1: Tables and Indexes��������������������������������������������������������������������������� ■■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 v www.it-ebooks.info ■ Contents at a Glance ■■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 vi www.it-ebooks.info ■ Contents at a Glance ■■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 vii www.it-ebooks.info 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: I wanted to write a book that explains how SQL Server works while keeping the content as practical as possible I wanted the book to be useful to both database administrators and developers There is a joke in SQL Server community: “How 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 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, 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 xxix www.it-ebooks.info ■ Introduction 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 Structured The 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 xxx www.it-ebooks.info ■ Introduction Downloading the Code You 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 Author You can visit my blog at: http://aboutsqlserver.com or email me at: dk@aboutsqlserver.com xxxi www.it-ebooks.info Part Tables and Indexes www.it-ebooks.info Chapter Data Storage Internals SQL Server database is a collection of objects that allow you to store and manipulate data In theory, SQL Server supports 32,767 databases per instance, although the typical installation usually has only several databases Obviously, the number of the databases SQL Server can handle depends on the load and hardware It is not unusual to see servers hosting dozens or even hundreds of small databases In this chapter, we will discuss the internal structure of the databases, and will cover how SQL Server stores the data Database Files and Filegroups Every database consists of one or more transaction log files and one or more data files A transaction log stores information about database transactions and all of the data modifications made in each session Every time the data is modified, SQL Server stores enough information in the transaction log to undo (rollback) or redo (replay) this action ■■Note  We will talk about the transaction log in greater detail in Part of this book “Inside the Transaction Log.” Every database has one primary data file, which by convention has an mdf extension In addition, every database can also have secondary database files Those files, by convention, have ndf extensions All database files are grouped into the filegroups A filegroup is a logical unit that simplifies database administration It permits the logical separation of database objects and physical database files When you create database objects-tables, for example, you specify into what filegroup they should be placed without worrying about the underlying data files’ configuration Listing 1-1 shows the script that creates a database with name OrderEntryDb This database consists of three filegroups The primary filegroup has one data file stored on the M: drive The second filegroup, Entities, has one data file stored on the N: drive The last filegroup, Orders, has two data files stored on the O: and P: drives Finally, there is a transaction log file stored on the L: drive Listing 1-1.  Creating a database create database [OrderEntryDb] on primary (name = N'OrderEntryDb', filename = N'm:\OEDb.mdf'), filegroup [Entities] (name = N'OrderEntry_Entities_F1', filename = N'n:\OEEntities_F1.ndf'), filegroup [Orders] (name = N'OrderEntry_Orders_F1', filename = N'o:\OEOrders_F1.ndf'), www.it-ebooks.info ■ Contents ■■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 x www.it-ebooks.info ■ Contents 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�������������������������������������������������������������������������������������125 Design Guidelines���������������������������������������������������������������������������������������������������������������������������������������������� 125 Identities, Sequences, and Uniqueidentifiers���������������������������������������������������������������������������������������������������� 130 Nonclustered Indexes Design Considerations���������������������������������������������������������������������������135 Optimizing and Tuning Indexes�������������������������������������������������������������������������������������������������138 Detecting 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 xi www.it-ebooks.info ■ Contents ■■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 xii www.it-ebooks.info ■ Contents ■■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 xiii www.it-ebooks.info ■ Contents ■■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 ����������������������������������������������������������������������������������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 xiv www.it-ebooks.info ■ Contents 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 xv www.it-ebooks.info ■ Contents 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 xvi www.it-ebooks.info ■ Contents 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 xvii www.it-ebooks.info ■ Contents 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 xviii www.it-ebooks.info ■ Contents ■■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 xix www.it-ebooks.info ■ Contents ■■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 xx www.it-ebooks.info ■ Contents ■■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 xxi www.it-ebooks.info 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 dk@aboutsqlserver.com xxiii www.it-ebooks.info 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 xxv www.it-ebooks.info 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 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! xxvii www.it-ebooks.info ... (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... blog at: http://aboutsqlserver.com or email me at: dk@aboutsqlserver.com xxxi www.it-ebooks.info Part Tables and Indexes www.it-ebooks.info Chapter Data Storage Internals SQL Server database is... Buffer pool, and SQL Server needs to read the data page from the disk When the page is in memory, SQL Server updates the data row This process includes two different steps: First SQL Server generates

Ngày đăng: 27/03/2019, 09:42

Từ khóa liên quan

Mục lục

  • Contents at a Glance

  • Contents

  • About the Author

  • About the Technical Reviewers

  • Acknowledgments

  • Introduction

  • Part 1: Tables and Indexes

    • Chapter 1: Data Storage Internals

      • Database Files and Filegroups

      • Data Pages and Data Rows

      • Large Objects Storage

        • Row-Overflow Storage

        • LOB Storage

        • SELECT * and I/O

        • Extents and Allocation Map Pages

        • Data Modifications

        • Much Ado About Data Row Size

        • Table Alteration

        • Summary

        • Chapter 2: Tables and Indexes: Internal Structure and Access Methods

          • Heap Tables

          • Clustered Indexes

          • Composite Indexes

          • Nonclustered Indexes

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

Tài liệu liên quan