Full-text indexes in MySQL - Time & Space Complexity
When searching text in a database, speed matters a lot. Full-text indexes help find words quickly in large text columns.
We want to understand how the search time changes as the text data grows.
Analyze the time complexity of the following full-text search query.
SELECT * FROM articles
WHERE MATCH(content) AGAINST('database');
This query searches the word 'database' in the content column using a full-text index.
Look at what repeats when the query runs.
- Primary operation: Searching the full-text index for matching words.
- How many times: The search scans index entries related to the search word, not the whole table rows.
As the number of rows and text size grows, the full-text index helps keep search fast.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few index lookups |
| 100 | More index lookups but still quick |
| 1000 | More index entries checked but not all rows scanned |
Pattern observation: The search time grows slowly because it uses the index, not scanning all text.
Time Complexity: O(k)
This means the search time grows slowly as the data grows, thanks to the index structure.
[X] Wrong: "Full-text search scans every row every time, so it's slow for big data."
[OK] Correct: Full-text indexes let the database jump directly to matching words, avoiding scanning all rows.
Understanding how full-text indexes speed up searches shows you know how databases handle big text data efficiently. This skill helps you explain and design fast search features.
"What if we searched multiple words instead of one? How would the time complexity change?"
Practice
FULLTEXT index in MySQL?Solution
Step 1: Understand what FULLTEXT indexes do
FULLTEXT indexes are designed to make searching for words inside large text columns faster and more efficient.Step 2: Compare with other index types
Unlike unique or sorting indexes, FULLTEXT indexes focus on text search, not uniqueness or ordering.Final Answer:
To speed up searching for words inside text columns -> Option AQuick Check:
FULLTEXT index = speeds up text search [OK]
- Confusing FULLTEXT with UNIQUE index
- Thinking FULLTEXT sorts data
- Assuming FULLTEXT compresses data
description in a table products?Solution
Step 1: Recall correct FULLTEXT index syntax
The correct syntax is: CREATE FULLTEXT INDEX index_name ON table_name(column_name);Step 2: Match syntax with options
CREATE FULLTEXT INDEX idx_desc ON products(description); matches the correct syntax exactly.Final Answer:
CREATE FULLTEXT INDEX idx_desc ON products(description); -> Option CQuick Check:
CREATE FULLTEXT INDEX ... ON ... [OK]
- Placing FULLTEXT keyword after INDEX
- Using wrong order of keywords
- Omitting INDEX keyword
articles with a FULLTEXT index on the content column, what will the following query return?SELECT id FROM articles WHERE MATCH(content) AGAINST('database');Solution
Step 1: Understand MATCH() AGAINST() usage
This function searches the FULLTEXT indexed column for the given word or phrase.Step 2: Interpret the query result
The query returns IDs of rows where 'database' appears anywhere in the content column.Final Answer:
All article IDs where the word 'database' appears in content -> Option DQuick Check:
MATCH() AGAINST() = full-text search result [OK]
- Thinking it matches exact whole content
- Assuming it sorts results
- Believing MATCH() needs multiple columns
SELECT * FROM posts WHERE MATCH(body) AGAINST('the');The
body column has a FULLTEXT index. What is the likely problem?Solution
Step 1: Recall FULLTEXT stopword behavior
MySQL ignores very common words (stopwords) by default in FULLTEXT searches.Step 2: Understand why no results appear
If 'the' is in the stopword list, the search ignores it, returning no matches.Final Answer:
The word 'the' is too common and ignored by default stopword list -> Option BQuick Check:
Stopwords cause no results for common words [OK]
- Thinking MATCH() needs multiple columns
- Assuming IN BOOLEAN MODE is always required
- Believing FULLTEXT index must cover multiple columns
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?Solution
Step 1: Understand boolean mode operators
In boolean mode,|means OR,+means AND, and quotes mean phrase search.Step 2: Identify correct OR query
To find rows containing either 'fast' OR 'reliable', use 'fast | reliable'.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.Final Answer:
SELECT * FROM reviews WHERE MATCH(comment) AGAINST('fast | reliable' IN BOOLEAN MODE); -> Option AQuick Check:
Boolean OR uses | operator [OK]
- Using space for OR instead of |
- Using + which means AND
- Using quotes for phrase instead of OR
Ready to prove it?
Pass the test and this pattern moves unknown
