Full-Text Search Indexes (MATCH ... AGAINST) Standard B+Tree indexes are incapable of efficiently searching for keywords inside unstructured text (e.g., blog posts, product reviews, or article bodies). Using LIKE '%keyword%' forces a full table scan that cripples database performance.
MySQL provides Full-Text Indexes (FTS) powered by an internal Inverted Index , queried using the MATCH() ... AGAINST() syntax.
How Inverted Indexes Work Instead of mapping rows to words, a Full-Text Inverted Index tokenizes text into distinct words (tokens) and maps each word to a list of Document IDs where it appears:
1 Token "database" ──> Appears in Doc #1, Doc #4, Doc #89
2 Token "relational" ──> Appears in Doc #4, Doc #12
Looking up "database" requires only a single index lookup to find all matching rows instantly!
Creating a Full-Text Index 1 CREATE TABLE articles (
2 article_id INT AUTO_INCREMENT PRIMARY KEY ,
3 title VARCHAR ( 200 ) NOT NULL ,
4 body TEXT NOT NULL ,
5 FULLTEXT INDEX ft_title_body ( title , body )
6 ) ENGINE = InnoDB ;
1. Natural Language Mode (Default) Natural Language Mode calculates a relevance score based on word frequency and uniqueness:
1 SELECT
2 article_id ,
3 title ,
4 MATCH ( title , body ) AGAINST ( 'mysql performance tuning' ) AS relevance_score
5 FROM articles
6 WHERE MATCH ( title , body ) AGAINST ( 'mysql performance tuning' IN NATURAL LANGUAGE MODE )
7 ORDER BY relevance_score DESC ;
2. Boolean Mode: Power Search with Operators Boolean Mode allows precision keyword filtering using search operators:
Operator Meaning Example +Word MUST be present +mysql +optimization-Word MUST NOT be present +mysql -oracle*` `** Wildcard / prefix operator auto* (matches automates, automatic)""Exact phrase search "query execution plan"( )Grouping expressions +mysql +(replication OR cluster)
1 -- Find articles mentioning 'mysql' and 'optimization', but NOT 'windows'
2 SELECT article_id , title
3 FROM articles
4 WHERE MATCH ( title , body ) AGAINST ( '+mysql +optimization -windows' IN BOOLEAN MODE ) ;
5
6 -- Exact phrase match
7 SELECT article_id , title
8 FROM articles
9 WHERE MATCH ( title , body ) AGAINST ( '"high availability"' IN BOOLEAN MODE ) ;
Minimum Word Length & Stopwords By default:
Words shorter than 3 characters (in InnoDB, controlled by innodb_ft_min_token_size) are ignored. Common English "stopwords" (like "the", "and", "is") are filtered out automatically. 1 -- Inspect minimum token length
2 SHOW VARIABLES LIKE 'innodb_ft_min_token_size' ;
Interactive Quiz • Knowledge Check
Multiple Choice Questions What data structure powers MySQL Full-Text Search?
A Hash Table
B Inverted Index
C B+Tree Clustered Heap
D R-Tree
Which SQL syntax is used to query a Full-Text index?
A WHERE title CONTAINS 'keyword'
B WHERE SEARCH(title, 'keyword')
C WHERE MATCH(columns) AGAINST('query_string')
D WHERE title LIKE_ANY('keyword')
In Boolean Mode, what does the operator + signify (e.g., +mysql)?
A The word is optional
B The word must be present in every matching row
C Sort in ascending order
D Match uppercase only
How do you perform an exact multi-word phrase search in Boolean Mode?
A Enclose the phrase in double quotes ("high availability")
B Separate words with ampersands (high & availability)
C Use brackets ([high availability])
D Prefix with the EQUAL operator
Why is LIKE '%keyword%' unsuitable for searching large textual corpuses?
A It fails to match lowercase letters
B The leading wildcard prevents index usage, forcing an expensive O(N) full table scan
C It only searches numbers
D It locks the entire database server