SQL Server全文搜索非常慢

时间:2010-12-28 17:59:35

标签: sql-server full-text-search

我有一个存储过程搜索一个包含大约200000多行且全文为FREETEXT的表。

以下是它的基础知识:

declare @searchKey varchar(150)
if @searchKey Is Null OR LEN(@searchKey)=0
    Set @searchKey='""';
Set @searchKey='car';
declare @perPage int
Set @perPage=40
declare @pageNo int
Set @pageNo=1

declare @startIndex int,@endIndex int;
Set @startIndex=@perPage*@pageNo-@perPage+1;
Set @endIndex=@perPage*@pageNo;

Select totalItems
--i pull other colums as well
from (
    Select Row_Number() over(order by CreateDate DESC) As rowNumber
,COUNT(*) OVER() as totalItems
--other columns are pulled as well
from MyTable P 
    Where 
@searchKey='""'
    OR FreeText((P.Title,P.Description),@searchKey)
) tempData
--where rowNumber>=@startIndex AND rowNumber<=@endIndex
where 
rowNumber>=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @endIndex ELSE rowNumber END
order by rowNumber

问题是它的运行速度比我想要的慢。加载页面大约需要3秒钟。当我使用like运算符时,同一页面的加载时间不到1秒。

1 个答案:

答案 0 :(得分:0)

根据我的经验,全文索引函数在包含“OR”运算符的子句中不能很好地工作。我必须通过调整查询来使用UNION来获得相同的行为。试试这个,看看你是否能获得更好的表现。

declare @searchKey varchar(150)
if @searchKey Is Null OR LEN(@searchKey)=0
    Set @searchKey='""';
Set @searchKey='car';
declare @perPage int
Set @perPage=40
declare @pageNo int
Set @pageNo=1

declare @startIndex int,@endIndex int;
Set @startIndex=@perPage*@pageNo-@perPage+1;
Set @endIndex=@perPage*@pageNo;

Select totalItems
--i pull other colums as well
from (
    Select Row_Number() over(order by CreateDate DESC) As rowNumber
,COUNT(*) OVER() as totalItems
--other columns are pulled as well
from
( 
 select * from
 MyTable A
    Where 
     @searchKey='""'
UNION
select * from MyTable B
    where FreeText((B.Title,B.Description),@searchKey)
) as innerTable

) tempData
--where rowNumber>=@startIndex AND rowNumber<=@endIndex
where 
rowNumber>=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @endIndex ELSE rowNumber END
order by rowNumber