有两个表,Costs
和Logs
。表中的数据Costs
可以是数百万行,Logs
表中的数据可以是数十亿行。
我需要在每次运行 100 条记录内更新生产环境中服务任务的表中的CostBy
列。Costs
CREATE TABLE Cost
(
C_PK uniqueidentifier primary key not null,
C_CostBy varchar(3) not null
)
CREATE TABLE Logs
(
L_PK uniqueidentifier primary key not null,
L_ParentTable varchar(255) not null, -- Table Cost and other table's name
L_ParentID uniqueidentifier not null, -- Cost's pk and other table's pk
L_Event varchar(3) not null, -- Part are 'ADD' and other event types
L_User varchar(3) not null
)
CREATE NONCLUSTERED INDEX [L_ParentID]
ON [dbo].[Costs] ([L_ParentID] ASC)
这是原始更新声明:
UPDATE TOP(100) Costs
SET CostBy = ISNULL(L_User, '~UK')
FROM Costs
LEFT JOIN Logs ON L_ParentID = C_PK AND L_Event = 'ADD'
WHERE CostBy = ''
但是,该语句引入了一个海量的性能问题,table scan in table 的成本很高Costs
。
我的问题是如何避免表中的表扫描Costs
或如何优化更新语句?
提前致谢。