Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL full text search vs "LIKE"

Let's say I have a fairly simple app that lets users store information on DVDs they own (title, actors, year, description, etc.) and I want to allow users to search their collection by any of these fields (e.g. "Keanu Reeves" or "The Matrix" would be valid search queries).

What's the advantage of going with SQL full text search vs simply splitting the query up by spaces and doing a few "LIKE" clauses in the SQL statement? Does it simply perform better or will it actually return results that are more accurate?

like image 982
Kevin Pang Avatar asked Jan 25 '09 22:01

Kevin Pang


People also ask

What is full text search vs LIKE?

Like uses wildcards only, and isn't all that powerful. Full text allows much more complex searching, including And, Or, Not, even similar sounding results (SOUNDEX) and many more items.

Is full text search faster than like?

Compare Full-Text Search queries to the LIKE predicate Furthermore, a LIKE query against a large amount of unstructured text data is much slower than an equivalent full-text query against the same data.

Is full text search good?

First, traditional string searches can be performed on smaller text fields. These methods are not as efficient as modern indexed searches but require fewer resources. Full-text searches provide more rich options for advanced querying but can be more complex to set up.

What is SQL full text search?

Full-text search refers to the functionality in SQL Server that supports full-text queries against character-based data. These types of queries can include words and phrases as well as multiple forms of a word or phrase.


2 Answers

Full text search is likely to be quicker since it will benefit from an index of words that it will use to look up the records, whereas using LIKE is going to need to full table scan.

In some cases LIKE will more accurate since LIKE "%The%" AND LIKE "%Matrix" will pick out "The Matrix" but not "Matrix Reloaded" whereas full text search will ignore "The" and return both. That said both would likely have been a better result.

like image 153
AnthonyWJones Avatar answered Sep 20 '22 04:09

AnthonyWJones


Full-text indexes (which are indexes) are much faster than using LIKE (which essentially examines each row every time). However, if you know the database will be small, there may not be a performance need to use full-text indexes. The only way to determine this is with some intelligent averaging and some testing based on that information.

Accuracy is a different question. Full-text indexing allows you to do several things (weighting, automatically matching eat/eats/eating, etc.) you couldn't possibly implement that in any sort of reasonable time-frame using LIKE. The real question is whether you need those features.

Without reading the full-text documentation's description of these features, you're really not going to know how you should proceed. So, read up!

Also, some basic tests (insert a bunch of rows in a table, maybe with some sort of public dictionary as a source of words) will go a long way to helping you decide.

like image 20
John Fisher Avatar answered Sep 21 '22 04:09

John Fisher