dbase query performance vs. iterating over large result array (:include)

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

Astorian wrote:

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?

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.