datamapper / dm-aggregates

DataMapper plugin providing support for aggregates on collections
http://datamapper.org/
MIT License
16 stars 15 forks source link

SEL for count? #11

Open solnic opened 13 years ago

solnic commented 13 years ago

Associations seem to be eager-loading when all that is required is a count. This can take awhile when the count of rows is even in the 10k+ area, if not more.

http://gist.github.com/156762


Created by Tony Pitale - 2009-07-27 22:00:09 UTC

Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/992

solnic commented 13 years ago

First stab at a dm-aggregates failing spec patch. Not sure it can be made to pass from within dm-aggregates.

by Tony Pitale

solnic commented 13 years ago

The problem may be with DataMapper::Collection#delegate_to_relationship which does not create a lazyarray or something like it. At that point, it fully builds the collection from the relationship before anything (e.g., count) is ever done to it. It would seem that the only time we would actually want to load something when we want to do something with it.

by Tony Pitale

solnic commented 13 years ago

This is probably not news to anyone but me, however, SEL is used regardless of whether or not the association in a loop is ever used for anything. That’s obviously an affect of the association not being a lazyarray when called.

by Tony Pitale

solnic commented 13 years ago

Here is a potential change to dm-core that fixes my original gist. It changes the spec and Collection to lazy load for #delegate_to_relationship. This ends up breaking the many_to_many spec in a way I have yet to understand, but I will post up anyway.

by Tony Pitale