27,579
社区成员
发帖
与我相关
我的任务
分享
select top 5 BookID,BookName,b.Author,PressTime,Thumbnail from dbo.BookInfo b
where
charindex(b.Booktypeid,(select BookTypeID from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true')) >0
or
(charindex(author,(select author from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true')) > 0)
or
(charindex(press,(select press from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true')) > 0)
--1.在表BookInfo的Booktypeid author press字段創建合適索引
--2.轉換 Booktypeid author press成列表形式 ,以便能使用到索引
Declare @sql nvarchar(4000)
if object_id('Tempdb..#') Is Not Null Drop Table #
if object_id('Tempdb..#QueryPlan') Is Not Null Drop Table #QueryPlan
Create Table #QueryPlan(Detail nvarchar(50),DetailType smallint) /* 1.BookTypeID 2.author 3.press */
Create clustered Index IX_#QueryPlan On #QueryPlan (DetailType Asc)
select author,BookTypeID,press Into # from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true'
If Exists(Select 1 from # Where BookTypeID>'')
Begin
Set @sql='Select 1,'''+Replace((Select BookTypeID From #),',',''' union All Select 1,''')+''''
insert into QueryPlan(DetailType,Detail) Exec (@sql)
End
If Exists(Select 1 from # Where author>'')
Begin
Set @sql='Select 2,'''+Replace((Select author From #),',',''' union All Select 2,''')+''''
insert into QueryPlan(DetailType,Detail) Exec (@sql)
End
If Exists(Select 1 from # Where press>'')
Begin
Set @sql='Select 3,'''+Replace((Select press From #),',',''' union All Select 3,''')+''''
insert into QueryPlan(DetailType,Detail) Exec (@sql)
End
--3修改查詢語句,查看執行計劃,是否應用到索引,調整索引,這樣就能提高查詢效率
--3.a
select top 5 BookID,BookName,b.Author,PressTime,Thumbnail
from dbo.BookInfo b
Where Exists(Select 1
From #
Where (Detail=b.BookTypeID And DetailType=1) --BookTypeID
Or (Detail=b.author And DetailType=2) --author
Or (Detail=b.press And DetailType=3) --press
)
--
select
top 5 BookID,BookName,b.Author,PressTime,Thumbnail from dbo.BookInfo b
where
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true' and
(charindex(b.Booktypeid,BookTypeID)>0 or charindex(b.author,author) or charindex(b.press,press)>0))
select top 5 BookID,BookName,b.Author,PressTime,Thumbnail from dbo.BookInfo b
where
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true' and
(charindex(b.Booktypeid,BookTypeID)>0 or charindex(b.author,author) or charindex(b.press,press)>0))
--或
select top 5 BookID,BookName,b.Author,PressTime,Thumbnail from dbo.BookInfo b
where
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true'
and (charindex(b.Booktypeid,BookTypeID)>0 or charindex(b.author,author)>0 or charindex(b.press,press)>0))
select top 5 BookID,BookName,b.Author,PressTime,Thumbnail from dbo.BookInfo b
where
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true' and charindex(b.Booktypeid,BookTypeID)>0)
or
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true' and charindex(b.author,author)>0)
or
exists (select 1 from dbo.QueryPlan where UserID = 'sunyusheng' and Flag = 'true' and charindex(b.press,press)>0)