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

View: 44050|Reply: 3

[Source] Entity Framework Query Optimization WITH(NOLOCK)

[Copy link]
Posted on 2021-1-6 14:52:16 | | | |
First of all, this article andNot by adding WITH(NOLOCK) to improve query performanceInstead, the data is read through the isolation mechanism of the transaction.

In SQL Server databases, when the same database table is frequently read and written at the same time, there will be a lock problem, that is, you cannot read until the previous insert, update, and delete transaction is completed, and you must wait until the operation is completed before you can perform the select operation, in order to prevent concurrent operations and read dirty data.

SELECT statement with WITH (NOLOCK) to solve the blocking deadlock.

Note:Using WITH(NOLOCK) will result in dirty data reading

ef displays the generated SQL statements
https://www.itsvse.com/thread-3813-1-1.html
The test code is as follows:

Use Func delegate to package the code as follows:

The resulting SQL statement is as follows:

2021-01-06 14:32:09.9505 [ thread] DEBUG DEBUG LogCenter - sql statement: SELECT TOP (1)
    [Extent1]. [ID] AS [ID],
    [Extent1]. [Name] AS [Name]
    FROM [dbo]. [Itsvse] AS [Extent1]
    WHERE ([Extent1].[ Name] = @p__linq__0) OR (([Extent1].[ Name] IS NULL) AND (@p__linq__0 IS NULL))
2021-01-06 14:32:09.9515 [ thread] DEBUG DEBUG LogCenter - sql statement:

2021-01-06 14:32:09.9525 [ thread] DEBUG DEBUG LogCenter - sql statement: -- p__linq__0: 'itsvse.com' (type = string, size = 4000)

2021-01-06 14:32:09.9525 [ thread] DEBUG DEBUG LogCenter - sql statement: -- Executing at 2021-01-06 14:32:09 +08:00

2021-01-06 14:32:09.9545 [ thread] DEBUG DEBUG LogCenter - sql statement: -- Completed in 1 ms with result: SqlDataReader

2021-01-06 14:32:09.9555 [ thread] DEBUG DEBUG LogCenter - sql statement:

2021-01-06 14:32:09.9555 [ thread] DEBUG DEBUG LogCenter - sql statement: Closed connection at 2021-01-06 14:32:09 +08:00
select query does not include the nolock keyword.

(End)





Previous:.NET/C# implements simple logging based on BlockingCollection
Next:.NET (CLS) public language specification
 Landlord| Posted on 2021-1-6 14:53:17 |
Use: Add the following statement to the Application_Start() of the Global.cs, and the generated SQL will automatically add with(nolock)
//ef命令拦截器
DbInterception.Add(new WithNoLockInterceptor());
If the SQL statement needs to be locked, add the following extension


1. Fixed the error problem of regular expressions provided on the Internet under special circumstances

 Landlord| Posted on 2021-1-10 10:27:32 |
Testing SQL scripts:

Test results:

No transaction is added by default, and the default isolation level is correct:



Duration: 17561 milliseconds

IsolationLevel.ReadUncommitted isolation level, as shown in the following image:



Time: 519 ms

Source:



 Landlord| Posted on 2023-5-2 11:45:35 |
ADO.NET settings allow dirty readingReadUncommitted, the code is as follows:


There is another way to write it, the dapper code is as follows:

There are no problems with both writing tests,It is recommended to use the first one directly!!!


Other ways to write:



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