30 minutes comprehensive analysis - SQL transaction + isolation level + blocking + deadlock

read table of contents

 In the past, I always pursued new things, and found the foundation is the most important. This year's main goal is to be proficient in SQL query and SQL performance optimization.

 This series is mainly a summary of T-SQL.

[T-SQL basics] 01. Single table query - several sql query questions

[T-SQL basics] 02. Join query

[T-SQL Basics] 03. Subqueries

[T-SQL Basics] 04. Table Expressions - Part 1

[T-SQL Basics] 04. Table Expression - Part 2

[T-SQL basics] 05. Set operations

[T-SQL Basics] 06. Perspective, inverse pivot, grouping set

[T-SQL basics] 07. Data modification

[T-SQL Basics] 08. Transactions and Concurrency

[T-SQL Basics] 09. Programmable Objects

----------------------------------------------------------

[T-SQL Advanced] 01. Easy-to- use SQL TVP~~Exclusive example of [add-delete-modify-check]

 ----------------------------------------------------------

[T-SQL performance tuning] 01. TempDB usage and performance issues

[T-SQL performance tuning] 02. Usage and performance problems of Transaction Log

[T-SQL performance tuning] 03. Execution plan

[T-SQL performance tuning] 04. Deadlock analysis

Continuous update... welcome to follow me!

back to the top

Overview:

This article is mainly a detailed explanation of transactions and concurrency in SQL.

back to the top

1. Affairs

1. What is a transaction

A sequence of operations performed for a single unit of work. Such as query, modify data, modify data definition.

2. Grammar

(1) Display the start and commit of the defined transaction

1

2

3

4

BEGIN TRAN

INSERT INTO b(t1) VALUES(1)

INSERT INTO b(t1) VALUES(2)

COMMIT TRAN

(2) Implicit definition

If you do not explicitly define the boundaries of the transaction, SQL Server will by default treat each individual statement as a transaction, that is, the transaction will be automatically committed after each statement is executed.

3. The four attributes of transactions ACID

(1) Atomicity

1. A transaction must be an atomic unit of work. The modifications made in the transaction are either all executed or not executed at all;

2. Before the transaction is completed (before the commit command is recorded in the transaction log), if the system fails or restarts, SQL Server will undo all modifications made in the transaction;

3. When a transaction encounters an error in processing, SQL Server usually rolls back the transaction automatically;

4. A few less serious errors will not cause automatic rollback of the transaction, such as primary key conflict, lock timeout, etc.;

5. Error handling can be used to catch the error mentioned in point 4 and take some action, such as logging the error in the log, and then rolling back the transaction;

6. SELECT @@TRANCOUNT can be used anywhere in the code to determine whether the place where SELECT @@TRANCOUNT is currently used is in an open transaction. If it is not within the scope of any open transaction, the function returns 0; if it is in an open transaction If the transaction returns within the range, a value greater than 0 is returned. Open a transaction, @@TRANCOUNT=@@TRANCOUNT+1; submit a transaction, @@TRANCOUNT-1.

 

(2) Consiitency

1. Simultaneous transactions do not conflict when modifying and querying data;

2. Consistency depends on the needs of the application. Consistency levels and how to control consistency will be discussed later.

 

(3) Isolation

1. Used to control data access to ensure that transactions only access data at the desired consistency level;

2. Use locks to isolate the data being modified and queried between transactions.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215688&siteId=291194637