我正在接管一些试图一次存储 ID 列表的代码,我发现这段代码对于我们试图完成的操作来说运行速度很慢。另外,在某些情况下,由于大量 id 会导致死锁。
USE [store]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateImagePriority]
@separator CHAR(1),
@filename varchar(50),
@parentId int,
@slaveIds varchar(8000)
AS
BEGIN
SET NOCOUNT ON
DECLARE @SLAPriorityint
DECLARE @separator_position INT
DECLARE @array_value VARCHAR(50)
SET @slaveIds = @slaveIds + @separator
SET @SLAPriority= 0
WHILE PATINDEX('%' + @separator + '%', @slaveIds ) <> 0
BEGIN
SET @SLAPriority= @SLAPriority+ 1
SELECT @separator_position = PATINDEX('%' + @separator + '%',@slaveIds )
SELECT @array_value = LEFT(@slaveIds , @separator_position - 1)
SELECT Array_Value = @array_value
SELECT @slaveIds = STUFF(@slaveIds , 1, @separator_position, '')
UPDATE image_info
SET SLA_PRIORITY = @SLAPriority
WHERE FILE=@filename and EXT_PAR_ID=@parentId and SLA_ID=@array_value
END
SET NOCOUNT OFF
END
这是我们将传入的示例:
例如
separator = ','
filename = 'burgerking'
parentId = '1859'
slaveIds = '15,16,19,20,21,25,28,29,30,38,99'
有关如何提高此代码速度的任何建议。
提前致谢!