Home All Groups Group Topic Archive Search About

which would be preferred

Author
23 Sep 2005 12:38 PM
rodchar
hey all,

i have 3 fields in a table that are of interest for searching.
Which would be better to do or faster or efficient?
concat all three fields and perform my LIKE search over that field
or
saying (field1 like 'this') or (field2 like 'this') or (field3 like 'this')

thanks,
rodchar

Author
23 Sep 2005 12:52 PM
Martijn Tonies
>
> i have 3 fields in a table that are of interest for searching.
> Which would be better to do or faster or efficient?
> concat all three fields and perform my LIKE search over that field

Concat? And then?

field1+field2+field3 like 'this%this%this%'?

This won't use an index.

> or
> saying (field1 like 'this') or (field2 like 'this') or (field3 like
'this')

This can use an index, as far as I know.


--
With regards,

Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, Oracle & MS SQL
Server
Upscene Productions
http://www.upscene.com
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com
Author
23 Sep 2005 12:58 PM
SQL
Are you doing a where Field1+ Field2+ Field3 like '%abc %'
or
are you concatinating the fields in the DB

also are they searching like 'abc%' or like '%abc%'
'abc%' is much faster because it can use the index

http://sqlservercode.blogspot.com/



Show quote
"rodchar" wrote:

> hey all,
>
> i have 3 fields in a table that are of interest for searching.
> Which would be better to do or faster or efficient?
> concat all three fields and perform my LIKE search over that field
> or
> saying (field1 like 'this') or (field2 like 'this') or (field3 like 'this')
>
> thanks,
> rodchar
Author
23 Sep 2005 4:58 PM
rodchar
i was thinking about doing in the db so my application can just search 1
field but is that worth the trouble?

Show quote
"SQL" wrote:

> Are you doing a where Field1+ Field2+ Field3 like '%abc %'
> or
> are you concatinating the fields in the DB
>
> also are they searching like 'abc%' or like '%abc%'
> 'abc%' is much faster because it can use the index
>
> http://sqlservercode.blogspot.com/
>
>
>
> "rodchar" wrote:
>
> > hey all,
> >
> > i have 3 fields in a table that are of interest for searching.
> > Which would be better to do or faster or efficient?
> > concat all three fields and perform my LIKE search over that field
> > or
> > saying (field1 like 'this') or (field2 like 'this') or (field3 like 'this')
> >
> > thanks,
> > rodchar
Author
23 Sep 2005 1:36 PM
Anith Sen
One option is to use :

WHERE @param IN ( col1, col2, col3 ) ;

--
Anith
Author
23 Sep 2005 1:47 PM
SQL
That would only work for exact matches not for part of a string

http://sqlservercode.blogspot.com/


Show quote
"Anith Sen" wrote:

> One option is to use :
>
> WHERE @param IN ( col1, col2, col3 ) ;
>
> --
> Anith
>
>
>
Author
23 Sep 2005 3:17 PM
Anith Sen
Ah, I missed the LIKE part. Another way to do this would be to derive the
data from multiple columns using a UNION, for instance, to search three
columns :

SELECT *
  FROM tbl t1
WHERE EXISTS ( SELECT *
                  FROM tbl t2,
                       ( SELECT 1 UNION
                         SELECT 2 UNION
                         SELECT 3 ) D ( n )
                 WHERE t1.key_col = t2.key_col
                   AND CASE n WHEN 1 THEN searched_col1
                              WHEN 2 THEN searched_col2
                              WHEN 3 THEN searched_col3
                       END LIKE '%' + @param + '%' );

--
Anith
Author
23 Sep 2005 4:56 PM
Jeremy Williams
True, but technically the OP did not use a syntax that would look at part of
the column value - they did not include leading or trailing '%' characters.
So based on the OP's post, Anith's suggestion is a valid one.

Show quote
"SQL" <S**@discussions.microsoft.com> wrote in message
news:FCABC311-9877-40C3-8D37-A85683AA5209@microsoft.com...
> That would only work for exact matches not for part of a string
>
> http://sqlservercode.blogspot.com/
>
>
> "Anith Sen" wrote:
>
>> One option is to use :
>>
>> WHERE @param IN ( col1, col2, col3 ) ;
>>
>> --
>> Anith
>>
>>
>>

AddThis Social Bookmark Button