简体   繁体   English

SQL Math更新性能优化

[英]SQL Math update performance optimizing

I am running simple update over >70 Mio. 我正在> 70 Mio运行简单更新。 Records (MS-SQL Server 2008) 记录(MS-SQL Server 2008)

UPDATE T1 SET COSTS=AMOUNT*0.003

and it takes up to 8 Hours. 最多需要8个小时。

Is there an simple "cheap" way to improve math performance of updates like this? 是否有一种简单的“便宜”方法来改善此类更新的数学性能?

And since there is no WHERE clause or compares, just multiply every record with value, creating an INDEX will have no effect... or? 而且由于没有WHERE子句或不进行比较,所以只需将每条记录与值相乘,创建INDEX将没有效果...或?

Thank you very much in advance for your time. 非常感谢您抽出宝贵的时间。

Writing 70 Mio rows does take time. 写入70 Mio行确实需要时间。 Adding an index would make things even worse, because the index has to be updated along with the table. 添加索引会使情况更糟,因为必须将索引与表一起更新。

Is this update really necessary? 此更新真的必要吗? Can't you solve the task with a view or computed column or whatever? 您不能使用视图或计算列或其他方法解决任务吗?

I fully agree with the above comments. 我完全同意以上评论。 The issue is not match calculations, instead it is the I/O of the database engine. 问题不是匹配计算,而是数据库引擎的I / O。

I just want to suggested computed columns. 我只想建议计算列。 You can add a "virtual" column to the table that is calculated from other values in the same row. 您可以在表中添加一个“虚拟”列,该列是根据同一行中的其他值计算得出的。 This means that you might be able to dispense with the update altogether. 这意味着您可以完全省去更新。 Just alter the table to add a column: 只需更改表以添加一列:

alter table add costs_new as amount * 0.03.

To keep the current name, you need to change the old name: 要保留当前名称,您需要更改旧名称:

exec sp_rename 't1.costs', 'costs_old', 'column';
alter table add costs as amount*0.03;

This may be a quick-and-dirty way to do what you need. 这可能是您需要的快速而肮脏的方法。

Your problem is not math (calculation of that value), but the fact that sql server needs to read every record and write it to the HDD again. 您的问题不是数学(该值的计算),而是sql服务器需要读取每条记录并将其再次写入HDD的事实。 Since calculation itself is really cheap, you can just have costs as calculated field wherever you need it. 由于计算本身确实很便宜,因此只要有costs ,就可以将costs作为计算字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM