// dialect comparison

Full-text search basics in every SQL dialect

Full-text search indexes words so LIKE '%term%' scans give way to real relevance searches. Every engine has one, and every engine spells it differently.

Filed under インデックス

The verdict

Five engines, five completely different APIs: catalogs and CONTAINS, GIN plus to_tsvector, InnoDB FULLTEXT, an FTS5 virtual table, and Oracle Text with an indextype clause. Porting full-text code means rewriting it, not translating syntax.

Where they differ

This is the least portable feature in this section. Creating the index is half the work; each engine also has its own query syntax, shown in the notes.

How each engine writes it

SQL Server

CREATE FULLTEXT CATALOG ft_catalog AS DEFAULT;
CREATE FULLTEXT INDEX ON articles (title, body) KEY INDEX PK_articles;

Query with CONTAINS(body, 'warrior') or FREETEXT. Requires the Full-Text Search feature and a unique key index on the table.

PostgreSQL

CREATE INDEX idx_articles_body ON articles USING GIN (to_tsvector('english', body));

Query with body @@ to_tsquery('english', 'warrior'). Match the exact to_tsvector call in the query or the index is ignored.

MySQL

CREATE FULLTEXT INDEX idx_articles_body ON articles (body);

InnoDB full-text (MyISAM also had it). Query with MATCH(body) AGAINST('warrior') in natural language or boolean mode.

SQLite

CREATE VIRTUAL TABLE articles_fts USING fts5(body, content='articles', content_rowid='id');

No index on an ordinary table; use a separate FTS5 virtual table. Query with SELECT rowid FROM articles_fts WHERE articles_fts MATCH 'warrior'.

Oracle

CREATE INDEX idx_articles_body ON articles (body) INDEXTYPE IS CTXSYS.CONTEXT;

Oracle Text (CTXSYS). Query with CONTAINS(body, 'warrior') > 0. CONTEXT indexes need syncing (ON COMMIT or manual) to see new rows.

Questions that come up

Can I just use LIKE '%word%'?

It works but scans the whole table and cannot use an index. Fine for small tables; full-text indexes are the real answer once scans hurt.

Which engine handles stemming and ranking?

PostgreSQL stems through the text search configuration ('english'), and SQL Server stems in FREETEXT and CONTAINS(... FORMSOF(INFLECTIONAL, ...)). InnoDB full-text does not stem at all. FTS5 stems only with tokenize='porter', and Oracle Text stems through its lexer preferences.

Does a full-text index update itself?

SQL Server and PostgreSQL track changes automatically, InnoDB does too, Oracle Text CONTEXT indexes need a SYNC policy, and SQLite FTS5 keeps its virtual table in sync only via triggers on the content table.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.