もっと効率的にしたい機能があります。コストの大部分はインデックスを探すことにあることを私は知っています。これら2つのインデックスシークを1つにマージする方法があるかどうか知りたいです。
これが私の機能です
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