求优化存储过程
CREATE procedure ProductSearch
(
@pagesize int,
@pageindex int,
@Keywords varchar(50),
@Address varchar(50),
@docount bit
)
as
BEGIN TRAN
DECLARE @COUNT INT
DECLARE @SID INT
set nocount on
if(@docount=1)
if(@Address <> ' ')
begin
IF((select count(ProductID) from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join FREETEXTTABLE(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY] where Company.Address LIKE '% '+@Address+ '% ') <21)
SET @COUNT=(select count(ProductID) from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join FREETEXTTABLE(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY] where Company.Address LIKE '% '+@Address+ '% ')
ELSE
SET @COUNT=20
select count(ProductID)+@COUNT from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join containstable(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY]
where Company.Address LIKE '% '+@Address+ '% '
end
else
begin
IF((select count(ProductID) from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join FREETEXTTABLE(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY]) <21)
SET @COUNT=(select count(ProductID) from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join FREETEXTTABLE(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY])
ELSE
SET @COUNT=20
select count(ProductID)+@COUNT from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID inner join containstable(Products,Title,@Keywords) as Product2
on Product1.ProductID=Product2.[KEY]
end
else
begin
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
if(@Address <> ' ')
begin
BEGIN
insert into @indextable(nid) select top 20 ProductID from Products as Product1 inner join Company on Product1.BusinesseID=Company.BusinesseID
inner join containstable(Products,Title,@Keywords) as Product2 on Product1.ProductID=Product2.[KEY] inner join Businesses on Product1.BusinesseID=Businesses.BusinesseID
where Businesses.Grade=