[Rails] find_by* efficiency - blocks?
Ara.T.Howard
Ara.T.Howard at noaa.gov
Sat Feb 26 04:30:34 GMT 2005
On Fri, 25 Feb 2005, hadley wickham wrote:
>> no. here is an example using sqlite:
>>
>> this technique uses sqlite's ability to pass a block with which to handle each
>> tuple as it is returned from the db.
>>
>> jib:~ > cat a.rb
>> #!/usr/bin/env ruby
>> require 'sqlite'
>> db = SQLite::Database::new(ARGV.shift, 0)
>> db.use_array = true
>> db.execute('select * from jobs'){|tuple| p tuple}
>>
>> jib:~ > time a.rb db > /dev/null
>>
>> real 0m1.510s
>> user 0m1.420s
>> sys 0m0.080s
>
> Aaaaah. Now I see - I was actually doing something similar (dumping
> 30,000 records into a CSV file) a couple of days ago and thought there
> must be a better way than loading them all into memory at once (and
> this obviously that better way).
the csv module that comes with ruby supports iterator type usage both for
reading and writing - i was just using it today ;-)
> Thanks for the detailed explanation and please do look into adding this
> functionality to rails!
it would be very, very easy to do - only bariers would be time and ability to
test things like DB2, which i do not have access to.
cheers.
-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| When you do something, you should burn yourself completely, like a good
| bonfire, leaving no trace of yourself. --Shunryu Suzuki
===============================================================================
More information about the Rails
mailing list