简体   繁体   English

LINQ double Join执行时间太长

[英]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. ftr_dist大约有72000行。 mst2 has 1100000 rows and trf2 has 340 rows. mst2具有1100000行,而trf2具有340行。 But it takes too long to get Data. 但是获取数据花费的时间太长。 How can I make this query faster? 如何使此查询更快? Thanks. 谢谢。

you are essentially performing 4 different queries. 您实际上是在执行4个不同的查询。 Linq does take longer to query than sql strings... try this staying with Linq. 与SQL字符串相比,Linq的查询时间更长。尝试将此与Linq一起使用。 Combine all of the queries into one. 将所有查询合并为一个。

I am a little confused in your lambda queries the (x => x.ID == x.ID). 我对您的lambda查询(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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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