I have Table1 with 10 VARCHAR(37) columns and an ID. It has 250 lines. I want to get for a specific set of 4 char string the count of the lines in TABLE1 which have at least one of the 10 columns containing this 4 char string.
The result will look like this
str count
............
T100 4
T010 50
. .
. .
. .
............
My question is not about how to do it, but about the performance
I did it like this
> DECLARE @Helper TABLE(Str Varchar(4), Count INT DEFAULT 0 )
> --I will skip here insering those 150 4char strings into @Helper on Str column
>
> UPDATE @Helper SET [Count] = SELECT COUNT(ID) FROM Table1 T WHERE
> CHARINDEX(Str,T.Col1) > 0 OR CHARINDEX(Str,T.Col2) > 0 ...... OR
> CHARINDEX(Str,T.Col10) > 0
>
> --where Col1...Col10 are the VARCHAR(37) columns
This takes ~2 seconds. Isn't it too much? My guess is that SQL Server evaluates all the conditions in the WHERE clause, even if only 1 will suffice ( i have only OR). Am i right ? Or how can I get this done under 2 seconds ? I am using SQL server 2008.
Thank you all!