Ric*_*cky 2 sql sql-server asp.net performance
我想加快以下查询
WHERE子句中有两个条件(参见下面的查询以供参考)
目前,大约需要60秒.但是,如果我删除where子句中的第一个条件(@Query为NULL),则它几乎立即返回.
有关如何加快速度的想法吗?表中约有700k行,这只会增长.
(注意:下面显示的查询被剥离为它的本质,我严格使用硬编码值来简化查询,以便将焦点引向上面概述的部分)
declare @Query nvarchar(255)
select @Query = 'oceans'
select
*
from
(select
row_number() over( order by b.BookTitle) as RowNumber,
b.*
from
Books b (nolock)
where
-- If I remove this first condition "@Query is NULL", then it returns almost immediately
-- Otherwise if I keep this here, it takes around 1 minute
-- Yes, I have full-text index on BookTitle, as well as a regular index.
(@Query is NULL) or (contains(b.BookTitle, @Query))
) as t1
where t1.RowNumber between 40 and 60
Run Code Online (Sandbox Code Playgroud)
你能把它分成两个查询吗? or通常会导致优化器出现问题:
if @Query is null
begin
select *
from (select row_number() over( order by b.BookTitle) as RowNumber, b.*
from Books b (nolock)
where @Query is NULL
) as t1
where t1.RowNumber between 40 and 60;
end
else
begin
select *
from (select row_number() over( order by b.BookTitle) as RowNumber, b.*
from Books b (nolock)
where contains(b.BookTitle, @Query)
) as t1
where t1.RowNumber between 40 and 60;
end
Run Code Online (Sandbox Code Playgroud)