34,837
社区成员




Select id From [Table] Where charindex(id,'1,2,3……‘)>0 and [ST] = 0
[/quote]
此方法不行,超占CPU,卡住了。他要in那么多的数据,怎么着效率也上不了去。至少实现起来简单,但是效率还得实测。
[quote=引用 13 楼 DBA_Huangzj 的回复:] 因为in本质就是or,优化器对每个or都要做分析,过多的or会导致优化器无法分析,最后报错
Select id From [Table] Where charindex(id,'1,2,3……‘)>0 and [ST] = 0
[/quote]
正在尝试用这个办法,但是charindex(id,'1,2,3……')>0要改成charindex(',' +id + ',' ,','+'1,2,3……'+',')>0[quote=引用 13 楼 DBA_Huangzj 的回复:] 因为in本质就是or,优化器对每个or都要做分析,过多的or会导致优化器无法分析,最后报错
Select id From [Table] Where charindex(id,'1,2,3……‘)>0 and [ST] = 0
[/quote]这个要看数据量了,当年遇到这个类似情况的时候还不会用charindex,所以没测过是否不抱错。不过这个函数基本上就是表扫描或索引扫描,所以可能性能没那么好。如果换成集合,加上索引,可能能用得上因为in本质就是or,优化器对每个or都要做分析,过多的or会导致优化器无法分析,最后报错
Select id From [Table] Where charindex(id,'1,2,3……‘)>0 and [ST] = 0
[quote=引用 6 楼 DBA_Huangzj 的回复:] 先用函数转换你的一串ID,必要的时候加上索引,然后和你的原数据join--1.函数 if exists(select * from sys.objects where name = 'f_splitSTR' and type = 'tf') drop function dbo.f_splitSTR go create function dbo.f_splitSTR ( @s varchar(8000), --要分拆的字符串 @split varchar(10) --分隔字符 ) returns @re table( --要返回的临时表 col varchar(1000) --临时表中的列 ) as begin declare @len int set @len = LEN(@split) --分隔符不一定就是一个字符,可能是2个字符 while CHARINDEX(@split,@s) >0 begin insert into @re values(left(@s,charindex(@split,@s) - 1)) set @s = STUFF(@s,1,charindex(@split,@s) - 1 + @len ,'') --覆盖:字符串以及分隔符 end insert into @re values(@s) return --返回临时表 end go declare @res varchar(100) set @res='1 2 3 4 5'; set @res = REPLACE(@res,' ',',') select * from dbo.f_splitSTR(@res,',') t /* col 1 2 3 4 5 */