The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
ActiveRecord::Base.all (without conditions) Considered Harmful

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Daniel Berger

Posts: 1383
Nickname: djberg96
Registered: Sep, 2004

Daniel Berger is a Ruby Programmer who also dabbles in C and Perl
ActiveRecord::Base.all (without conditions) Considered Harmful Posted: Feb 16, 2011 2:23 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Daniel Berger.
Original Post: ActiveRecord::Base.all (without conditions) Considered Harmful
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Daniel Berger
Latest Posts From Testing 1,2,3...

Advertisement
We've pretty much reached the conclusion that ActiveRecord::Base.all, or .find(:all) if you prefer, should never be used without conditionals. If you want to iterate over every record in the table, you should use my custom .foreach method.

Some recent site flakiness inspired me to do some comparisons of a .all vs a .foreach. The details are below. For the lazy, the short version is that a .all call will suck down rather large amounts of RAM and CPU, growing larger as the number of records grows. The .foreach call consumes about the same amount of CPU, but with low and steady RAM usage.

Table: Teacher

Overview:

61572 records.

28 columns, mostly varchar (character) fields of with
a size of about 25, with one field at 4000 characters, but which is usually empty.

Specs:

Ubuntu 10.04 (lucid)
Kernel Linux 2.6.32-28-generic
Memory: 1.9 GB
2 CPU, dual core 3GHz Intel
Ruby 1.8.7-p330
Rails 3.0.4

=> Teacher.all

Memory: 163mb
%62.5 CPU
%7.61 MEM.
Total time: About 30 seconds.

=> Teacher.foreach

Memory: 41mb
%68.34 CPU
%1.42 MEM
Total time: About 38 seconds.

The figures I'm showing are what the process finally capped out at. The .all call starts off at 41mb and climbs until it finishes the process, while the .foreach call sits steady at 41mb, though its CPU usage climbs about as much as the .all call.

This, of course, will only get worse on tables with larger datasets, and will absolutely hammer your server. Frankly, I think the find_each implementation ought to be the default implementation, though last I checked CPK broke it.

Read: ActiveRecord::Base.all (without conditions) Considered Harmful

Topic: ActiveRecord::Base.all (without conditions) Considered Harmful Previous Topic   Next Topic Topic: The nuts-and-bolts of configuring Resque and resque-web for a production Rails app

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use