Search code examples
sql-serverdatabaseperformancet-sqldatabase-performance

Sql Server Performance: table variable inner join vs multiple conditions in where clause


What is faster in MS Sql Server, a where clause with multiple conditions or a inner join after creating a table variable? For example:

select A.* from A where A.fk='one  ' or A.fk='two  ' or A.fk='three' ...ect.

vs

declare @temp (key as char(matchingWidth)) table;
insert into @temp values ('one  ');
insert into @temp values ('two  ');
insert into @temp values ('three');
select A.* from A inner join @temp t on A.fk=t.key;

I know normally the difference would be negligible; however, sadly the database I am querying use the char type for primary keys...

If it helps, in my particular case, table A has a few million records, and there would usually be about a hundred ids I'd be querying for. The column is indexed, but not a clustered index.

EDIT: I am also open to the same thing with a temp table... although I was under the impression that both a temp table and table variable where virtually identical in terms of performance.

Thanks!


Solution

  • In most cases the first approach will win as table variable does not use statistics. You'll notice big performance decrease with big amount of data. When you have just few values then there is not supposed to be any noticeable difference.