I have some list of strings (the number of string varies from 10 to 100) and I need to select values from some large table (100K-5M records) as efficiently as I can. It seems to me that I have 3 options basically - to use 'in' clause, to use table variable or to use temp table. something like this:
select col1, col2, col3, name from my_large_table
where index_field1 = 'xxx'
and index_field2 = 'yyy'
and name in ('name1', 'name2', 'name3', ... 'nameX')
or
declare @tbl table (name nvarchar(50))
insert @tbl(name) values ('name1', 'name2', 'name3', ... 'nameX')
select col1, col2, col3, name
from my_large_table inner join @tbl as tbl on (tbl.name = my_large_table.name)
where index_field1 = 'xxx'
and index_field2 = 'yyy'
The large table has clustered index on (index_field1, index_field2, name, index_field3).
Actually for each set of names I have 4-5 queries from the large table: select, then update and/or insert and/or delete according to some logic - each time constraining the query on this set of names.
The name set and the queries are built dynamically in .net client, so there is no problems of readability, code simplicity or similar. The only goal is to reach the best performance, since this batch will be executed a lot of times. So the question is - should I use 'in' clause, table variable or something else to write my condition?