简体   繁体   中英

LINQ double Join takes too long to execute

I have this query :

    var ftr_dist = db.DIST_VIEW.Where(x => x.CITY == "Los Angeles");
    var mst2 = db.TB_SERVICE.Where(x => x.ID == x.ID);
    var trf2 = db.TYPE.Where(x => x.ID == x.ID);

    var Data = (from ftr in ftr_dist
                      join mst in mst2 on ftr.CUSTOMER_ID equals mst.CUSTOMER_ID
                      join trf in trf2 on mst.TYPE_ID equals trf.ID
                      select new TypeObj { City = ftr.CITY, County = ftr.COUNTY, Type = trf.Type }
                      ).OrderBy(i => i.City).ThenBy(i => i.County).ToList();

ftr_dist has about 72000 rows. mst2 has 1100000 rows and trf2 has 340 rows. But it takes too long to get Data. How can I make this query faster? Thanks.

you are essentially performing 4 different queries. Linq does take longer to query than sql strings... try this staying with Linq. Combine all of the queries into one.

I am a little confused in your lambda queries the (x => x.ID == x.ID). You may need to add this to the below

var data = from ftr in db.DIST_VIEW
           join mst in db.TB_SERVICE on ftr.CUSTOMER_ID equals 
           mst.CUSTOMER_ID
           join trf in db.TYPE on trf.TYPE_ID equals ftr.ID
           where ftr.CITY == "Los Angeles"
           select new TypeObj 
           {
            City = ftr.City,
            Country = ftr.County,
            Type - trf.Type
           }.OrderBy(i => i.City).ThenBy(i => i.County).ToList();

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