У нас есть следующая хранимая процедура:
ALTER procedure [dbo].[spMergePPObjectBlobProperty]
(
@values dbo.udtPPObjectBlobProperty readonly
)
as
begin
begin try
declare @updatetime datetime
set @updatetime = GetUTCDate()
merge tblPPObjectBlobProperty as t
using (select * from @values) as s
on t.InsertionId = s.InsertionId and t.PropertyMapNameId = s.PropertyMapNameId
when matched then
update set UpdateTime = @updatetime, [Value] = s.BlobValue, UpdateId = s.UpdateId
when not matched then
insert (PropertyMapNameId, Value, UpdateId, UpdateTime, InsertionId)
values(s.PropertyMapNameId, s.BlobValue, s.UpdateId, @updatetime, s.InsertionId)
option (loop join);
end try
begin catch
declare @errormessage varchar(256)
-- Get the error message
select @errormessage = ERROR_MESSAGE()
-- Raise an error and return
raiserror('Error updating entries in the tblPPObjectBlobProperty Table. %s', 16, 1, @errormessage)
end catch
end
Иногда выполнение этой sp может занять несколько секунд с добавлением всего нескольких строк. В других случаях это очень быстро.
У нас есть несколько этих sps, и это единственный, который иногда кажется медленным, и единственный, который вставляется в таблицу с столбцом varbinary (max) .
Используемый здесь тип определяется следующим образом:
CREATE TYPE [dbo].[udtPPObjectBlobProperty] AS TABLE(
[InsertionId] [bigint] NOT NULL,
[PropertyMapNameId] [int] NOT NULL,
[BlobValue] [varbinary](max) NULL,
[UpdateId] [bigint] NULL,
PRIMARY KEY CLUSTERED
(
[InsertionId] ASC,
[PropertyMapNameId] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO
Можно ли каким-либо образом оптимизировать эту хранимую процедуру?