我正在使用MS SQL并拥有一张表,其中包含数百个工作成本中心的数百万活动。我们希望根据不同的管理水平和报告结构来分析数据。因此,我尝试使用主数据表中的记录更新数据。
到目前为止一切正常,但速度太慢了。这就是我现在这样做的方式:
UPDATE [Activity - E].dbo.td_Import
SET [Sender.Type] = tmp_CC.[Type],
[Sender.Person Responsible] = tmp_CC.[Person Responsible],
[Sender.Level1] = tmp_CC.[Level 1],
[Sender.Level2] = tmp_CC.[Level 2],
[Sender.Level3] = tmp_CC.[Level 3],
[Sender.Level4] = tmp_CC.[Level 4],
[Sender.Active] = tmp_CC.[Active]
FROM ( SELECT [Cost Center], [Type], [Person Responsible], [Level 1], [Level 2], [Level 3], [Level 4], [Active]
FROM [Master Data - E].dbo.md_CostCenter md_CC
WHERE (md_CC.[Record Invalid] LIKE '') OR (md_CC.[Record invalid] IS NULL)) tmp_CC
WHERE ([Sender.CC] LIKE tmp_CC.[Cost Center]) AND ([Sender.Type] IS }NULL)
我必须运行类似的代码五次,大约需要90分钟才能更新所有记录。由于数据应该全天候提供给管理层,我只有一个约两小时的短窗口来获取数据,更新所有信息并为管理者创建单独的报告(excel报告,每个报告大约需要10分钟的宏)
我正在考虑使用主数据表加入数据,但我在所有教程中都完全迷失了。我得到的只是一些临时表,但我的活动表(td_Import)中没有存储任何内容。
对初学者有什么建议吗?非常感谢。
克里斯
答案 0 :(得分:0)
我想我终于得到了加入的概念。
以下代码将110万条记录的运行时间从30多分钟改为3分钟。 此外,我还提供了一个视图,以减少有效成本中心的选择(查看[活动成本中心])。
UPDATE [Activity - E].dbo.td_Import
SET [Sender.Type] = md_CC.[Type],
[Sender.Person Responsible] = md_CC.[Person Responsible],
[Sender.Level1] = md_CC.[Level 1],
[Sender.Level2] = md_CC.[Level 2],
[Sender.Level3] = md_CC.[Level 3],
[Sender.Level4] = md_CC.[Level 4],
[Sender.Active] = md_CC.[Active]
FROM [Activity - E].dbo.td_Import td_Imp
LEFT JOIN [Master Data - E].dbo.[Active Cost Centers] md_CC
ON td_Imp.[Sender.CC] LIKE md_CC.[Cost Center]
我希望这会对其他人有所帮助。
克里斯