我有一个功能,我想提高效率。我知道大部分成本都在寻找索引上。我想知道是否有办法将这两个索引搜索合并为一个。
这是我的功能
ALTER FUNCTION [dbo].[Search]
(
@Query nvarchar(150)
)
RETURNS @result TABLE
(
ID int,
ResultPhrase nvarchar(max),
Domain nvarchar(50)
)
AS
BEGIN
declare @id int,
@OffsetArticle nvarchar(50),
@OffsetProfile nvarchar(50),
@index int,
@domain nvarchar(50),
@StringResult nvarchar(max),
@substrIndex int,
@substrCount int,
@field nvarchar(40),
@begin int,
@end int
DECLARE pidCursor CURSOR FAST_FORWARD
FOR select id,OffsetArticle,OffsetProfile from PerfTest where contains (data,@Query)
open pidCursor
FETCH NEXT FROM pidCursor INTO @id,@OffsetArticle,@OffsetProfile
while (@@FETCH_STATUS = 0)
begin
select @index = CHARINDEX(@Query,data) from PerfTest where ID=@id
exec @domain = dbo.FindDomain @id=@id ,@index=@index, @offsetArticle=@OffsetArticle, @offsetProfile=@OffsetProfile
if(@domain = N'Profile')
set @end = SUBSTRING(@offsetProfile,CHARINDEX(N'-',@offsetProfile)+1,len(@offsetProfile)-CHARINDEX(N'-',@offsetProfile))
set @begin = SUBSTRING(@offsetProfile,1,CHARINDEX(N'-',@offsetProfile)-1)
if(@domain = N'Article')
set @end = SUBSTRING(@OffsetArticle,CHARINDEX(N'-',@OffsetArticle)+1,len(@OffsetArticle)-CHARINDEX(N'-',@OffsetArticle))
set @begin = SUBSTRING(@OffsetArticle,1,CHARINDEX(N'-',@OffsetArticle)-1)
if(@index - 20 < CAST(@begin as int))
set @substrIndex = @index
else
set @substrIndex = @index - 20
if(@index + 150 > CAST(@end as int))
set @substrCount = @end
else
set @substrCount = @index + 150
select @StringResult = SUBSTRING(data,@substrIndex,@substrCount) from PerfTest where ID=@id
insert @result (id,ResultPhrase,Domain) values (@id,@StringResult,@domain)
if(@@FETCH_STATUS = 0)
FETCH NEXT FROM pidCursor INTO @id,@OffsetArticle,@OffsetProfile
end
CLOSE pidCursor
DEALlOCATE pidCursor
RETURN
END