2011-01-31 107 views
0

爲了讓總之我有一個完整的文本搜索查詢確實在單個表公司查冊。一旦搜索完成,我從結果中提取額外的統計信息,如前5名,前5名等...TSQL - 優化全文檢索查詢與臨時表

如何優化此查詢,目前需要約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

「但我聽說有與SQL 2008多個性能問題」 - 哪些問題? – 2011-01-31 02:15:25

回答

3

你執行計劃的結果可能會被欺騙。在SQL 2005中,全文引擎是一種外部服務,因此SQL無法準確報告該難題中發生的情況。

我不知道你在2008年聽到的什麼性能問題,但在2008年的全文引擎變得完全與數據庫集成,使得它更在你們這樣的情況下,有效的,你要加入數據庫表對一組全文結果。如果升級是你的一個選擇,我鼓勵你去追求這個選擇。

參見:SQL Server 2008 Full-Text Search: Internals and Enhancements