简体   繁体   English

在 PostgreSql 中批量更新和删除哪个效率更高?

[英]In PostgreSql which is more efficient bulk Update or delete?

In my java web application, I need to delete a set of records from a table.在我的 java web 应用程序中,我需要从表中删除一组记录。 I have 2 options:我有两个选择:

  1. Directly run delete query from the application直接从应用程序运行删除查询
  2. Update a value in all the records to hide them from the user view and let a daemon thread run and clean up these records in the background.更新所有记录中的值以将它们从用户视图中隐藏,并让守护线程运行并在后台清理这些记录。

The number of records can range from 10 to 100000. My intent here is to know the efficient and safe way to do that without putting too much load on the server.记录的数量可以在 10 到 100000 之间。我的目的是了解在不给服务器增加太多负载的情况下实现这一点的有效且安全的方法。

PS: Feel free to share any other suitable approach. PS:随意分享任何其他合适的方法。

Under the hood, an UPDATE is actually:在引擎盖下, UPDATE实际上是:

  1. Flag updated row as deleted将更新的行标记为已删除
  2. Insert new row, which is a copy of the row in 1. above, but reflecting updated columns插入新行,这是上面 1. 中行的副本,但反映了更新的列

Therefore, since an UPDATE operation involves two writes, a DELETE is actually more efficient, as it simply flags the deleted row, using one write (because of the Multi-Version Concurrency Control (MVCC) framework)因此,由于UPDATE操作涉及两次写入,因此DELETE实际上更有效,因为它只是使用一次写入标记已删除的行(因为多版本并发控制 (MVCC) 框架)

Disclosure: I work for EnterpriseDB (EDB)披露:我为EnterpriseDB (EDB)工作

Update command takes much more time as compared to delete command.与删除命令相比,更新命令需要更多时间。 In the update, you are actually making a copy of the data and the old version of the data is just hidden from the view.在更新中,您实际上是在制作数据的副本,而旧版本的数据只是从视图中隐藏。

If you require these records in the future, you can move these records in another table.如果以后需要这些记录,可以将这些记录移到另一个表中。

The advantage of this approach are:-这种方法的优点是:-

  1. You are not overloading your current table, Hence all operations for this table will be fast.您没有超载当前表,因此该表的所有操作都会很快。
  2. You can use these records in the future.您将来可以使用这些记录。

Update更新

As you don't need the records and as update is more expensive then delete you should issue a delete call in batches of let say 500 to 5000 records.由于您不需要记录并且更新比删除更昂贵,因此您应该分批发出删除调用,比如说 500 到 5000 条记录。

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

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