首先我在book表建立了全文索引
然后exec sp_fulltext_service verify_signature,0保证缓存不被清除,保证以后的查询速度
-----------------------------------------
--根据书名搜索图书
create PROCEDURE getBookListSearchByBookName
@keyword nvarchar(500),
@startRow int,--如果每页是20条,第五页的@startRow=100
@stopRow int,--如果每页是20条,第五页的@stopRow=120
@rowCount int output
AS
if @rowCount=0
begin
SELECT @rowCount=count(*)  
FROM ContainsTABLE(book,name,@keyword)--根据多次试验,containstable比contains快10倍,当然也就是零点几秒左右
endif @rowCount=0 returndeclare @t_table table---- 建立有标识符列的table变量
(
[rownum] [int] IDENTITY (1, 1) Primary key NOT NULL,
[id] int,
[isbn] [varchar] (100) ,
[sortId] [smallint] ,
[bookName] [nvarchar](50) ,
[author] [nvarchar](50) ,
[publishingCompanyCode] [varchar](10) ,
[pageCount] [int],
[publishDate] [datetime],
[listPrice] [decimal](10,2),
[summary] [nvarchar](500),
[publishingCompanyName] [nvarchar](20),
[salePrice] [decimal](10,2),
[salePriceVip] [decimal](10,2),
[averageMark] [decimal](4,1),
[storeCounts] [int]
)
SET NOCOUNT ON--关闭统计
Set RowCount @stopRow---- 在返回指定的@stopRow行数之后停止处理查询insert into @t_table
(
[id],[isbn],[sortId],[bookName],[author],[publishingCompanyCode],[pageCount],
[publishDate],[listPrice],[summary],[publishingCompanyName],[salePrice],[salePriceVip],[averageMark],[storeCounts]
)
select b.id,b.isbn,b.sortId,b.name,b.author,b.publishingCompanyCode,b.pageCount,
b.publishDate,b.listPrice,b.summary,pc.name,
isnull(b.salePrice,0) as salePrice,
isnull(b.salePriceVip,0) as salePriceVip,
isnull(b.averageMark,0) as averageMark,
isnull(b.storeCounts,0) as storeCounts
from book b
inner join publishingCompany pc on b.publishingCompanyCode=pc.code
where CONTAINS(b.name,@keyword)
order by b.sales desc--sales已建非聚集索引SELECT 
[id],[isbn],[sortId],[bookName],[author],[publishingCompanyCode],[pageCount],
[publishDate],[listPrice],[summary],[publishingCompanyName],[salePrice],[salePriceVip],[averageMark],[storeCounts]
FROM @t_table WHERE rownum >= @startRow ORDER BY rownum---- 返回到正确的结果
GO