Using FULLTEXT Searches
Problem
You want to search through a lot of text.
Solution
Use a FULLTEXT index.
Discussion
You can use pattern matches to look through any number of rows, but as the amount of text goes up, the match operation can become quite slow. It’s also common to look for the same text in several string columns, which with pattern matching tends to result in unwieldy queries:
SELECT * fromtbl_nameWHEREcol1LIKE 'pat' ORcol2LIKE 'pat' ORcol3LIKE 'pat' ...
A useful alternative (available as of MySQL 3.23.23) is to use
FULLTEXT searching, which is designed for looking
through large amounts of text, and can search multiple columns
simultaneously. To use this capability, add a
FULLTEXT index to your table, then use the
MATCH operator to look for strings in the indexed
column or columns. FULLTEXT indexing can be used
with MyISAM tables, for columns of type CHAR,
VARCHAR, or TEXT.
FULLTEXT searching is best illustrated with a
reasonably good-sized body of text. If you don’t
have a sample dataset, several repositories of freely available
electronic text are available on the Internet. For the examples here,
the one I’ve chosen is the complete text of the King
James Version of the Bible (KJV), which is relatively large and has
the advantage of being nicely structured by book, chapter, and verse.
Because of its size, this dataset is not included with the
recipes distribution, but is available separately
as the mcb-kjv distribution at the MySQL Cookbook
web site.[25] (See
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access