我有要求在我的SQL Server數據庫中搜索幾個不同的表。我需要根據在哪個表中匹配的結果對結果進行排序。優化跨多個表的全文搜索
我採取的方法如下所示。但是,隨着數據量的增長,這看起來效率不高。
任何人都可以建議任何技巧來優化?
-- Full-text query
DECLARE @FtsQuery nvarchar(100)
SET @FtsQuery = 'FORMSOF(INFLECTIONAL, detail)'
-- Maximum characters in description column
DECLARE @MaxDescription int
SET @MaxDescription = 250
SELECT 1 AS RankGroup, FTS.Rank, Id, Title, LEFT([Description], @MaxDescription) AS Description FROM Table1
INNER JOIN CONTAINSTABLE(Table1, *, @FtsQuery) AS FTS ON FTS.[KEY] = Table1.Id
UNION SELECT 2, FTS.Rank, Id, Title, NULL FROM Table2
INNER JOIN CONTAINSTABLE(Table2, *, @FtsQuery) AS FTS ON FTS.[KEY] = Table2.Id
UNION SELECT 3, FTS.Rank, Id, Title, LEFT([Description], @MaxDescription) FROM Table3
INNER JOIN CONTAINSTABLE(Table3, *, @FtsQuery) AS FTS ON FTS.[KEY] = Table3.Id
UNION SELECT 4, FTS.Rank, Id, Title, LEFT([Description], @MaxDescription) FROM Table4
INNER JOIN CONTAINSTABLE(Table4, *, @FtsQuery) AS FTS ON FTS.[KEY] = Table4.Id
UNION SELECT 5, FTS.Rank, Id, Title, LEFT([Description], @MaxDescription) FROM Table5
INNER JOIN CONTAINSTABLE(Table5, *, @FtsQuery) AS FTS ON FTS.[KEY] = Table5.Id
ORDER BY RankGroup, Rank DESC
我想過的一個想法是創建一個索引視圖,然後在視圖上執行搜索。但是由於這種觀點需要這些,所以很難看出這將更加有效。
你可以安全地做一個簡單的優化是用'UNION ALL'替換'UNION'。欲瞭解更多信息:http://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all – niaher