首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQL Server 啟用全文檢索功能完整教學

     編輯:關於SqlServer

SQL Server 的全文檢索功能能夠對大量文字資料進行高效的語意搜尋,超越傳統 LIKE 查詢的效能限制。本文透過 pubs 範例資料庫,完整示範從啟用全文索引到實際查詢的完整流程。

環境準備與啟用流程

1. 啟用資料庫層級的全文索引支援

use pubs
go
execute sp_fulltext_database 'enable'
go

2. 建立全文目錄

execute sp_fulltext_catalog 'ft_titles', 'create'
go

全文目錄是儲存全文索引的邏輯容器,一個資料庫可建立多個目錄以利管理。

3. 為資料表建立全文索引

execute sp_fulltext_table 'titles', 'create', 'ft_titles', 'UPKCL_titleidind'
go

參數說明:

  • titles:目標資料表
  • ft_titles:所屬全文目錄
  • UPKCL_titleidind:資料表的唯一索引(主鍵索引),可透過 sp_help titles 查詢取得

4. 指定要建立索引的欄位

exec sp_fulltext_column 'titles', 'title', 'add'
go
exec sp_fulltext_column 'titles', 'notes', 'add'
go

5. 啟用全文索引並填充目錄

exec sp_fulltext_table 'titles', 'activate'
go
exec sp_fulltext_catalog 'ft_titles', 'start_full'
go

全文搜尋查詢實戰

CONTAINS 謂詞:精確詞彙匹配

-- 片語搜尋
select title, notes from titles
where contains(title, '"computer Cooking"')
go

-- 單一關鍵字
select title, notes from titles
where contains(title, 'computer')
go

FREETEXT 謂詞:語意模糊搜尋

-- 自然語言查詢
select title, notes from titles
where freetext(title, 'computer Cooking')
go

select title, notes from titles
where freetext(title, '"computer Cooking"')
go

-- 搜尋所有全文索引欄位
select title, notes from titles
where freetext(*, 'computer')
go

關鍵差異比較

特性CONTAINSFREETEXT
搜尋模式精確匹配、支援布林邏輯、近接搜尋語意擴展、詞幹分析、同義詞匹配
適用場景精確查找特定詞彙或片語自然語言問答、模糊概念搜尋
效能較快較慢(需語意分析)

維護建議

  • 定期執行 start_incremental 或 start_full 更新索引
  • 監控全文目錄大小,避免磁碟空間不足
  • 考慮設定變更追蹤為自動,減少手動維護負擔
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved