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

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

WBOY
WBOYOriginal
2016-06-07 18:00:061108browse

今天在修改后台用户公司管理列表时,发现列表加载超慢的bug!好几十秒啊!数据是相对其他的列表是稍微多点,不过也就4000多条,之前是好的,为啥这么慢呢?

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

首先,先看看我之前是如何写这个提示状态字段的,实体中加入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秒,哇塞!

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

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

真的是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秒,以下截图作解释:
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn