SQL Server Full Text Search Very Slow

谁说胖子不能爱 提交于 2019-12-13 03:39:30

问题


I have a stored procedure that searches a table which has about 200000+ rows with full text FREETEXT.

Here is the basics of it:

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

The problem is its running slower then i would like it. Its taking about 3 seconds to load the page. Same page was loading in less then 1 sec when i was using like operator.


回答1:


In my experience, full text index functions do not work well in a clause that contains an "OR" operator. I have had to get the same behavior by adjusting my query to use a UNION. Try this and see if you can get better performance.

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


来源:https://stackoverflow.com/questions/4548172/sql-server-full-text-search-very-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!