C# Linq查询上的数据库响应时间异常缓慢

C# Linq查询上的数据库响应时间异常缓慢,c#,sql-server,linq,C#,Sql Server,Linq,我已经写了一个我认为非常可靠的Linq语句,但是执行时会有2到5秒的等待时间。有人想过如何加快速度吗 t.states = (from s in tmdb.tmZipCodes where zips.Contains(s.ZipCode) && s.tmLicensing.Required.Equals(true) group s by new Licensin

我已经写了一个我认为非常可靠的Linq语句,但是执行时会有2到5秒的等待时间。有人想过如何加快速度吗

            t.states = (from s in tmdb.tmZipCodes
                        where zips.Contains(s.ZipCode) && s.tmLicensing.Required.Equals(true)
                        group s by new Licensing { 
                            stateCode = s.tmLicensing.StateCode,
                            stateName = s.tmLicensing.StateName,
                            FIPSCode = s.tmLicensing.FIPSCode,
                            required = (bool)s.tmLicensing.Required,
                            requirements = s.tmLicensing.Requirements,
                            canWorkWhen = s.tmLicensing.CanWorkWhen,
                            appProccesingTime = (int) s.tmLicensing.AppProcessingTime 
                        }
                            into state
                            select state.Key).ToList();

我已经将它改为两阶段查询,通过执行一个不同的查询来进行分组,几乎可以在瞬间运行,但在我看来,让它运行得比单个查询快得多有点违反直觉。

我不确定为什么要花这么长时间,但看看,它将向您显示正在生成的实际查询并帮助优化

另外,可能不是实际的查询花费了很长时间,而是查询生成。我发现最长的部分是linq转换为sql语句的时间


您可以使用已编译的查询来加速sql生成过程。可以在上找到一些信息。我不想推广我的博客,但我认为它很合适。

我不知道为什么要花这么长时间,但这可能有助于查看,它将显示生成的实际查询并帮助优化

另外,可能不是实际的查询花费了很长时间,而是查询生成。我发现最长的部分是linq转换为sql语句的时间


您可以使用已编译的查询来加速sql生成过程。可以在上找到一些信息。我不是想宣传我的博客,但我认为它是合适的。

我希望这与我的博客无关,但是

s.tmLicensing.Required.Equals(true)
(对我来说)看起来非常像:

假设它是布尔属性

既然你知道这是真的,我也不认为把它放在分组中有什么意义


说到这些,约翰·博克在两个方面都是绝对正确的:找出是SQL还是LINQ,然后攻击相关的部分。

我希望这是无关紧要的,但是

s.tmLicensing.Required.Equals(true)
(对我来说)看起来非常像:

假设它是布尔属性

既然你知道这是真的,我也不认为把它放在分组中有什么意义


说到这里,John Boker在两个方面都是绝对正确的:找出是SQL还是LINQ,然后攻击相关位。

您似乎没有使用组,只是在最后选择了键。那么,这和你想要的一样吗

t.states = (from s in tmdb.tmZipCodes
            where zips.Contains(s.ZipCode) && s.tmLicensing.Required.Equals(true)
            select new Licensing { 
                stateCode = s.tmLicensing.StateCode,
                stateName = s.tmLicensing.StateName,
                FIPSCode = s.tmLicensing.FIPSCode,
                required = (bool)s.tmLicensing.Required,
                requirements = s.tmLicensing.Requirements,
                canWorkWhen = s.tmLicensing.CanWorkWhen,
                appProccesingTime = (int) s.tmLicensing.AppProcessingTime 
            }).Distinct().ToList();

还要记住,LINQ在必须执行查询之前不会执行查询。因此,如果在两条语句中构建查询,则在调用ToList之前,它不会针对数据上下文(在本例中为SQL Server)执行该查询。当查询运行时,它会将多个查询合并到一个查询中并执行该查询。

您似乎没有使用组,只是选择了最后的键。那么,这和你想要的一样吗

t.states = (from s in tmdb.tmZipCodes
            where zips.Contains(s.ZipCode) && s.tmLicensing.Required.Equals(true)
            select new Licensing { 
                stateCode = s.tmLicensing.StateCode,
                stateName = s.tmLicensing.StateName,
                FIPSCode = s.tmLicensing.FIPSCode,
                required = (bool)s.tmLicensing.Required,
                requirements = s.tmLicensing.Requirements,
                canWorkWhen = s.tmLicensing.CanWorkWhen,
                appProccesingTime = (int) s.tmLicensing.AppProcessingTime 
            }).Distinct().ToList();

还要记住,LINQ在必须执行查询之前不会执行查询。因此,如果在两条语句中构建查询,则在调用ToList之前,它不会针对数据上下文(在本例中为SQL Server)执行该查询。当查询运行时,它会将多个查询合并到一个查询中并执行该查询。

有时候,当您复制和粘贴代码时,您会忽略一些小细节。我有另一个函数,它填充了我刚刚从中撕下代码的授权对象。有时候,当你复制和粘贴代码时,你会忽略一些小细节。我有另一个函数,它填充了我刚刚从中提取代码的许可对象。