[LRUG] Request for assistance with Rails performance oddity

John Winters john at sinodun.org.uk
Sat Oct 30 02:07:24 PDT 2010


On 30/10/10 07:17, Levent Ali wrote:
> Could activerecord be trying to load existing records into memory?

Funnily enough - yes.

I've got a bit further with figuring this out, and it's caused by an
after-effect of an earlier bit of code.

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.

Is there a way of telling ActiveRecord to discard any cached database
records it currently has in memory?

TIA,
John



More information about the Chat mailing list