# Dbase query performance vs. iterating over large result arra

**URL:** <https://www.ruby-forum.com/t/dbase-query-performance-vs-iterating-over-large-result-arra/110291>\
**Category:** Rails\
**Created:** [July 24, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/dbase-query-performance-vs-iterating-over-large-result-arra/110291 "2007-07-24T23:33:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 24, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/dbase-query-performance-vs-iterating-over-large-result-arra/110291/1 "2007-07-24T23:33:00Z")

</div>

Hello Folks. Please excuse my beginner’s ignorance …

My question regards database query performance vs. iterating over  
nested results in an array. In the example below, a Project has many  
Tasks. Tasks have a due date. When I display Projects, I would like to  
also display the cumulative delay for all open Project Tasks based on  
their due date, and today’s date.

Something like …

Project | Delay  
XYZ | 5 days (4 tasks late, 1.25 day average delay)  
… 100 records

As I learn RoR, I’ve discovered a couple ways I can do this. I can  
perform the calculations in a single query. I can use the :include  
option to build an array with nested children, and iterate over each  
Project’s Tasks with something like parent.child.each.do. Or, finally,  
I can issue 100 queries to the database, once for each record. Each  
has advantages, but which is the most commonly accepted method in the  
RoR community?

Thanks in advance

John

---

<div class="post-metadata">

**Author:** ![Astorian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ad7895/32.png) [@Astorian](https://www.ruby-forum.com/u/Astorian)\
**Post date:** [July 25, 2007, 9:28am UTC](https://www.ruby-forum.com/t/dbase-query-performance-vs-iterating-over-large-result-arra/110291/2 "2007-07-25T09:28:00Z")

</div>

Astorian wrote:

> Project | Delay  
> XYZ | 5 days (4 tasks late, 1.25 day average delay)  
> … 100 records
> 
> As I learn RoR, I’ve discovered a couple ways I can do this. I can  
> perform the calculations in a single query. I can use the :include  
> option to build an array with nested children, and iterate over each  
> Project’s Tasks with something like parent.child.each.do. Or, finally,  
> I can issue 100 queries to the database, once for each record. Each  
> has advantages, but which is the most commonly accepted method in the  
> RoR community?

Doing 100 queries to the database sounds like a bad idea. There’s no  
reason not to use :include in the example given. Try doing it both ways  
and see the difference in execution time for yourself.

–  
Cheers,

- Jacob A.
