1

case 1:

SELECT Column1, 
       Cloumn2, 
       Cloumn3 
  FROM TableName
 WHERE Column3 In (SELECT COL3 
                     FROM #TempTable)

Case 2:

DECLARE @valueClo3 varchar(50)
    set @valueClo3   = 'AnyValue'
SELECT Column1, 
       Cloumn2, 
       Cloumn3 
  FROM TableName
 WHERE Column3 In (@valueClo3)

Case 1 takes too much time(approx 3 min), while case 2 takes only 10 sec. #TempTable has only one value 'AnyValue'

3
  • 2
    Presumably different estimates of how many rows will match and different execution plans. Commented Feb 20, 2014 at 12:43
  • is this your complete query or you have posted simplified version of it ? highly unlikely to have such huge time difference for above posted cases . Commented Feb 20, 2014 at 12:48
  • Can you post the execution plan for each query? This should highlight where the difference is coming from. Commented Feb 20, 2014 at 12:52

1 Answer 1

1

Subqueries take a much longer time than any other choice. That being said, IN will be faster, but still functions as an 'or'.

I would recommend trying to join your temporary table. Based on your example, an inner join will will likely accomplish your goal.

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

2 Comments

To test, use SQL Profiler and issue a DBCC DROPCLEANBUFFERS before each one. Note both the CPU time and reads to determine which one will work best for you. Do remember, that your join column on your temp table may need indexed depending on it's size.
I have improved performance of my query by applying index on the table. My index was in order column3, column2 and column1 which improves the performance.

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.