想起我之前在此列表中加入了一個(gè)字段,用于方便提示管理員公司的產(chǎn)品列表是否有修改之類的狀態(tài)字段,于是可以斷定是加了此字段的原因。
首先,先看看我之前是如何寫這個(gè)提示狀態(tài)字段的,實(shí)體中加入ContentStatus,然后直接在Linq語句中Select 實(shí)體對象中加入ContentStatus=Product_Maintain.Count(C => C.CompanyID == company.ID C.IsDeleted == 0 (C.AuditStatus == 0 || C.AuditStatus == 4))>0?"產(chǎn)品有更新":""。這時(shí)我想應(yīng)該是加入三元運(yùn)算,linq在轉(zhuǎn)Sql時(shí),產(chǎn)生過多的,Case,when ,then語句,三元運(yùn)算增加了判斷會(huì)影響查詢性能,于是我去掉后,再運(yùn)行查看頁面,仍然很慢,感覺不出快了多少。
這時(shí),我想起了LinqPad,看看到底轉(zhuǎn)換生成了怎樣的Sql語句。運(yùn)用Count(條件)生成Sql代碼如下:
復(fù)制代碼 代碼如下:
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
這時(shí)我發(fā)現(xiàn)一個(gè)很簡單的Count的Sql 語句,linq轉(zhuǎn)換后變得如此復(fù)雜,我直接在sql server中運(yùn)行此代碼,發(fā)現(xiàn)查詢還是很慢,于是我直接把ContentStatus=Product_Maintain.Where(C => C.CompanyID == company.ID C.IsDeleted == 0 (C.AuditStatus == 0 || C.AuditStatus == 4)).Count()生成Sql語句為:
代碼
復(fù)制代碼 代碼如下:
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))
發(fā)現(xiàn)運(yùn)行速度那是快了一個(gè)數(shù)量級(jí)??!
后臺(tái)列表查詢結(jié)果速度大大提升有圖為證(聲明:以下圖都為項(xiàng)目中截圖,不是簡單的單表查詢,還連了用戶表,詳細(xì)表等數(shù)量也都挺大的):
圖1為Count結(jié)果,用了35秒,哇塞!
圖2為Where(條件).Count()結(jié)果,同樣的數(shù)據(jù)只用了4秒鐘,差了10倍!
然后為了取值方面我還是加入三元運(yùn)算,ContentStatus=Product_Maintain.Where(C => C.CompanyID == company.ID C.IsDeleted == 0 (C.AuditStatus == 0 || C.AuditStatus == 4)).Count()>0?"產(chǎn)品有更新":""。結(jié)果如下:
真的是Count()與 Where()區(qū)別,不可能這么大差距吧?于是我單寫
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))
發(fā)現(xiàn)速度差不多,生成的代碼是一樣的。
復(fù)制代碼 代碼如下:
SELECT COUNT(*) AS [value]
FROM [GasSNS_Company_Equipment_Maintain] AS [t0]
WHERE ([t0].[IsDeleted] = @p0) AND (([t0].[AuditStatus] = @p1) OR ([t0].[AuditStatus] = @p2))
原來是我如果在Select中取某表的數(shù)量并且條件中使用了之前from后的某個(gè)變量時(shí),這時(shí)用Count(條件)和Where(條件).Count()產(chǎn)生代碼才會(huì)不同,查詢速度才會(huì)出現(xiàn)數(shù)量級(jí)的差別。
代碼
復(fù)制代碼 代碼如下:
//效率低版本:
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))
}
與
復(fù)制代碼 代碼如下:
//效率高版本:
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語句是相同的,效率也一樣。
總結(jié)到此,望各位看官以后要注意!本人入園兩年來,第一發(fā)在首頁,請各位看官不吝賜教!
謝謝各位看官的指點(diǎn),聲明下以上查詢圖都LinqPad查詢結(jié)果截圖。至于為啥4秒左右為LinqPad查詢時(shí)間,Linq生成Sql語句在Sql Server中執(zhí)行不到1秒,以下截圖作解釋: