积极答复者
为何我的查询条件增加一个后性能消耗这么多

问题
-
原先的sql语句:
select ac.*,bp.portname,bs.shipname,cb.ItinDur,cb.SDFare,cb.BDFare,cb.ODFare,cb.IDFare from( select shipcode,linecode,ItinDur,min(saildate)as saildate,min(cruisecode)as cruisecode,min(SDFare + GovtFees)as SDFare, min(BDFare + GovtFees)as BDFare,min(ODFare + GovtFees)as ODFare,min(IDFare + GovtFees)as IDFare from cru_bestfare group by shipcode,linecode,ItinDur ) cb inner join cru_cruises as ac on cb.shipcode = ac.shipcode and cb.linecode = ac.linecode and cb.cruisecode = ac.CruisesCode and cb.saildate = ac.DepartDate inner join base_port as bp on ac.fromport = bp.portcode inner join base_ship bs on ac.shipCode = bs.shipCode and ac.Source = bs.Source and ac.linecode = bs.linecode where ac.active = 1 and ac.modify = 1 and (cb.SDFare > 0 or cb.BDFare > 0 or cb.ODFare > 0 or cb.IDFare > 0)
这个sql语句的执行时间为 <0 秒
增加一个查询条件:
select ac.*,bp.portname,bs.shipname,cb.ItinDur,cb.SDFare,cb.BDFare,cb.ODFare,cb.IDFare from( select shipcode,linecode,ItinDur,min(saildate)as saildate,min(cruisecode)as cruisecode,min(SDFare + GovtFees)as SDFare, min(BDFare + GovtFees)as BDFare,min(ODFare + GovtFees)as ODFare,min(IDFare + GovtFees)as IDFare from cru_bestfare group by shipcode,linecode,ItinDur ) cb inner join cru_cruises as ac on cb.shipcode = ac.shipcode and cb.linecode = ac.linecode and cb.cruisecode = ac.CruisesCode and cb.saildate = ac.DepartDate inner join base_port as bp on ac.fromport = bp.portcode inner join base_ship bs on ac.shipCode = bs.shipCode and ac.Source = bs.Source and ac.linecode = bs.linecode where ac.active = 1 and ac.modify = 1 and ac.DurationDays < 8 and (cb.SDFare > 0 or cb.BDFare > 0 or cb.ODFare > 0 or cb.IDFare > 0)
增加的条件为: and ac.DurationDays < 8 其中 DurationDays 是int类型
然后执行时间变为13秒
上面每个表的数据都不到10000行 为什么会影响这么大呢? 怎么改进???
答案
-
您好
1. 請將 ac.* 的部分,不要用 *,明確指定欄位名稱
2. 針對您的查詢條件建立索引
3. 參考 [SQL SERVER][Performance]查詢效能調校 使用執行計畫分析你的 SQL
歡迎參觀我的Blog.NET菜鳥自救會
- 已建议为答案 Molly Chen_Moderator 2011年8月18日 8:39
- 已标记为答案 Molly Chen_Moderator 2011年8月19日 2:43
全部回复
-
重建index
SQL Server 2005 - 索引的維護作業 - Part 1
http://www.dotblogs.com.tw/limingstudio/archive/2009/11/12/11600.aspx
SQL Server 2005 - 索引的維護作業 - Part2
http://www.dotblogs.com.tw/limingstudio/archive/2009/11/12/11601.aspx
SQL Server 2005 - 索引的維護作業 - Part 3
http://www.dotblogs.com.tw/limingstudio/archive/2009/11/12/11603.aspx
http://www.dotblogs.com.tw/ricochen/archive/2011/07/31/32374.aspx
Shadowと愉快なコード達
Please correct me if my concept is wrong -
試試看透過更新統計資料來提升查詢效能。
http://msdn.microsoft.com/zh-tw/library/ms190397.aspx
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
您好
1. 請將 ac.* 的部分,不要用 *,明確指定欄位名稱
2. 針對您的查詢條件建立索引
3. 參考 [SQL SERVER][Performance]查詢效能調校 使用執行計畫分析你的 SQL
歡迎參觀我的Blog.NET菜鳥自救會
- 已建议为答案 Molly Chen_Moderator 2011年8月18日 8:39
- 已标记为答案 Molly Chen_Moderator 2011年8月19日 2:43