Bird
Raised Fist0
MySQLquery~5 mins

Full-text indexes in MySQL

Choose your learning style10 modes available
Introduction
Full-text indexes help you quickly find words or phrases inside large text columns, like searching for keywords in articles or product descriptions.
You want to search for specific words inside a large text column, like searching blog posts for a topic.
You need to find records that contain certain keywords in product descriptions.
You want to build a simple search feature for a website that looks inside text fields.
You want to speed up text searches compared to using LIKE with wildcards.
You want to rank search results by how relevant they are to the search words.
Syntax
MySQL
CREATE FULLTEXT INDEX index_name ON table_name (column1, column2, ...);
Full-text indexes work best on CHAR, VARCHAR, or TEXT columns.
In MySQL, full-text indexes are supported on MyISAM and InnoDB tables.
Examples
Creates a full-text index named 'ft_index' on the 'title' and 'body' columns of the 'articles' table.
MySQL
CREATE FULLTEXT INDEX ft_index ON articles (title, body);
Adds a full-text index on the 'description' column of the 'products' table.
MySQL
ALTER TABLE products ADD FULLTEXT INDEX ft_desc (description);
Sample Program
This example creates a 'books' table with a full-text index on 'title' and 'description'. It inserts three books and then searches for books related to 'SQL'.
MySQL
CREATE TABLE books (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(100),
  description TEXT,
  FULLTEXT (title, description)
) ENGINE=InnoDB;

INSERT INTO books (title, description) VALUES
('Learn SQL', 'A book about learning SQL databases'),
('Cooking Tips', 'Tips and tricks for cooking delicious meals'),
('SQL Performance', 'How to optimize SQL queries for speed');

SELECT id, title FROM books
WHERE MATCH(title, description) AGAINST('SQL');
OutputSuccess
Important Notes
Full-text search is case-insensitive by default.
Stopwords (common words like 'the', 'and') may be ignored in searches.
Minimum word length for indexing depends on MySQL settings (default is 3 or 4 characters).
Summary
Full-text indexes speed up searching for words inside text columns.
They are useful for building search features in databases.
Use MATCH() AGAINST() to perform full-text searches on indexed columns.

Practice

(1/5)
1. What is the main purpose of a FULLTEXT index in MySQL?
easy
A. To speed up searching for words inside text columns
B. To enforce uniqueness on text columns
C. To sort text columns alphabetically
D. To compress text data for storage

Solution

  1. Step 1: Understand what FULLTEXT indexes do

    FULLTEXT indexes are designed to make searching for words inside large text columns faster and more efficient.
  2. Step 2: Compare with other index types

    Unlike unique or sorting indexes, FULLTEXT indexes focus on text search, not uniqueness or ordering.
  3. Final Answer:

    To speed up searching for words inside text columns -> Option A
  4. Quick Check:

    FULLTEXT index = speeds up text search [OK]
Hint: FULLTEXT indexes are for fast word searches in text columns [OK]
Common Mistakes:
  • Confusing FULLTEXT with UNIQUE index
  • Thinking FULLTEXT sorts data
  • Assuming FULLTEXT compresses data
2. Which of the following is the correct syntax to create a FULLTEXT index on the column description in a table products?
easy
A. CREATE INDEX idx_desc FULLTEXT ON products(description);
B. CREATE INDEX idx_desc ON products(description) FULLTEXT;
C. CREATE FULLTEXT INDEX idx_desc ON products(description);
D. CREATE FULLTEXT ON products(description) INDEX idx_desc;

Solution

  1. Step 1: Recall correct FULLTEXT index syntax

    The correct syntax is: CREATE FULLTEXT INDEX index_name ON table_name(column_name);
  2. Step 2: Match syntax with options

    CREATE FULLTEXT INDEX idx_desc ON products(description); matches the correct syntax exactly.
  3. Final Answer:

    CREATE FULLTEXT INDEX idx_desc ON products(description); -> Option C
  4. Quick Check:

    CREATE FULLTEXT INDEX ... ON ... [OK]
