我正在尝试删除只有大约39K记录的表,但由于某些原因,即使删除单个记录也需要时间(大约1.5分钟)。如何提高删除操作的性能。如何确保日志活动不会花费太多时间。我可以将“DELETE”语句放在while循环中,然后打开一个事务并在每次成功完成时提交它。还有其他有效的方法吗?
[PrimaryKey]这里有一个“聚集索引”
DECLARE @BatchCount INT;
SELECT @BatchCount = COUNT(1) FROM #DHDID
DECLARE @Counter INT = 1
WHILE( @Counter <= @BatchCount)
BEGIN
BEGIN TRANSACTION
DECLARE @ID INT;
SELECT @ID = DHDID FROM #DHDID WHERE ID = @Counter
DELETE FROM <MYTABLE> WHERE [PrimaryKey] = @ID
COMMIT TRANSACTION
SET @Counter = @Counter + 1
END
答案 0 :(得分:1)
我会尝试在#DHDID表上创建索引:
CREATE NONCLUSTERED INDEX [idx] ON [#DHDID] ([ID] ASC) INCLUDE ([DHDID])