SQL Server性能调优杂记(四)
2008-12-08 10:15:13 来源:WEB开发网这前后2种SQL文,在执行计划上有差异,所以导致一快一慢。后者竟然可以慢到17秒之多(第一产生执行计划的时候),即使马上再执行也有10秒到15秒,改善不大。
看一下SQL文1的执行计划
SQL文2的执行计划
前后最大的差别就是多了中间的Filter.
Filter这一步对于整个性能的影响很明显。而这步处理的工作就是原本我们SQL文意图中要忽略掉的部分。
所以,千万要小心,当用SQLCommand的动态参数为你的应用程序带来共通化方便的同时,也会让你程序带来性能问题的风险。
解决问题的方法,显然不能修改程序代码(换成动态自己产生SQL文),因为这是基础框架代码。
只能把SQL文改写一下
BEGIN
IF NOT(@CWB_NO IS NULL)
SELECT a.AWB_NO, a.BWB_NO,a.CWB_NO,a.ORIGIN,
a.DEST, a.MODIFY_ON, a.CREATED_ON,a.CONSIGNOR_CUSTOMER_CODE,
a.CONSIGNOR_CODE,a.CONSIGNOR_NAME,a.CONSIGNEE_NAME, a.CWB_STATUS,
a.CWB_TYPE,ISNULL(a.PCS, 0) AS PCS,a.BWBLIST,b.PWEIGHT
FROM TB_CWB AS a
LEFT JOIN TB_CWBWEIGHT AS b
ON a.CWB_NO = b.CWB_NO
AND b.AVAILABLE = ''Y''
WHERE a.AVAILABLE = ''Y''
AND a.CWB_NO = @CWB_NO
ELSE
SELECT a.AWB_NO, a.BWB_NO,a.CWB_NO,a.ORIGIN,
a.DEST, a.MODIFY_ON, a.CREATED_ON,a.CONSIGNOR_CUSTOMER_CODE,
a.CONSIGNOR_CODE,a.CONSIGNOR_NAME,a.CONSIGNEE_NAME, a.CWB_STATUS,
a.CWB_TYPE,ISNULL(a.PCS, 0) AS PCS,a.BWBLIST,b.PWEIGHT
FROM TB_CWB AS a
LEFT JOIN TB_CWBWEIGHT AS b
ON a.CWB_NO = b.CWB_NO
AND b.AVAILABLE = ''Y''
WHERE a.AVAILABLE = ''Y''
AND (a.AWB_NO = @AWB_NO OR @AWB_NO IS NULL)
AND (a.BWB_NO = @BWB_NO OR @BWB_NO IS NULL)
AND (a.IE_TYPE = @IE_TYPE OR @IE_TYPE IS NULL)
AND (a.CREATED_ON >= @DateFrom OR @DateFrom IS NULL)
AND (a.CREATED_ON <= @DateTo OR @DateTo IS NULL)
AND (a.PAYMENT = @PAYMENT OR @PAYMENT IS NULL)
AND (a.ORIGIN = @ORIGIN OR @ORIGIN IS NULL)
AND (a.DEST = @DEST OR @DEST IS NULL)
AND (a.CONSIGNOR_CUSTOMER_CODE = @CONSIGNOR_CUSTOMER_CODE OR @CONSIGNOR_CUSTOMER_CODE IS NULL)
AND (a.CONSIGNOR_NAME LIKE ''%'' + @CONSIGNOR_NAME + ''%'' OR @CONSIGNOR_NAME IS NULL)
AND (a.CONSIGNEE_NAME LIKE ''%'' + @CONSIGNEE_NAME + ''%'' OR @CONSIGNEE_NAME IS NULL)
AND (a.CWB_TYPE = @CWB_TYPE OR @CWB_TYPE IS NULL)
END
结果SQL文的执行计划就正确了。
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接