Pedro Morte Rolo Pedro Morte Rolo - 1 year ago 66
Ruby Question

Making Rails #destroy_all run faster

I want to run

, though, each alarm is associated to many
s, and each
is associated to many
s,being both associations marked as

So, when I invoke Alarm.destroy all, this invokation is taking ages to run. Is there any way I could make it faster? How?

Until now I've tried
and it is still slow.

Answer Source

The faster alternative to destroy_all is delete_all but this won't chase down and destroy any dependencies, nor will it trigger any before_destroy or after_destroy hooks.

If you're really concerned about speed, you'd probably re-write this with this in mind. You can even rack it up as a series of conditions:

Alarm.delete_all(:id => alarm_ids)
AlarmEvent.delete_all(:alarm_id => alarm_ids)