简体   繁体   English

linq语句非常慢

[英]linq statement very slow

I have a linq to sql data context set up. 我有一个linq to sql数据上下文设置。 I try to expect an amount of logs identified by a mappingID, so im using that to write a webclient, that shows the status of these downloads. 我试图期望由mappingID标识的日志数量,所以我用它来编写一个Webclient,以显示这些下载的状态。 Right now i have a situation where a linq statement is taking forever, although the amount of rows seems relatively low. 现在我有一种情况,尽管行数似乎相对较低,但linq语句将永远占用时间。

The statement taking forever is: 永远接受的声明是:

 var dlCatToUnion = (from cl in currentLogs
                            where ndcat.All(x=>x!=cl.CategoryCountryCategoryTypeMapping.CategoryID)
                            group cl by cl.CategoryCountryCategoryTypeMapping.Category.CategoryID into t1
                            select new CategoryStruct
                            {
                                CategoryName = t1.Max(x => x.CategoryCountryCategoryTypeMapping.Category.Name),
                                Status = t1.Any(x=>x.Response!=(int)ErrorCodes.staticCodes.success)
                                                ? (int)ErrorCodes.staticCodes.genericFailure : (int)ErrorCodes.staticCodes.success,
                                AverageResponseTime = 0,
                                categoryId = t1.Key
                            }
                            );

Specifically if you look at the second line where it says where ndcat.All(x=>x!=cl.CategoryCountryCategoryTypeMapping.CategoryID) if i take this part out, its instant. 具体来说,如果您看第二行,它说出where ndcat.All(x=>x!=cl.CategoryCountryCategoryTypeMapping.CategoryID)where ndcat.All(x=>x!=cl.CategoryCountryCategoryTypeMapping.CategoryID)如果我将这一部分拿出来,它是即时的。

To take a look at what that line is doing: 看一下该行在做什么:

 var ndcat = (from ndid in notDownloadedIds
                     where ndid.Category.StorefrontID==StorefrontID
                     group ndid by ndid.CategoryID into finalCat
                     select finalCat.Key);

And then notDownloadedIds 然后notDownloadedIds

 notDownloadedIds = cDataContext.CategoryCountryCategoryTypeMappings.Where(mapping =>!
currentLogs.Select(dll => dll.CategoryCountryCategoryTypeMappingID).Any(id => id == mapping.CategoryCountryCategoryTypeMappingID));

To give some estimates of row counts, currentLogs is around 25k rows, CategoryCountryCategoryTYpeMappingID is about 53k rows. 为了估算行数,currentLogs大约为2.5万行,CategoryCountryCategoryTYpeMappingID为大约53k行。 ndcat ends up being 47 rows(also it enumerates just about instantly). ndcat最终为47行(也几乎立即枚举)。

Also to note ive changed the suspect line to a ! 还要注意我把可疑线改为了! ...Any(...) statement and its just as slow. ... Any(...)语句及其缓慢。

Is there anywhere where im being ineffecient? 我在哪里效率低下?

Have you tried changing: 您是否尝试过更改:

where ndcat.All(x => x != cl.CategoryCountryCategoryTypeMapping.CategoryID)

to: 至:

where !ndcat.Any(x => x == cl.CategoryCountryCategoryTypeMapping.CategoryID)

?? ??

我最终进行了一些更改,但总而言之,我在分组之后而不是之前进行了ndcat检查,这使速度大大提高了。

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

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