0

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!

3
  • 1
    I guess it would be good to check both cases and compare execution plans. Commented Mar 17, 2015 at 13:06
  • Good idea, but I don't have permissions needed to analyze execution plans. Additionally, I was hoping I could learn from someone else's hard fought experience as this is the first time I've had to dance around poor performance due to bad DB design. Commented Mar 17, 2015 at 13:08
  • 1
    Unfortunately, someone else's hard fought experience wasn't with your data in your schema on you kit... Commented Mar 17, 2015 at 13:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

What about a temp table?
@RhysJones yes, but in example where is table variable. A lot of people call it as temporary table.
@TheCatWhisperer temp table will win in most cases. However it uses resources to create the table and in your provided example the first case would probably win. Anyway the only true answer for your question is TEST, TEST, TEST ... check in profiler and you'll see the benefits.
@sidux No - a table variable and a temp table are very different things.
@TheCatWhisperer I mentioned CONDITIONAL index (add WHERE conditions to the index). And answer - yes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.