简体   繁体   中英

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. Here's what I'm currently doing in 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.

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

You also may try wrapping your call in an ActiveRecord::Base.transaction block

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

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 . This will cause deletions in purse SQL. Warning: Ruby callbacks like before_destroy will not get called

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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