简体   繁体   English

如何有效删除与另一条记录相关且大于某个阈值的所有记录?

[英]How do I delete all records related another record and greater than a certain threshold efficiently?

I'm trying to delete all notifications older than the first 99 for every user. 我正在尝试为每个用户删除所有早于前99个的通知。 Here's what I'm currently doing in rails: 这是我目前在rails中执行的操作:

User.all.each do |u|
  u.notifications.order('created_at DESC').offset(99).destory_all
end

This works fine, but it's horribly inefficient. 这可以正常工作,但是效率很低。 You end up with a lookup for every user plus a delete query for every notification. 最后,您将为每个用户查找一个查询,并为每个通知添加一个删除查询。

How would I go about doing this in a single query or at least more efficiently? 我将如何在单个查询中执行此操作,或者至少要更有效地执行此操作?

That looks pretty good so far. 到目前为止看起来还不错。 Here are a few improvements I can think of 这是我能想到的一些改进

When call user, use .includes to eager-load their notifications and .find_each for batching. 呼叫用户时,请使用.includes来加载其通知,并使用.find_each进行批处理。

User.includes(:notifications).find_each do |u|
  ...
end

You also may try wrapping your call in an ActiveRecord::Base.transaction block 您也可以尝试将呼叫包装在ActiveRecord::Base.transaction块中

ActiveRecord::Base.transaction do User.all.each do |u| ActiveRecord :: Base.transaction做User.all.each做| u | u.notifications.order('created_at DESC').offset(99).destory_all end end u.notifications.order('created_at DESC')。offset(99).destory_all结束端

This will perform a batch transaction, instead of one-by-one. 这将执行批处理事务,而不是一个接一个的事务。

Now, if you really want to make an improvement, use .delete_all instead of .destroy_all . 现在,如果您确实要进行改进,请使用.delete_all而不是.destroy_all This will cause deletions in purse SQL. 这将导致钱包SQL中的删除。 Warning: Ruby callbacks like before_destroy will not get called 警告:Ruby回调如before_destroy不会被调用

暂无
暂无

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

相关问题 如何删除所有相关记录? - How do I delete all related records? 如何在另一个表中显示每个半相关记录的所有记录? - How do I show all records from one table for each semi-related record in another? SQL:分组和计数,但仅限于所有记录都大于阈值 - SQL: group and count but only if ALL records are greater than a threshold 如何在 SQL 服务器中查询大于特定日期的所有日期? - How do I query for all dates greater than a certain date in SQL Server? 如何创建SQL Delete,它也会删除相关记录 - How do I create SQL Delete that will also delete related records 如何删除表中具有另一个表中相应记录的所有记录 - How do I delete all the records in a table that have corresponding records in another table 查询从同一表中的另一条记录获取值并按大于间隙阈值的差异进行过滤 - Query for getting value from another record in same table and filter by difference greater than a gap threshold 如何使用 Microsoft SQL Server 2008 获取日期比今天的日期大 6 个月的所有记录? - How do I get all records where date is 6 months greater than today's date, using Microsoft SQL Server 2008? 如何编写 SQL 查询以查找与另一个表中的记录相关的记录而忽略另一条记录 - How can I write a SQL query to find records related to a record in another table ignoring another record 如何删除记录以及ASP.NET(C#)中的所有相关记录 - How to delete a record along with all related records in ASP.NET (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM