Chris Rericha wrote:> Does anyone know the difference in performance between an
> ActiveRecord::find and Enumerable::find (or detect since ActiveRecord
> redefines the find method) for a has_many association?
>
> Here is an example:
>
> I have a list that has 10 lines and I want to find the first blank line.
> Which approach would be better?
>
> list.lines.detect { |line| line.content == nil }
>
> or
>
> list.lines.find_by_content_id(nil)
>
> I appreciate any insight you can provide.
There is no correct answer to such a generic question. It depends on a
number of factors including the size of your dataset, which database
software you are using, the relative performance/load of your webserver
and database server boxes, network architecture, whether you have the
lines cached in Rails already, etc. In general, network latency means a
trip to the db server is going to be slower than a Ruby operation on an
array. However, if you haven''t fetched the lines already, it may be
faster to have the db pick the empty one for you.
But remember, premature optimization is the root of all evil. If the
peformance difference is small enough not to matter, don''t waste your
time thinking about it. When it becomes an issue, do some performance
monitoring to figure out where the time is going, then address the
bottlenecks you uncover.
--
Josh Susser
http://blog.hasmanythrough.com
--
Posted via http://www.ruby-forum.com/.