[Dbix-class] Chaining searches with joins on the same table

Rob Kinyon rob.kinyon at gmail.com
Fri Jul 10 15:50:09 GMT 2009


On Fri, Jul 10, 2009 at 11:39, A Murka<amurkat at gmail.com> wrote:
> On Fri, Jul 10, 2009 at 9:59 AM, Rob Kinyon<rob.kinyon at gmail.com> wrote:
>> On Fri, Jul 10, 2009 at 00:08, A Murka<amurkat at gmail.com> wrote:
>> [snip]
>>> Do I just have to create a method that does both joins at once? Is a
>>> better way to implement these aggergates?
>>
>> There is such a thing as putting too much into one query. These
>> queries do very different things. So, just do two queries. Simplify
>> your life.
>
> The trouble with that is that it would mean making an extra query for
> each image as it's displayed: one to select all the images, then one
> for each to select the user's rating for it. On a thumbnail page with
> many images, this would mean many extra queries.

Two queries. One to get all the images, then one to get all the user
ratings for each image. Use the new subquery interface.

my $rs_all_images = resultset('Image')->get_all_images;
my $rs_user_ratings = resultset('UserRatings')->search({
    user_id => $user->id,
    image_id => [ $rs_all_images->get_column('image_id')->as_query ],
})

Rob



More information about the DBIx-Class mailing list