当前位置: 代码迷 >> Sql Server >> case 查询慢解决办法
  详细解决方案

case 查询慢解决办法

热度:79   发布时间:2016-04-27 17:21:49.0
case 查询慢
select * from View_Print_Consigness where Print_state=1 and V_Level=(case when substring(d_NO,1,1)=''A'' then ''H'' else ''D'' end)

查询9秒

select * from View_Print_Consigness where Print_state=1
查询3秒

请问为什么加上V_Level=(case when substring(d_NO,1,1)=''A'' then ''H'' else ''D'' end)之后就会慢了这么多呀?

有什么办法替换case的吗?

------解决方案--------------------
说实话,我没看懂你的语句.
------解决方案--------------------
CSDN:多了' '这些空格,看起来不自在..

 substring(d_No,1,1)替换为left(d_No,1)

用以下方法生成临时表测测
select *,case when left(d_No,1)='A' then 'H' else 'D' end as Lev
into #
from View_Print_Consigness where Print_state=1 

select 
*
from 
#
where lev=V_Level


------解决方案--------------------
because SQL Compiler cannot do much when you use case/substring in the where clause. Use temporary table or CTE to strip out the substring(d_No,1,1) and put it in a separate column probably will make things better if your table is very large.
------解决方案--------------------
用left代替
  相关解决方案