Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Full Text Search, Temp table, Performance

Author  Topic 

SQLOverload
Starting Member

2 Posts

Posted - 2011-01-30 : 21:00:15
Hello.

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.

Thank you




quote:
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




Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-31 : 06:34:16
Change the table variable to a temp (#) table and see if that works better. Table variables can be tricky sometimes...

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2011-01-31 : 08:20:20
Also try adding a full text index on the columns used for full text search.

PBUH

Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-31 : 08:30:25
quote:
Also try adding a full text index on the columns used for full text search.
You are not able to use functions like CONTAINSTABLE unless you have a full-text index present.

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page

SQLOverload
Starting Member

2 Posts

Posted - 2011-01-31 : 16:07:51
Thanks for the tips.

I'll give it a shot.

As a followup is there a way to optimize the selects/order clause on the stats at the end of the query ?
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-02-01 : 03:04:47
Not much you can do here...it will scan the entire table more or less regardless of what you do. It could be benificial to add a clustered index to the temp-table though...might have an impact if there are many rows in it.

- Lumbago

My blog-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -