Dmitri Korotkevitch
Land O Lakes, Florida, USA
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484239568 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-3956-8 e-ISBN 978-1-4842-3957-5
https://doi.org/10.1007/978-1-4842-3957-5
Library of Congress Control Number: 2018958877
Dmitri Korotkevitch 2018
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
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, email orders-ny@springer-sbm.com, 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.
Introduction
Some time ago, one of my colleagues asked me, What do you like about SQL Server the most? I had heard this question many times before, and so I provided my usual answer: SQL Server Internals. I like to understand how the product works and solve complex problems with this knowledge.
His next question was not so simple though: How did you fall in love with SQL Server Internals? After some time thinking, I answered, Well, I guess it started when I had to work on the locking issues. I had to learn SQL Server Internals to troubleshoot complex deadlocks and blocking conditions. And I enjoyed the sense of satisfaction those challenges gave me.
This is, in fact, the truth. The Concurrency Model has always been an essential part of my SQL Server journey, and I have always been fascinated by it. Concurrency is, perhaps, one of the most confusing and least understood parts of SQL Server, but, at the same time, it is also quite logical. The internal implementation is vaguely documented; however, as soon as you grasp the core concepts, everything starts to fit together nicely.
It is also fair to say that concurrency topics have always been my favorites. My first few SQL Saturday presentations and first few blog posts were about locking and blocking. I even started to write my first book, the first edition of Pro SQL Server Internals , from Chapter the first chapter in the Locking, Blocking, and Concurrency partbefore going back to write the beginning.
Those few chapters, by the way, were the first and worst chapters I have ever written. I am very glad that I had an opportunity to revisit them in the second edition of Internals book. Nevertheless, I was unable to cover the subject as deeply as I wanted to due to deadlines and space constraints (I am sure that Apress regularly ran out of paper printing the 900-page manuscript in its current form). Thus, I am very glad that I can present you with a separate book on SQL Server locking, blocking, and concurrency now.
If you have read Pro SQL Server Internals before, you will notice some familiar content. Nevertheless, I did my best to expand the coverage of the old topics and added quite a few new ones. I also made many changes in the demo scripts and added the new Blocking Monitoring Framework code, which dramatically simplifies troubleshooting concurrency issues in the system.
This book covers all modern versions of SQL Server, starting with SQL Server 2005, along with Microsoft Azure SQL Databases. There may be a few very minor version-specific differences; however, conceptually the SQL Server Concurrency Model has not changed much over the years.
Nor do I expect it to dramatically change in the near future, so this book should be applicable to at least several future versions of SQL Server.
Finally, I would like to thank you again 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
This book consists of 14 chapters and is structured in the following way:
Chapter , Data Storage and Access Methods, describes how SQL Server stores and works with the data in disk-based tables. This knowledge is the essential cornerstone to understanding the SQL Server Concurrency Model.
Chapter , Transaction Management and Concurrency Models, provides an overview of optimistic and pessimistic concurrency and focuses on transaction management and error handling in the system.
Chapter , Lock Types, explains the key elements of SQL Server concurrency, such as lock types.
Chapter , Blocking in the System, discusses why blocking occurs in the system and shows how to troubleshoot it.
Chapter , Deadlocks, demonstrates the common causes of deadlocks and outlines how to address them.
Chapter , Optimistic Isolation Levels, covers optimistic concurrency in SQL Server.
Chapter , Lock Escalations, talks about lock escalation techniques that SQL Server uses to reduce locking overhead in the system.
Chapter , Schema and Low-Priority Locks, covers the schema locks that occur during schema modifications in the database. It also explains low-priority locks that may help to reduce blocking during index and partition management in recent versions of SQL Server.
Chapter , Lock Partitioning, discusses lock partitioning, which SQL Server uses in systems that have 16 or more logical CPUs.
Chapter , Application Locks, focuses on application locks that can be created in the code programmatically.
Chapter , Designing a Transaction Strategy, provides guidelines on how to design transaction strategies in the system.