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

View: 27|Reply: 0

[Tips] WITH (NOLOCK) optimization for high-concurrency EF Core queries

[Copy link]
Posted onYesterday at 20:48 | | |
Requirements: Some data allows dirty reading, such as article content, page views, comments, etc., which is not as rigorous as financial data. To improve query performance, ReadUncommitted transactions may be used, but SqlTransaction and TransactionScope have leak-isolation level bugs that have not been fixed yet, so consider using the WITH (NOLOCK) solution.

Review:
Entity Framework Query Optimization WITH(NOLOCK)
https://www.itsvse.com/thread-9513-1-1.html

.NET/C# uses SqlScriptDOM to parse and execute SQL statements
https://www.itsvse.com/thread-11222-1-1.html

SqlTransaction and TransactionScope leak isolation levels (this bug has existed for 8 years and still hasn't been fixed!) I estimate many people might be affected, but may not realize it):The hyperlink login is visible.

This article needs to reference the SqlScriptDOM to parse SQL statements and add WITH(NOLOCK). Refer to the open-source project:The hyperlink login is visible.

Nuget quotes as follows:
Create a new AllowDirtyReadAttribute.cs feature, code as follows:
Create a new WithNoLockInterceptor to override certain methods in DbCommandInterceptor. The code is as follows:
Inject into EF Core with the following configuration:
Use to add it above the controller method[AllowDirtyRead]Can.

Raw SQL statement:
SELECT   [o]. [Id],
         [o]. [CustomerName],
         [o0]. [Id],
         [o0]. [OrderId],
         [o0]. [Product]
FROM     [Orders] AS [o]
             LEFT OUTER JOIN
         [OrderLines] AS [o0]
         ON [o]. [Id] = [o0]. [OrderId]
WHERE    [o]. [Id] = 1
ORDER BY [o]. [Id];

Statement generated after adding NOLOCK:
SELECT   [o]. [Id],
         [o]. [CustomerName],
         [o0]. [Id],
         [o0]. [OrderId],
         [o0]. [Product]
FROM     [Orders] AS [o] WITH (NOLOCK)
         LEFT OUTER JOIN
         [OrderLines] AS [o0] WITH (NOLOCK)
         ON [o]. [Id] = [o0]. [OrderId]
WHERE    [o]. [Id] = 1
ORDER BY [o]. [Id];




Previous:(MSSQL) SQL Server Full-Text Search, Full-Text Index
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