This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12282|Reply: 0

[Source] SQL Server sets the transaction isolation level

[Copy link]
Posted on 2/5/2021 11:53:34 AM | | | |
Entity Framework Query Optimization WITH(NOLOCK)
https://www.itsvse.com/thread-9513-1-1.html

Database locks NOLOCK, HOLDLOCK, UPDLOCK, TABLOCK, TABLOCKX
https://www.itsvse.com/thread-9519-1-1.html
The level of isolation of transactions in SQL Server and their relationship to dirty reads, non-repeatable reads, phantom reads, etc. (code arguments and time sequences)

Understanding these problems that may arise in the case of concurrent access to the database, we can continue to understand the concept of database isolation level, in layman's terms: how do you want to isolate concurrent transactions, and to what extent? For example, if dirty reads can be tolerated, or if you don't want concurrent transactions to have dirty reads, then these can be set to the isolation level to make the isolation between concurrent transactions loose or severe.

The higher the level of isolation, the less chance of reading dirty data or incomplete data, but the more severe the performance degradation is in high-concurrency systems. The lower the isolation level, the greater the performance improvement in the concurrent system, but the data itself may be incomplete.

In SQL Server 2012, you can set the isolation level of a transaction (from low to high) using this syntax:

SET TRANSACTION ISOLATION LEVEL
    { READ UNCOMMITTED
    | READ COMMITTED
    | REPEATABLE READ
    | SNAPSHOT
    | SERIALIZABLE
    }
[ ; ]
First, create a new test script, create a database, and insert test data, as follows:



Create a new window A, open a transaction, perform the update operation, and wait 10 seconds before committing, the code is as follows:

Create a new window B, set the transaction READ UNCOMMITTED (uncommitted read, the lowest level, the easy problem is dirty reading, because it can read the data modified by other transactions but not committed.) It does the same thing as setting (NOLOCK) on the SELECT statement object table in a transaction. Query the data, the code is as follows:

Create a new window C, directly query the data, as follows:

In turn,Execute windows A, B, and C, and find that when updating data, window B can immediately return data (It is possible that the reading is dirty data), C window needs to wait for A window to finish executingwill return the data, as shown in the figure below:


(Window B)


(Window C)

(End)





Previous:Redis Persistence Difference Between RDB and AOF
Next:MySQL exports the query results to a file
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com