Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Normal Index vs. Fulltext Index

what exactly is the difference (and advantages/disadvantages) between a fulltext index and a regular index on a varchar column? When would I use which index?

I have a multitude of varchar columns (addresses - city name, street name etc.) which I need to be searchable in the most performant way and I'm trying to figure out which index type to use and why.

Thank you!

like image 585
Alex Avatar asked Jul 23 '09 17:07

Alex


3 Answers

It depends on the kind of search you want to do. For example, you can't use a normal index with this query:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE '%' + @SearchText + '%'

It's not sargable. This is sargable, but the selectivity might not be very good:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE @SearchText + '%'

You use a full-text index completely differently:

SELECT * FROM [MyTable] WHERE CONTAINS([MyColumn], @SearchText)
like image 159
Joel Coehoorn Avatar answered Sep 21 '22 16:09

Joel Coehoorn


Usually, when searching with a normal index, you can search only in a single field, e.g. "find all cities that begin with A" or something like that.

Fulltext index allows you to search across multiple columns, e.g. search at once in street, city, province, etc. That might be an advantage if you want to do something like a Google-style search - just punch in a search term and find all rows that have that search term anywhere in any of the varchar columns.

Additionally, with a regular search, you are fairly limited in what you can do - you can search for an exact match or just LIKE - that's about it.

With fulltext index, you can search for word forms (ran, run, etc.) and also for similar words by specifying your own thesaurus. You can search based on several languages if that's an issue. You can search for entries that have two or more terms that are "NEAR" one another.

Marc

like image 21
marc_s Avatar answered Sep 24 '22 16:09

marc_s


From the MSDN:

In contrast to full-text search, the LIKE Transact-SQL predicate works on character patterns only. Also, you cannot use the LIKE predicate to query formatted binary data. 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.

A LIKE query against millions of rows of text data can take minutes to return; whereas a full-text query can take only seconds or less against the same data, depending on the number of rows that are returned.

like image 45
user142360 Avatar answered Sep 23 '22 16:09

user142360