This is third part of "Introduction to SQL Server Transaction" series. You can see previous sections below:
In this part we discuss how SQL server has implemented concurrency control.
Locking and Versioning
SQL server uses following two techniques to implement concurrency control:
- Locking
- Versioning
Locking
Locking is the traditional mechanism SQL Server uses to isolate transactions.
When a transaction accesses data, SQL Server places locks on the data to prevent other transactions from making conflicting changes. Locking type and granularity decide the effect of the lock and the scale.
Locking Types:
There are different types of locks SQL server can placed. Each lock type has some level of restrictions for other transactions. Here is summary of locking types and what it blocks:
We will have a talk about type of locks and locking in detail in future blogs.
Locking Granularity: Locks can be applied at row level, page level, table level, or even database level. Granularity allows SQL server to not to lock more objects than it required.
When lock is placed on row level, only that row is restricted from accessed by other transactions. Other rows are free to read and write operations, from other transactions. This reduce the blocking
Page level locks on the other hand locks all rows in that page from read or modification (depend on lock type). Same with table and database level locks, they lock more rows, hence more data and prone for more blocking issues.
Locking is primarily used in Read Committed, Repeatable Read, and Serializable isolation levels.
Versioning
Versioning uses a multi-version concurrency control (MVCC) approach. Instead of locking data for readers, SQL Server maintains row versions (multiple version of a row) in tempdb, allowing readers to see a consistent snapshot of the data.
When a row is modified, SQL Server keeps an older version in tempdb so that readers can still access the version valid at the start of their transaction or query.
This is a much recent technique and Isolation levels using versioning are Read Committed Snapshot (RCSI) and Snapshot Isolation (SI).
Versioning helps reduce blocking and deadlocks, improving concurrency.
No comments:
Post a Comment