Hint: FULLTEXT index syntax: CREATE FULLTEXT INDEX name ON table(column) [OK]
Common Mistakes:
  • Placing FULLTEXT keyword after INDEX
  • Using wrong order of keywords
  • Omitting INDEX keyword
3. Given a table articles with a FULLTEXT index on the content column, what will the following query return?

SELECT id FROM articles WHERE MATCH(content) AGAINST('database');
medium
A. Syntax error because MATCH() requires multiple columns
B. All article IDs sorted alphabetically by content
C. All article IDs where content exactly equals 'database'
D. All article IDs where the word 'database' appears in content

Solution

  1. Step 1: Understand MATCH() AGAINST() usage

    This function searches the FULLTEXT indexed column for the given word or phrase.
  2. Step 2: Interpret the query result

    The query returns IDs of rows where 'database' appears anywhere in the content column.
  3. Final Answer:

    All article IDs where the word 'database' appears in content -> Option D
  4. Quick Check:

    MATCH() AGAINST() = full-text search result [OK]
Hint: MATCH() AGAINST() finds rows containing the search word [OK]
Common Mistakes:
  • Thinking it matches exact whole content
  • Assuming it sorts results
  • Believing MATCH() needs multiple columns
4. You wrote this query but it returns no results even though matching rows exist:

SELECT * FROM posts WHERE MATCH(body) AGAINST('the');

The body column has a FULLTEXT index. What is the likely problem?
medium
A. AGAINST() requires the IN BOOLEAN MODE modifier
B. The word 'the' is too common and ignored by default stopword list
C. FULLTEXT index must be created on multiple columns
D. MATCH() cannot be used on a single column

Solution

  1. Step 1: Recall FULLTEXT stopword behavior

    MySQL ignores very common words (stopwords) by default in FULLTEXT searches.
  2. Step 2: Understand why no results appear

    If 'the' is in the stopword list, the search ignores it, returning no matches.
  3. Final Answer:

    The word 'the' is too common and ignored by default stopword list -> Option B
  4. Quick Check:

    Stopwords cause no results for common words [OK]
Hint: Common words may be ignored by FULLTEXT stopword list [OK]
Common Mistakes:
  • Thinking MATCH() needs multiple columns
  • Assuming IN BOOLEAN MODE is always required
  • Believing FULLTEXT index must cover multiple columns
5. You want to find rows in the reviews table where the comment column contains either 'fast' or 'reliable'. The comment column has a FULLTEXT index. Which query correctly finds these rows using boolean mode?
hard
A. SELECT * FROM reviews WHERE MATCH(comment) AGAINST('fast | reliable' IN BOOLEAN MODE);
B. SELECT * FROM reviews WHERE MATCH(comment) AGAINST('+fast +reliable' IN BOOLEAN MODE);
C. SELECT * FROM reviews WHERE MATCH(comment) AGAINST('fast reliable' IN BOOLEAN MODE);
D. SELECT * FROM reviews WHERE MATCH(comment) AGAINST('"fast reliable"' IN BOOLEAN MODE);

Solution

  1. Step 1: Understand boolean mode operators

    In boolean mode, | means OR, + means AND, and quotes mean phrase search.
  2. Step 2: Identify correct OR query

    To find rows containing either 'fast' OR 'reliable', use 'fast | reliable'.
  3. Step 3: Check other options

    SELECT * FROM reviews WHERE MATCH(comment) AGAINST('fast reliable' IN BOOLEAN MODE); searches for both words but not as OR; SELECT * FROM reviews WHERE MATCH(comment) AGAINST('+fast +reliable' IN BOOLEAN MODE); requires both words; SELECT * FROM reviews WHERE MATCH(comment) AGAINST('"fast reliable"' IN BOOLEAN MODE); searches exact phrase.
  4. Final Answer:

    SELECT * FROM reviews WHERE MATCH(comment) AGAINST('fast | reliable' IN BOOLEAN MODE); -> Option A
  5. Quick Check:

    Boolean OR uses | operator [OK]
Hint: Use | for OR in boolean full-text searches [OK]
Common Mistakes:
  • Using space for OR instead of |
  • Using + which means AND
  • Using quotes for phrase instead of OR
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard