Cont()与Where().Count()有时性能差别如此之大!

时间:2021-08-14 03:00:33

想起我之前在此列表中加入了一个字段,用于方便提示管理员公司的产品列表是否有修改之类的状态字段,于是可以断定是加了此字段的原因。 

首先,先看看我之前是如何写这个提示状态字段的,实体中加入ContentStatus,然后直接在Linq语句中Select 实体对象中加入ContentStatus=Product_Maintain.Count(C => C.CompanyID == company.ID && C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4))>0?"产品有更新":""。这时我想应该是加入三元运算,linq在转Sql时,产生过多的,Case,when ,then语句,三元运算增加了判断会影响查询性能,于是我去掉后,再运行查看页面,仍然很慢,感觉不出快了多少。 

这时,我想起了LinqPad,看看到底转换生成了怎样的Sql语句。运用Count(条件)生成Sql代码如下: 

复制代码代码如下:


SELECT COUNT(*) AS [value] 
FROM ( 
SELECT 
(CASE 
WHEN ([t1].[CompanyID] = ([t0].[ID])) AND ([t1].[IsDeleted] = @p0) AND (([t1].[AuditStatus] = @p1) OR ([t1].[AuditStatus] = @p2)) THEN 1 
WHEN NOT (([t1].[CompanyID] = ([t0].[ID])) AND ([t1].[IsDeleted] = @p0) AND (([t1].[AuditStatus] = @p1) OR ([t1].[AuditStatus] = @p2))) THEN 0 
ELSE NULL 
END) AS [value] 
FROM [Company_Product_Maintain] AS [t1] 
) AS [t2] 
WHERE [t2].[value] = 1 



这时我发现一个很简单的Count的Sql 语句,linq转换后变得如此复杂,我直接在sql server中运行此代码,发现查询还是很慢,于是我直接把ContentStatus=Product_Maintain.Where(C => C.CompanyID == company.ID && C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4)).Count()生成Sql语句为: 

代码 

复制代码代码如下:


SELECT COUNT(*) AS [value] 
FROM [GasSNS_Company_Equipment_Maintain] AS [t1] 
WHERE ([t1].[CompanyID] = ([t0].[ID])) AND ([t1].[IsDeleted] = @p0) AND (([t1].[AuditStatus] = @p1) OR ([t1].[AuditStatus] = @p2)) 


发现运行速度那是快了一个数量级啊! 
后台列表查询结果速度大大提升有图为证(声明:以下图都为项目中截图,不是简单的单表查询,还连了用户表,详细表等数量也都挺大的):

 图1为Count结果,用了35秒,哇塞!

 

Cont()与Where().Count()有时性能差别如此之大!

 

图2为Where(条件).Count()结果,同样的数据只用了4秒钟,差了10倍!

 

Cont()与Where().Count()有时性能差别如此之大!

 

然后为了取值方面我还是加入三元运算,ContentStatus=Product_Maintain.Where(C => C.CompanyID == company.ID && C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4)).Count()>0?"产品有更新":""。结果如下:

 

Cont()与Where().Count()有时性能差别如此之大!

 

真的是Count()与 Where()区别,不可能这么大差距吧?于是我单写

 Product_Maintain.Where(C => C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4)).Count()  与

 Product_Maintain.Count(C => C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4))   

发现速度差不多,生成的代码是一样的。

复制代码代码如下:


SELECT COUNT(*) AS [value] 
FROM [GasSNS_Company_Equipment_Maintain] AS [t0] 
WHERE ([t0].[IsDeleted] = @p0) AND (([t0].[AuditStatus] = @p1) OR ([t0].[AuditStatus] = @p2)) 


原来是我如果在Select中取某表的数量并且条件中使用了之前from后的某个变量时,这时用Count(条件)和Where(条件).Count()产生代码才会不同,查询速度才会出现数量级的差别。 
代码 

复制代码代码如下:


//效率低版本: 
from company in Company 
select new 

contacter = v.ContacterID, 
count = Product_Maintain.Count(C => C.CompanyID == company.ID &&C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4)) 


与 

复制代码代码如下:


//效率高版本: 
from company in Company 
select new 

contacter = v.ContacterID, 
count = Product_Maintain.Where(C =>C.CompanyID == company.ID && C.IsDeleted == 0 && (C.AuditStatus == 0 || C.AuditStatus == 4)).Count() 


否则,Count()与Where().Count()生成的SQL语句是相同的,效率也一样。

      总结到此,望各位看官以后要注意!本人入园两年来,第一发在首页,请各位看官不吝赐教!

      

      谢谢各位看官的指点,声明下以上查询图都LinqPad查询结果截图。至于为啥4秒左右为LinqPad查询时间,Linq生成Sql语句在Sql Server中执行不到1秒,以下截图作解释:

   

Cont()与Where().Count()有时性能差别如此之大!