Database / SQLite Interview questions
Why might full-text search (FTS5) be needed instead of a LIKE query?
A LIKE '%word%' query has to scan every row and check the pattern against each one, since a
leading wildcard prevents SQLite from using a normal B-tree index at all — on a large table, this means
a full table scan for every search, which gets slower as the table grows and offers none of the
relevance-ranking or linguistic features (stemming, tokenization) a real search feature typically needs.
-- slow: full table scan, no relevance ranking SELECT * FROM articles WHERE body LIKE '%database%'; -- FTS5: indexed, ranked full-text search CREATE VIRTUAL TABLE articles_fts USING fts5(title, body); SELECT * FROM articles_fts WHERE articles_fts MATCH 'database';
FTS5 is SQLite's built-in full-text search extension: it builds a specialized inverted index over
the text (mapping words to the rows containing them), supporting fast MATCH queries, relevance
ranking, phrase search, and prefix matching — capabilities a plain LIKE scan simply can't
provide efficiently or at all, regardless of table size.
More Related questions...