当OR时,为什么SQL FullText查询会变慢?

时间:2010-07-01 23:17:35

标签: sql sql-server performance

在SQL Server(2008)中,我在两列上有一个FullText索引,称之为Table1.FirstNamesTable2.LastNames。在分析了一些查询后,我想出了以下结果:

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE CONTAINS(FirstNames, 'Bob') OR CONTAINS(LastNames, 'Bob')

=> 31 197ms

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE (FirstNames LIKE '%Bob%') OR CONTAINS(LastNames, 'Bob')

=> 1941ms

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE CONTAINS(FirstNames, 'Bob') OR LastNames LIKE '%Bob%'

=> 3201ms

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE CONTAINS(FirstNames, 'Bob')

=> 565ms

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE FirstNames LIKE '%Bob%'

=> 670ms

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE CONTAINS(LastNames, 'Bob')

=> 17MS

SELECT *
FROM (Table1 LEFT JOIN Table2 ON Table1.SomeKey=Table2.SomeKey)
WHERE LastNames LIKE '%Bob%'

=> 3ms的

即使重建FullText索引,此行为仍然存在。

对于特定语言的大型数据集,FullText通常比LIKE查询快得多,但为什么当我将两个FullText碰撞时,查询速度减慢一个数量级?

3 个答案:

答案 0 :(得分:5)

更改为使用ContainsTable有帮助吗?

它在这里Adding more OR searches with CONTAINS Brings Query to Crawl

同一个回答者(Joe Stefanelli)通过将FREETEXT个谓词与OR一起更改为FREETEXTTABLE SQL Server full text query across multiple tables - why so slow? <,设法带来了类似的改进/ p>

答案 1 :(得分:0)

答案 2 :(得分:0)

我会看看每个人的执行计划。我猜你会从中学到很多东西。

Here is a decent link将向您展示如何显示执行计划以及解释它的一些提示。