Postgres:更新未更改的行
假设,我有一个以下查询:
UPDATE table_name
SET column_name1 = column_value1, ..., column_nameN = column_valueN
WHERE id = M
问题是,column_value1,...,column_valueN 没有改变。该查询是否会真正执行?与使用实际更改的数据进行更新相比,这种情况下的性能如何?如果我每页有大约 50 个此类查询且数据未更改怎么办?
Say, I have a following query:
UPDATE table_name
SET column_name1 = column_value1, ..., column_nameN = column_valueN
WHERE id = M
The thing is, that column_value1, ..., column_valueN have not changed. Will this query be really executed and what about performance in this case comparing to update with really changed data? What if I have about 50 of such queries per page with not-changed data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要通过仅指定更改的列和行来帮助 postgresql。它将继续对您指定的任何内容执行更新,而不检查数据是否已更改。
ps 这就是 ORM 派上用场的地方。
编辑:您可能还对 如何加快速度感兴趣在 PostgreSQL 中进行更新/替换操作?,其中 OP 经历了所有麻烦来加速 UPDATE 性能,而仅通过更新更改的数据就可以实现最佳性能。
You need to help postgresql here by specifying only the changed columns and rows. It will go ahead and perform update on whatever you specify without checking if the data has been changed.
p.s. This is where ORM comes in handy.
EDIT: You may also be interested in How can I speed up update/replace operations in PostgreSQL?, where the OP went through all the troubles to speed up UPDATE performance, when the best performance can be achieved by updating changed data only.