TSQL - Optimizing Full Text Search query with temp table
To make it short I have a full text search query that does a company search on a single table. Once the search is complete I pull extra stats from the result such as Top 5 titles, top 5 locations etc...
How can this query be optimized it currently takes about 5 seconds to execute on < 25,000 rows and based on the execution plan its mostly on the last 3 select statements.
SQL SERVER: 2005. I can upgrade to 2008 but I've heard there are more performance issues with SQL 2008.
Help is greatly appreciated.
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 in开发者_StackOverflowto @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
Your execution plan results may be deceiving. In SQL 2005, the fulltext engine is an external service, so SQL cannot accurately report on what's happening in that piece of the puzzle.
I'm not sure what performance issues you've heard of in 2008, but in 2008 the fulltext engine becomes fully integrated with the database, making it much more efficient in a case like yours where you're joining a database table against a set of fulltext results. If upgrading is an option for you, I'd encourage you to pursue that option.
See: SQL Server 2008 Full-Text Search: Internals and Enhancements
精彩评论