日期:2014-05-17  浏览次数:20537 次

怎样提高这语句的效率
本帖最后由 zzxap 于 2012-11-20 17:12:56 编辑
select * from Invoicing_Invoicing where id not in (712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,774,1052,1053,1054,1055,1056,1057,1059,1061,1062,1065,1067,1068,1069,1070,1071,1087,1088,1089,1091,1092,1093,1095,1097,1098,1099,1100,1638,1640,1641,1642,1878,1879,1880,1881,1882,1883,1884,1885,1886) and userid=132

id是作为一串字符传进来的。貌似用不了leftjoin


------解决方案--------------------

select * 
from Invoicing_Invoicing 
where charindex(','+cast(id as varchar(10))+',',','+'712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,774,1052,1053,1054,1055,1056,1057,1059,1061,1062,1065,1067,1068,1069,1070,1071,1087,1088,1089,1091,1092,1093,1095,1097,1098,1099,1100,1638,1640,1641,1642,1878,1879,1880,1881,1882,1883,1884,1885,1886'+',')>0
and userid=132

------解决方案--------------------
引用:
SQL code?12345select * from Invoicing_Invoicing where charindex(','+cast(id as varchar(10))+',',','+'712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,774,1052,1053,1054……

楼上没考虑的头和尾的问题。。
我发个我自己写的Func吧。

ALTER function [dbo].[Func_Contains]        
(        
@Source varchar(200),        
@Temp varchar(200)        
)        
returns bit        
begin        
declare @i int      
set @i=0      
set @i=charindex(',',@Source)         
if(@i<1)        
begin        
   if (@Temp=@Source)      
   begin      
   return 1      
   end      
end      
else      
begin        
  if(@Temp =substring(@Source,0,charindex(',',@Source)))  
  return 1  
      if(charindex(','+@Temp+',',@Source)>0)         
      return 1              
       while (charindex(','+@Temp,@Source)>0) 
begin
 if(@Temp=substring(@Source,0,charindex(',',@Source)))
 return 1
 set @Source=substring(@Source,charindex(',',@Source)+1,len(@Source))
end
if(@Temp=@Source)
 return 1
end    
return 0        
end 


------解决方案--------------------
引用:
引用:引用:
where中先写userid=132,然后再写not in,至于索引,应该不用说了吧。
高手,为什么这样写,可以解释下吗?首先,where的顺序是有严格限定的,从左到右顺序执行,如果userid有索引的话,就会先大面积筛选掉不必要的数据,值剩