MSSQL lock basics tutorial

Source: Internet
Author: User
. Unlocked
Locking is a mechanism used by the database engine to synchronize simultaneous access to the same data block by multiple users.
Before a transaction obtains the dependency of the current status of a data block, it must protect itself from the impact of modifications to the same data by other transactions.

Lock granularity and hierarchy

RID is used to lock the row identifier of a single row in the heap.
the row lock used in the key index to protect the key range in a serializable transaction.
8 KB page in the page database, for example, data page or index page.
extent is a group of eight consecutive pages, such as data pages or index pages.
HOBT heap or B-tree. Protects the data page heap locks of indexes or tables without clustered indexes.
A table contains all the data and indexes of the entire table.
file database file.
resources dedicated to the application Program .
metadata lock.
allocation_unit: Allocation unit.
the entire database.

Lock mode:
Share lock (s): used for read-only operations (select) to lock shared resources. The shared lock does not stop other users from reading, but prevents other users from writing and modifying.
Exclusive lock (x): used for data modification operations, such as insert, update, or delete. Make sure that the same resource is not updated at the same time.
Update lock (u): To update transaction data, you must first obtain the update lock. To prevent deadlocks, the engine allows a transaction to obtain the update lock (U Lock) of resources at a time. Only queries that obtain the update lock can create an exclusive lock.
Intention lock: used to establish the lock hierarchy. Intention lock types include: Intention sharing (is), intention exclusive (IX), and intention exclusive sharing (six ). Before creating a shared lock and an exclusive lock, the engine uses an intent lock to protect the shared lock (s lock) or exclusive lock (x lock) from being placed on the underlying resources of the lock hierarchy.
Schema lock (Sch-m): used to execute Table Data Definition Language (DDL) operations. When the schema modification lock takes effect, all operations beyond the lock will be blocked.
Bulk update lock: A bulk update lock is used when you copy large data volumes to a table, specify the tablock prompt, or use sp_tableoption to set the table lock on bulk table option. The large-capacity update lock (Bu lock) allows multiple threads to concurrently load data to the same table in large capacity, and prevents other processes that do not load data in large capacity from accessing the table.

Monitoring and Management lock

Use SQL Server Profiler to monitor locks events to capture information about lock events in the trail.
Use System Monitor to monitor SQL Server locks objects and Database Engine instance lock levels.
Query the SYS. dm_tran_locks dynamic management view to obtain information about the current lock status in the Database Engine instance.
Use the system stored procedure sp_lock (TRANSACT-SQL) to return information about the active locks in Database Engine instances. For SQL Server 2005, use the SYS. dm_tran_locks dynamic management view.
Use System View SYS. syslockinfo (TRANSACT-SQL) to return information about the active locks in Database Engine instances. For SQL Server 2005, use the SYS. dm_tran_locks dynamic management view.

Minimize deadlocks

access objects in the same order.
avoid user interaction in transactions.
keep the transaction brief and in a batch.
use a lower isolation level.
use the row version-based isolation level.
set the read_committed_snapshot database option to on to allow committed read transactions to use row version control.
Use Snapshot isolation.
use the bound connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.