简体   繁体   中英

Improve EF Linq query performance

I have this problem where I need to perform a Linq query on a big table (1m records) joining other smaller tables. Running the query is taking too long and sometimes ending in a Execution Timeout Expired . I am not sure how to improve the query to cut execution time.

var result =  from p in db.population
              join e in db.engineers on p.id equals e.personId into ps
              from e in ps.DefaultIfEmpty()
              where (e == null || e.activity == "student")
              select p.name;

var final result = result.Take(100).ToList();

Basically I want to get the first 100 persons from db.population who are not an engineer, engineer-students are excluded.

I am not sure if the query can be written in a better way to improve performance ?

Try This Do it Before join

var result =  from p in db.population
              join e in db.engineers.WHERE( c=> c.activity == "student")
              on p.id equals e.personId into ps
              from e in ps.DefaultIfEmpty()
              where (e == null)
              select p.name;

var final result = result.Take(100).ToList();

or

join e in db.engineers.WHERE( c=> c.activity == "student").Take(100) 

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