Sql server C Linq.Any()与Linq.Where(...count()>0的性能

Sql server C Linq.Any()与Linq.Where(...count()>0的性能,sql-server,linq,c#-4.0,nhibernate,performance-testing,Sql Server,Linq,C# 4.0,Nhibernate,Performance Testing,当使用LINQ ANY时,我在.NET应用程序中对NHibernate LINQ查询进行负载测试时遇到了应用程序缓慢的问题 列FileContent是VARCHARmax bool hasIllustrations = CensusIllustration.Linq() .Any(c => c.Participant.Census.Id == census.Id && c.FileContent != null

当使用LINQ ANY时,我在.NET应用程序中对NHibernate LINQ查询进行负载测试时遇到了应用程序缓慢的问题

列FileContent是VARCHARmax

        bool hasIllustrations = CensusIllustration.Linq()
                        .Any(c => c.Participant.Census.Id == census.Id && c.FileContent != null);
上述查询从代码开始大约需要1分钟,而SSMS在1秒内执行。我使用生成的SQL,如下所示

DECLARE @p0 AS SQL_VARIANT;
SET @p0 = NULL;

select top 1 censusillu0_.CensusIllustration_Id as CensusIl1_5_, 
censusillu0_.FileName as FileName5_, censusillu0_.ParticipantId as Particip3_5_, 
censusillu0_.FileContent as FileCont4_5_, 
censusillu0_.CensusParticipant_Id as CensusPa5_5_ from CensusIllustration censusillu0_, 
CensusParticipant censuspart1_ where censusillu0_.CensusParticipant_Id=censuspart1_.CensusParticipant_Id and censuspart1_.Census_id=@p0 and (censusillu0_.FileContent is not null)  
如果我按如下方式替换代码,它也会在1秒内从代码开始执行

        bool hasIllustrations2 = CensusIllustration.Linq().Where(c => c.Participant.Census.Id == census.Id && c.FileContent != null).Count() > 0;
为此生成的SQL是

-以下变量的类型和值数据不可用。它们的值已设置为默认值

DECLARE @p0 AS SQL_VARIANT;
SET @p0 = NULL;

select cast(count(*) as INT) as col_0_0_ from CensusIllustration censusillu0_, 
CensusParticipant censuspart1_ where 
censusillu0_.CensusParticipant_Id=censuspart1_.CensusParticipant_Id and 
censuspart1_.Census_id=@p0 and (censusillu0_.FileContent is not null)
我试着花时间研究大数据列上任何一个的慢度,每一篇文章都建议任何一个和哪里。COUNT>0或FirstOrdefault不会有任何区别


有人能帮我理解为什么第一个查询从代码开始大约需要1分钟,第二个查询从代码开始需要1秒钟,这是因为计数总是比带顶部的select更快。

您看过执行计划了吗?如果同一个查询从SSMS开始执行需要1秒钟,但从代码开始运行需要1分钟,一定还有别的事情在进行,因为在查询之后没有什么事情要做。您是否在缓存中的结果过期之前,以冷态运行第一个查询,以热态运行第二个查询?在运行一分钟的查询时,是否还有其他内容访问相同的表?@Peter,由于环境访问的原因,我无法获取执行计划。但是我们需要拉SQL。@dasblinkenlight它在我运行它的任何时候都很慢。第一次或第二次跑步都无所谓。我看到了在后端创建的sql查询的区别。一个使用select top1,另一个使用select count*。但是,由于查询在SSMS di am clue less中几乎在相同的时间运行,LINQ方面有什么不同当您在SSMS中运行相同的查询时,您是否将sp_executesql与普查id的参数一起使用?