The LIKE '%word%' operator reads every row and only matches letters. Full text search keeps a ready made index of words, so SQL Server can find whole words, all the forms of a word, phrases and rank the best matches, quickly, even in long text columns.
Part of the SQL Server for Beginners series. This article is the written version of lesson 19 of the Bigland Network video course. All examples were run in SQL Server Management Studio on a test instance with the Full-Text Search feature installed. Run every example on a test server, not on production.
Watch lesson 19 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. The test table
- 2. The LIKE way
- 3. Catalog and full text index
- 4. CONTAINS: words, word forms, AND, phrases
- 5. Ranking with CONTAINSTABLE
- 6. A classic error
- 7. Mistakes and best practices
- 8. Practice and clean up
1. The test table
CREATE TABLE dbo.Articles (
Id INT IDENTITY(1,1) CONSTRAINT PK_Articles PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Body VARCHAR(400) NOT NULL);
INSERT INTO dbo.Articles (Title, Body) VALUES
('Backup basics', 'A full backup copies the whole database. Run backups every night and test your restore.'),
('Index tuning', 'A good index helps queries run faster. Too many indexes slow down every insert.'),
('Query performance', 'Slow queries often need a better index, or fewer rows to read. Check the execution plan.'),
('Security for beginners', 'Give users the least permissions they need. Never share the sa login.'),
('Running jobs', 'SQL Agent runs jobs on a schedule, like a nightly backup job.'),
('Restore drill', 'Practice a restore before the real disaster. A backup you never restored is only a hope.');
SELECT Id, Title FROM dbo.Articles;
A full text index needs a unique, single column, not null key: here the named primary key PK_Articles.
2. The LIKE way
SELECT Id, Title FROM dbo.Articles
WHERE Body LIKE '%backup%';
| Id | Title |
|---|---|
| 1 | Backup basics |
| 5 | Running jobs |
| 6 | Restore drill |
It works, but SQL Server must read the text of every row, it matches letters and not words, and it cannot rank the results.
3. Catalog and full text index
CREATE FULLTEXT CATALOG ft_Articles;
CREATE FULLTEXT INDEX ON dbo.Articles (Title, Body)
KEY INDEX PK_Articles ON ft_Articles
WITH CHANGE_TRACKING AUTO;
SELECT c.name AS CatalogName, i.is_enabled
FROM sys.fulltext_indexes i
JOIN sys.fulltext_catalogs c ON c.fulltext_catalog_id = i.fulltext_catalog_id;
Result: one row, ft_Articles, is_enabled = 1. After creation SQL Server fills (populates) the index in the background: on a big table wait for it to finish before you trust the results.
4. CONTAINS: words, word forms, AND, phrases
-- one exact word: articles 1 and 2
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'run');
-- all forms of the word (run, runs, running, ran): articles 1, 2 and 5
SELECT Id, Title FROM dbo.Articles
WHERE CONTAINS(Body, 'FORMSOF(INFLECTIONAL, run)');
-- both words: articles 1 and 6
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'backup AND restore');
-- an exact phrase: article 5
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, '"nightly backup"');
Use OR when either word is enough, AND NOT to exclude a word, and a star for a prefix, for example '"ind*"'.
5. Ranking with CONTAINSTABLE
SELECT a.Id, a.Title, k.[RANK]
FROM CONTAINSTABLE(dbo.Articles, Body, 'index OR plan') AS k
JOIN dbo.Articles a ON a.Id = k.[KEY]
ORDER BY k.[RANK] DESC;
| Id | Title | RANK |
|---|---|---|
| 3 | Query performance | 32 |
| 2 | Index tuning | 24 |
A higher RANK is a better match. The exact numbers can differ between servers, so trust the order, not the values.
6. A classic error
SELECT Id, Title FROM dbo.Articles
WHERE CONTAINS(Body, 'index backup');
Error 7630: syntax error near ‘backup’ in the full-text search condition. Two words need an operator: 'index AND backup' or 'index OR backup'.
7. Mistakes and best practices
- Two words with no operator.
- Searching a column that is not in the full text index.
- Querying right after creating the index, while it is still filling.
- Expecting CONTAINS to find part of a word.
- Using LIKE with a leading percent on a big table.
- Best practice: use full text for words in long text, index only the columns people search, use CHANGE_TRACKING AUTO, use CONTAINSTABLE when the order of the best match matters, and keep LIKE for small, simple searches.
8. Practice and clean up
- Find the articles that mention restore in Title or Body.
- Which articles mention backup but not restore?
- How many articles mention backup in Title or Body?
-- 1 (articles 1 and 6)
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS((Title, Body), 'restore');
-- 2 (article 5)
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'backup AND NOT restore');
-- 3 (3)
SELECT COUNT(*) AS Articles_With_Backup FROM dbo.Articles WHERE CONTAINS((Title, Body), 'backup');
-- clean up: index, then catalog, then table
DROP FULLTEXT INDEX ON dbo.Articles;
DROP FULLTEXT CATALOG ft_Articles;
DROP TABLE dbo.Articles;
Frequently asked questions
Is full text search installed by default?
It is an optional feature of SQL Server. Check with SELECT SERVERPROPERTY('IsFullTextInstalled'): 1 means it is installed.
Why does my new index return no rows?
The index is filled in the background (population). Wait until it finishes, then query again.
Can CONTAINS find part of a word?
Only from the start of a word, with a prefix term such as '"back*"'. For text in the middle of a word, use LIKE.
What to learn next
The next lesson, lesson 20: Query Store and Slow Queries, covers Query Store, the black box recorder of SQL Server, to find slow queries after the fact. Previous lesson: Lesson 18: Partitioning and Large Tables.
The whole series
- Lesson 1: SELECT, WHERE and ORDER BY
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables
- Lesson 19: Full Text Search (this article)
- lesson 20: Query Store and Slow Queries
Sources and further reading
- Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
- Microsoft Learn, full text search, CONTAINS, FORMSOF and CONTAINSTABLE, for the full details of each feature.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Partitioning Tutorial: Partition Function, Scheme, SPLIT and SWITCH
Slice a big table into partitions: create a partition function and scheme, load rows, SPLIT a new range and SWITCH old data out in…
