简体   繁体   中英

Creating a compiled query linq and performance issues with regular LINQ

I currently have a query which looks like following:

 using (var ctx = new myEntities())
 {
    ctx.Configuration.LazyLoadingEnabled = false;
    ctx.Configuration.ProxyCreationEnabled = false;
    ctx.Database.CommandTimeout = 200;
    competitors = DBRetry.Do(() => ctx.SearchedUsers.AsNoTracking().Where(x => x.InQueue == true).OrderBy(x => x.LastUpdatedAt).Take(2000).ToList(), TimeSpan.FromSeconds(1));

 }

Since often the table "SearchedUsers" is used by other users/applications, I commonly get slowdowns/deadlocks when doing a query against this database.

I've done a little bit of research to speed up this process by turning off lazy loading, using AsNoTracking() trick with the quer and increasing the command timeout when performing the query against the DB, however the deadlocks occur still sometimes...

So my questions here are:

  1. How would I turn this query into a compiled one
  2. How much of a performance could I expect when performing a compiled query vs the regular one?

Can someone help me out to implement the compiled query solution one ?

Create your query as a view in the database. Set a NOLOCK hint on the query or ISOLATION LEVEL READ UNCOMMITTED. Then set your model object to the view as a table.

Compiling the query would help with performance, but it's not going to help you with the locking of records.

Also, why are you using the CommandTimeout of 200 milliseconds? What happens if you tweak that?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM