Post

🔍 Supercharge Text Search with Full-Text Index in SQL

Searching through large volumes of text using LIKE ‘%keyword%’ might get the job done on small tables — but it becomes a performance bottleneck as data grows...

🔍 Supercharge Text Search with Full-Text Index in SQL

Searching through large volumes of text using LIKE ‘%keyword%’ might get the job done on small tables — but it becomes aperformance bottleneckas data grows. That’s whereFull-Text Indexingbecomes a game-changer. It enables fast, flexible, and intelligent searching across large datasets.

🧠 What is a Full-Text Index?

AFull-Text Indexis a special kind of index designed forefficient text search. Instead of scanning entire strings, it breaks text intotokens (words)and builds a structure that allows rapid searching, filtering, and ranking.

Key Features:

  • Token-based search (not string-based)
  • Natural language understanding
  • Relevance scoring
  • Phrase and boolean query support

🚫 Why LIKE ‘%keyword%’ is Bad for Performance

Example:

SELECT * FROM articles WHERE content LIKE '%database tuning%';

Issues:

  • Forcesfull table scan
  • Ignores indexesdue to leading %
  • Very slow for large datasets
  • Limited flexibility

✅ Full-Text Index in Action

Let’s look at how different databases implement Full-Text Search:

📌 Example: Full-Text Search in

MySQL

Step 1: Create Full-Text Index

ALTER TABLE articles ADD FULLTEXT(content);

Step 2: Search Using MATCH…AGAINST

SELECT * FROM articlesWHERE MATCH(content) AGAINST('database tuning' IN NATURAL LANGUAGE MODE);

Boolean Mode Example:

SELECT * FROM articlesWHERE MATCH(content) AGAINST('+database -tuning' IN BOOLEAN MODE);

✅ Returns resultscontaining “database” but not “tuning”

📌 Example: Full-Text Search in

PostgreSQL

Step 1: Add a TSVECTOR Column

ALTER TABLE articles ADD COLUMN content_vector TSVECTOR;

Step 2: Populate and Index

UPDATE articlesSET content_vector = to_tsvector('english', content);
CREATE INDEX content_idxON articles USING GIN(content_vector);
SELECT * FROM articlesWHERE content_vector @@ to_tsquery('database & tuning');

Phrase Search Example

SELECT * FROM articlesWHERE content_vector @@ phraseto_tsquery('database tuning');

🔍 Supports stemming, stop words, and phrase matching!

📈 Performance Comparison

🏗️ Real-World Use Case

Scenario:

You’re building a blog platform like Medium and want users tosearch articles by content.

SELECT * FROM postsWHERE body LIKE '%sql tuning%';
  • Takessecondsfor 1M+ rows
  • Poor user experience

With Full-Text Index (MySQL):

ALTER TABLE posts ADD FULLTEXT(body);
SELECT id, title, MATCH(body) AGAINST('sql tuning' IN NATURAL LANGUAGE MODE) AS relevanceFROM postsORDER BY relevance DESCLIMIT 10;

⚡ Results returned inmillisecondswith relevance scoring.

🔄 Updating Full-Text Indexes

For dynamic content (blogs, comments),keep indexes fresh:

MySQL:

Indexes update automatically with inserts/updates (if FULLTEXT is used).

PostgreSQL:

Manually or via triggers:

CREATE TRIGGER update_vectorBEFORE INSERT OR UPDATE ON articlesFOR EACH ROW EXECUTE FUNCTIONtsvector_update_trigger('content_vector', 'pg_catalog.english', 'content');

📊 Summary Table

🧾 Pro Tips

  • UseGIN indexfor tsvector in PostgreSQL.
  • Combine full-text with filters (WHERE MATCH … AND category = ‘Tech’)
  • For multilingual apps, specify appropriate language for stemming.
  • Keep an eye onindex size and update cost.
  • For exact match on short fields like usernames, emails.
  • When data changes frequently and immediate search indexing is needed.
  • If simple substring search is enough.

✅ Conclusion

If your application deals with a lot of user-generated or unstructured text —Full-Text Indexing is essential.

It takes your queries from sluggish to snappy, supports intelligent ranking, and improves user experience dramatically. Whether you’re building a blog engine, document search, or even an e-commerce search engine, Full-Text Index is your best friend. 🧠⚡

This post is licensed under CC BY 4.0 by the author.