TSQL - 使用临时表优化全文搜索查询

时间:2011-01-31 02:07:52

标签: sql-server tsql full-text-search temp-tables

为了简化,我有一个全文搜索查询,可以在单个表上进行公司搜索。搜索完成后,我会从结果中提取额外的统计数据,例如前5名,前5名等......

如何优化此查询,它目前需要大约5秒才能执行< 25,000行,基于执行计划,主要在最后3个选择语句中。

SQL SERVER:2005。我可以升级到2008但我听说SQL 2008存在更多性能问题。

非常感谢帮助。


CREATE PROCEDURE [usp_Company_Search]


 @KeywordNear as varchar(250),
 @LocationNear as varchar(250) = null,
 @PageIndex as int,
 @Pagesize as int

AS

BEGIN

DECLARE @tbl TABLE
(
 row int,
 [Rank] int,
 CompanyID int,
 CompanyDesc  text,
 Title nvarchar(150),
 Company nvarchar(150),
 Category nvarchar(50),
 Source nvarchar(50),
 URI nvarchar(250),
 Location varchar(60),
 DateCreated nvarchar(50)
)

 IF (@LocationNear is not null) BEGIN

  WITH CompanySearch as 
   (
    SELECT ROW_NUMBER() OVER (ORDER BY rs.rank desc) as row,
      rs.Rank as [Rank], 
      J.CompanyID,
      J.CompanyDesc, 
      J.Title, 
      J.Company, 
      J.Category, 
      J.Source, 
      J.URI, 
      J.Location,
      J.DateCreated
    FROM Company J
     INNER JOIN
      CONTAINSTABLE (Company,RawStripped, @KeywordNear) rs
      ON J.Companyid = rs.[KEY] AND
      CONTAINS (Location, @LocationNear) 
   )

    insert into @tbl select * from CompanySearch

       SELECT
     CompanySearch.[Rank], 
     CompanySearch.CompanyID,
     CompanySearch.CompanyDesc, 
     CompanySearch.Title, 
     CompanySearch.Company, 
     CompanySearch.Category, 
     CompanySearch.Source, 
     CompanySearch.URI, 
     CompanySearch.Location,
     CompanySearch.DateCreated
    FROM @tbl as CompanySearch
    WHERE CompanySearch.row between (@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize
  END
 ELSE 
  BEGIN
  WITH CompanySearch as 
    (
     SELECT ROW_NUMBER() OVER (ORDER BY rs.rank desc) as row,
       rs.Rank, 
       J.CompanyID,
       J.CompanyDesc, 
       J.Title, 
       J.Company, 
       J.Category, 
       J.Source, 
       J.URI, 
       J.Location,
       J.DateCreated
     FROM Company J
      INNER JOIN
       CONTAINSTABLE (Company,RawStripped, @KeywordNear) rs
       ON J.Companyid = rs.[KEY] 
    )

    insert into @tbl select * from CompanySearch

        SELECT
      CompanySearch.Rank, 
      CompanySearch.CompanyID,
      CompanySearch.CompanyDesc, 
      CompanySearch.Title, 
      CompanySearch.Company, 
      CompanySearch.Category, 
      CompanySearch.Source, 
      CompanySearch.URI, 
      CompanySearch.Location,
      CompanySearch.DateCreated
     FROM @tbl as CompanySearch
     WHERE CompanySearch.row between (@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

 END

   SELECT Max(row) as RecordCount from @tbl
   select top 5 title, count(title) as cnt from @tbl group by title order by cnt desc
   SELECT top 5 Location, count(location) as cnt from @tbl group by location order by cnt desc
   SELECT top 5 Company, count(company) as cnt from @tbl group by company order by cnt desc


END

1 个答案:

答案 0 :(得分:3)

您的执行计划结果可能是欺骗性的。在SQL 2005中,全文引擎是一种外部服务,因此SQL无法准确地报告该难题中发生的事情。

我不确定您在2008年听说过哪些性能问题,但在2008年全文引擎与数据库完全集成,在您加入数据库表的情况下更加高效反对一组全文结果。如果升级是您的选择,我建议您继续选择。

请参阅:SQL Server 2008 Full-Text Search: Internals and Enhancements