views:

121

answers:

4

I am designing a search form, and I am wondering whether should I give the possibility to search by using LIKE %search_string% for a table that is going to have up to 4 million rows

+5  A: 

In general, I would say no. This is a good candidate for full-text indexing. The leading % in your search string is going to eliminate the possibility of using any indexes.

There may be cases where the wait is acceptable and/or you do not want the additional administrative overhead of maintaining full-text indexes, in which case you might opt for LIKE.

RedFilter
I could actually do it without the leading %
Omu
@Omu: If you can omit the leading `%` and create an index on the column(s) in question, then you may find performance is quite acceptable. You will need to test with your particular dataset and hardware to know for sure.
RedFilter
+2  A: 

The search with LIKE %search_string% is very slow even on indexed columns. Worstcase the search does a full table scan.

If a search LIKE search_string% is enough I'd just provide this possibility.

Yves M.
+2  A: 

It depends - without knowing how responsive the search has to be, it could either be fine or completely no go. You'll only really know if you profile your search with likely data patterns and search criteria.

And as RedFilter points out, you might want to consider Full Text Search, if plain search isn't performing well

Damien_The_Unbeliever
+3  A: 

No, you should really only use LIKE '%...%' when your tables are relatively small or you don't care about the performance of your own or other peoples' queries on your database.

There are other ways to achieve this capability which scale much better, full text indexing or, if that's unavailable or not flexible enough, using insert/update triggers to extract non-noise words for querying later.

I mention that last possibility since you may not want a full text index. In other words, do you really care about words like "is", "or" and "but" (these are the noise-words I was alluding to before).

You can separate the field into words and place the relevant ones in another table and use blindingly fast queries on that table to find the actual rows.

paxdiablo
Your unbalanced parenthesis bother me :P
tenfour
@tenfour, that sounds dangerously like "I find your lack of faith disturbing" and I'm having some trouble breathing :-) Not to worry, fixed in the update.
paxdiablo
Full Text indexing isn't an area I know that much about but I think on SQL Server 2008 you can quite easily use a custom (potentially empty) noise word list rather than having to use the system one.
Martin Smith
and if my column is nvarchar(20) and will contain just one word, than it's ok ?
Omu
@Omu - Is that going to be the typical case? That you are searching for substrings in a single word? If so I don't actually know whether full text indexing would give you any benefit. I'm not sure if it indexes partial words. If this is the case you should add this info to your question along with info about the version of SQL Server that you are using.
Martin Smith
@Martin Smith I meant should I use Like on nvarchar(20) (one word) for a 4 million rows table ?
Omu
@pakdiable i was just joking anyway :) the plague of developers...
tenfour