Use uma função como esta:
CREATE function [dbo].[list_to_table] (@list varchar(4000))
returns @tab table (item varchar(100))
begin
if CHARINDEX(',',@list) = 0 or CHARINDEX(',',@list) is null
begin
insert into @tab (item) values (@list);
return;
end
declare @c_pos int;
declare @n_pos int;
declare @l_pos int;
set @c_pos = 0;
set @n_pos = CHARINDEX(',',@list,@c_pos);
while @n_pos > 0
begin
insert into @tab (item) values (SUBSTRING(@list,@c_pos+1,@n_pos - @c_pos-1));
set @c_pos = @n_pos;
set @l_pos = @n_pos;
set @n_pos = CHARINDEX(',',@list,@c_pos+1);
end;
insert into @tab (item) values (SUBSTRING(@list,@l_pos+1,4000));
return;
end;
Em vez de usar like, você faz uma junção interna com a tabela retornada pela função:
select * from table_1 where id in ('a','b','c')
torna-se
select * from table_1 a inner join [dbo].[list_to_table] ('a,b,c') b on (a.id = b.item)
Em uma tabela de registro 1M não indexada, a segunda versão demorou cerca de metade do tempo ...
Felicidades