Вопрос

One of my columns can only contain 4 possible values (val1, val2, val3, val4).

I need to add an additional filter in the WHERE clause to exlude one of those 4 values (i.e. val4).

Is it going to be faster to use NOT IN ('val4') or IN ('val1','val2','val3')?

Это было полезно?

Решение

NOT IN ('val4') is basically equivalent to

WHERE
    NOT (column = 'val4') /* or column <> 'val4' */

whereas IN ('val1', 'val2', 'val3') is basically equivalent to

WHERE
(
   column = 'val1'
   OR
   column = 'val2'
   OR 
   column = 'val3'
)

I would hope that the first option is faster, if only negligibly. The SQL engine might optimize IN differently depending on how many values you specify in the IN function and what indexes are available on your table. What behavior are you seeing that you're questioning the method to use?

Другие советы

Check the query plan. If Val4 is non clustered index I bet both plans are an index seek.
I tested in, not in, =, and <> and all were the same query plan.
If they have the same query plan I doubt the actual performance difference would be big enough to see. But I would still go with the single value.

Since there are only a few values an indexed view would probably have the best performance.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top