Explaing locking in SQL Server

In SQL Server locking can be done at various levels, the levels are as follows:
  • RID - this is used to lock a single row
  • Key - this is used to lock rows within an index
  • Page - this is used to lock a 8 KB data page or index page
  • Extent - this is used to lock contiguous 8 data pages or index pages
  • Table - this is used to lock a table including the data and index
  • Database - this is used to lock the full database
The various types of locking that are available are as follows:
  • Shared Lock (S)
    Shared lock is used for concurrent read operations (SELECT), once the read operation is done the lock is released unless the lock has been specified as a repeated process or a locking hint has been used to keep the lock till the duration of the transaction.
  • Update Lock (U)
    This type of lock is used on resources that can be updated and prevents the common type of deadlock where by two transactions are trying to update at the same time. Only one transaction can obtain an Update lock, if any changes are to be made then the lock is changed to Exclusive lock otherwise it is changed to Shared Lock.
  • Exclusive Lock (X)
    This type of lock is used to make sure that the data is not updated by multiple transactions at one point of time, this includes the (INSERT, UPDATE, DELETE) statements.
  • Intent Lock
    This type of lock is used to show the intent of getting a Shared or Exclusive lock at a lower level resource. The various types of intent locks that are available are as follows:
    • Intent Shared (IS)
      This is issued by a transaction when it wants to read some resources
    • Intent Exclusive (IX)
      This is issued by a transaction when it wants to update some resources
    • Shared with Intent Exclusive (SIX)
      This is issues by a transaction with the intention of reading some resources and then modify some of them
  • Schema Lock (Sch)
    This type of lock is used when changes to the schema of the table is done, the two types of schema locks that are there are as follows:
    • Schema-Modification Lock (Sch-M)
      This type of lock is acquired when DDL statements are being executed
    • Schema-Stability Lock (Sch-S)
      This type of lock is acquired when the query is being compiled, at this point no other transaction locks are blocked.
  • Bulk Update Lock (BU)
    This lock is used when data is being uploaded to a table in bulk.

Comments

Popular posts from this blog

jQuery Basics

What is the difference between a Page Layout and Master Page in SharePoint?

Accessing data from SharePoint 2010 to an ASP.NET application