ActiveRecord Table Transform (or, how to write to the db 27,000 times in 24 seconds instead of 9 minutes)

I implemented my new scheme and running time went from 9 minutes to 24 SECONDS. I liked this approach so much I decided to generalize it as ActiveRecord::Base.transform. Sample usage:

# if users don't have names, give them a random one
NAMES = ['Adam', 'Ethan', 'Patrick']
User.transform(:name, :conditions => 'name is null').each do |i| = NAMES[rand * NAMES.length]

Really interesting use of temp tables here.



1 response
This sure brings back memories. I learned this technique over 10 years ago in a C++ app. We didn't have an ORM, of course, so we always thought in terms of SQL, not objects. ORMs handle the basics but you'll always go back to fundamentals eventually.