我正在尝试更新包含~90,000行的表中的列。是否有更新表的优化方法?
我添加了必要的索引..以便不会发生表扫描/查找。但是仍然需要很长时间才能运行(1小时)。
我的情景:
DECLARE @ParentID NVARCHAR(100),
@Con_ERID INT
DECLARE @MaxCount INT,
@MinCount INT,
@Id INT
SELECT @MaxCount = MAX(Id) from [dbo].[ParentIDStaging] where Type='grid'
SET @MinCount = 1
WHILE @MinCount <= @MaxCount
BEGIN
SELECT @Id = ConID FROM [dbo].[ParentIDStaging] WHERE Id = @MinCount and Type = 'grid'
IF @Id IS NOT NULL
BEGIN
SELECT @Con_ERID = ErId FROM Context (NOLOCK) Where ConId = @Id
SELECT @ParentID = Identifier FROM Recording (NOLOCK) where ErId = @Con_ERID
BEGIN TRAN
UPDATE [ParentIDStaging] WITH (ROWLOCK)
SET [ParentID] = @ParentID
WHERE ContentType = 'grid'
AND ConID = @Id
COMMIT
END
SET @MinCount = @MinCount + 1
END
答案 0 :(得分:2)
循环缓慢。尝试在一次更新中执行此操作,并使用连接包含相关的其他表。您的查询可能会像这样写(不知道您的实际架构):
UPDATE PS
SET PS.ParentID = Recording.Identifier
FROM ParetnIDStaging PS
JOIN Context on (Context.ConId = PS.ConId)
JOIN Recording on (Recording.ErId = Context.ErId)
WHERE ...
答案 1 :(得分:0)
这是因为您一次循环和更新一条记录并使用显式锁定/事务。
在不知道你的基础结构的情况下 - 我敢打赌你可以通过选择更新来做你正在尝试的事情。
答案 2 :(得分:0)
UPDATE ParentIDStaging
SET parentIdStaging.ParentID=recording.Identifier
from ParentIDStaging
join Context on context.ConId = ParentIDStaging.ConId
join recording on contect.erid=recording.erId
WHERE parentIdStaging.ContentType = 'grid'
AND parentidStaging.Type='grid'