读书人

使用CASE作查询条件后如何速度变得很

发布时间: 2012-02-10 21:27:41 作者: rapoo

使用CASE作查询条件后,怎么速度变得很慢,应怎样解决?
Select * from vwOutHouseDsp
where iDocStatus <=100 and dDocDate> =@tdBDate and dDocDate <=@tdEDate
and (cDocType= '销售发货 ' or cDocType= '销售退货 ')
and WhSysCode=case when @tcWhSysCode= ' ' then WhSysCode else @tcWhSysCode End
and ISysCode=case when @tcISysCode= ' ' then ISysCode else @tcISysCode End
and IBSysCode=case when @tcIBSysCode= ' ' then IBSysCode else @tcIBSysCode

以上这句在WHERE上使用CASE后查速度就变得很慢了,如果把
and WhSysCode=case when @tcWhSysCode= ' ' then WhSysCode else @tcWhSysCode End
and ISysCode=case when @tcISysCode= ' ' then ISysCode else @tcISysCode End
and IBSysCode=case when @tcIBSysCode= ' ' then IBSysCode else @tcIBSysCode
这几句去掉,速度就正常了,怎么会这样呢?
另外,如果不用CASE,这种情况的条件查询怎样解决比较好呢?


[解决办法]
把条件
@tcWhSysCode,@tcISysCode,@tcIBSysCode 默认设成null
create procedure protest
@tcWhSysCode int =null,
@tcISysCode int =null,
@tcIBSysCode int =null
as
Select * from vwOutHouseDsp
where iDocStatus <=100 and dDocDate> =@tdBDate and dDocDate <=@tdEDate
and (cDocType= '销售发货 ' or cDocType= '销售退货 ')
and WhSysCode=isnull(@tcWhSysCode,WhSysCode)
and ISysCode=isnull(@tcISysCode,ISysCode)
and IBSysCode=isnull(@tcIBSysCode,IBSysCode)

读书人网 >SQL Server

热点推荐