This is a guest post from my friend Kevin Kline. Kevin serves as Principal Program Manager at SentryOne. He is a founder and former president of PASS and the author of popular IT books like SQL in a Nutshell. Kevin is a renowned database expert, software industry veteran, Microsoft SQL Server MVP and long-time blogger at SentryOne. As a noted leader in the SQL Server community, he blogs about Microsoft Data Platform features and best practices, SQL Server trends as well as professional development for data professionals.
Understanding the way that SQL server blocking works is key to ensuring that your database is able to run smoothly and optimally, yet this can be a tricky subject to unpick if you are new to it.
You can get an in-depth explanation of SQL server blocking by reading this guide from SentryOne, but for a briefer introduction to what is involved, read on.
Image Source: Pixabay
Basics of blocking
In certain SQL server systems, two or more processes may need to make use of the hardware and software resources available to them simultaneously. The nature of the process will determine whether or not it is granted ‘lock’ status, which effectively allows it to gain priority over other processes and leverage the resources ahead of them.
Any processes which are competing with locked processes are referred to as being blocked, since they are effectively being made to wait in a queue until the prioritized, locked process has been completed and the resources are freed up.
Server blocking is vital to ensuring that the integrity of the data remains uncompromised and that the entire ecosystem is able to operate as usual.
To appreciate why and when blocking occurs, it is necessary to look at the different types of lock modes that are available in the SQL server environment.
The most potent of the pack is the Exclusive lock, which is generally applied to make sure that data is modified in a logical order, rather than allowing multiple processes to tinker with it at once. Update is another lock mode that, as the name suggests, is applied during updates so that they follow the proper sequence.
Locks can be applied at several levels, from row level to the page, table and the database, with this hierarchy being useful in determining where blocks might originate.
While SQL server blocking can be considered an entirely routine aspect of database operation, there are circumstances in which problems can arise.
For example, blocking may cascade across a plethora of processes in a chain if one process is blocked by a locked process and in turn passes this onto subsequent processes that are waiting to be executed.
Of course working out whether a particular block should be considered an issue or not is not always straightforward, but in general so long as the block does not last more than five seconds or so it should not be too disruptive.
SQL server deadlocks are closely related to blocking, although they are not exactly the same. Rather than effectively queuing processes through locks to allow for ordered execution, they occur when more than one process has an exclusive lock over specific resources, meaning neither can be completed.
In this instance, the server will terminate one of the conflicting processes, and like blocking a deadlock is not intrinsically bad, but needs to be monitored and managed by the administrator.
Making use of monitoring
As you may have gathered, while SQL server blocking is commonplace, acceptable and ultimately necessary to keep a database in good order, it is also something which it pays to keep tabs on to ensure seamless operation.
It is possible to do this manually, although this is a very labor-intensive procedure. Instead using a modern monitoring solution to automatically track blocks and provide alerts if problems arise is much more efficient.
Furthermore it may be helpful to use monitoring software which is capable of expressing blocking data visually, so that it can be digested quickly. As your experience with managing an SQL server grows, your skill with weeding out worrisome blocks will improve, but even veterans need the right tools to make their lives easier.
If you found this post useful, please see Sentry One’s guide on SQL Server Blocking at https://www.sentryone.com/sql-server/sql-server-blocking.