[LRUG] Request for assistance with Rails performance oddity
Andrew Stewart
boss at airbladesoftware.com
Sat Oct 30 09:35:01 PDT 2010
On 30 Oct 2010, at 10:07, John Winters wrote:
> Before loading events for a range of dates the code purges any existing
> events in that range. It does this by the extraordinarily inefficient
> method of loading all events and destroying all those in the specified
> range. (In my defence, there is a note just above that line of code
> saying it needs to be converted to a single SQL call.)
>
> It seems that the remaining events (still 26858 of them) then hang
> around in memory and ActiveRecord takes a quick glance at each of them
> before creating each new event record. Lots of quick glances add up to
> a load of CPU time.
>
> By purging old events using a separate run of the script and then
> loading new events with a fresh run I can get back to the original fast
> speed.
If you use #delete_all then ActiveRecord will issue a SQL call without loading the individual records into memory. If you use #destroy_all, ActiveRecord loads each one in turn into memory so it can fire before/after_destroy callbacks and cascade to associations.
Cheers,
Andy Stewart
-------
http://airbladesoftware.com
More information about the Chat
mailing list