SQL - Full-Text Search

Overview

Full-text search (FTS) indexes and queries text quickly. MySQL and SQL Server have built-in FTS; SQLite offers FTS3/4/5 as extensions. This page shows engine-specific patterns (static examples).

At a glance

  • Use FTS for linguistic matching (tokenization, stemming) beyond simple LIKE filters.
  • Tailor language/stoplists; keep FTS indexes updated (batch or real-time) to ensure freshness.
  • Expect different ranking/boolean semantics across engines; test queries on representative data.
-- Create a FULLTEXT index (MyISAM/InnoDB as supported)
CREATE TABLE articles (
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(200),
  body TEXT,
  FULLTEXT(title, body)
);

-- Query
SELECT id, title
FROM articles
WHERE MATCH(title, body) AGAINST ('+database +scaling' IN BOOLEAN MODE);
-- Enable full-text on a table
CREATE TABLE Articles (
  Id INT PRIMARY KEY,
  Title NVARCHAR(200),
  Body NVARCHAR(MAX)
);
CREATE FULLTEXT CATALOG ft AS DEFAULT;
CREATE FULLTEXT INDEX ON Articles(Title, Body) KEY INDEX PK__Articles__Id;

-- Query
SELECT Id, Title
FROM Articles
WHERE CONTAINS((Title, Body), '"database" AND "scaling"');
-- SQLite FTS5 (if compiled)
CREATE VIRTUAL TABLE articles USING fts5(title, body);
INSERT INTO articles(title, body) VALUES ('DB Tuning', 'database performance scaling tips'),
                                         ('Intro', 'hello world');
SELECT rowid, title FROM articles WHERE articles MATCH 'database AND scaling';

Example Input (conceptual)

idtitlebody
1DB Tuningdatabase performance scaling tips
2Introhello world

Expected Output (conceptual)

idtitle
1DB Tuning