[Dbix-class] Re: Best possible way to retrieve data using relationship

Peter Rabbitson rabbit+dbic at rabbit.us
Fri Dec 31 14:17:59 GMT 2010


linuxsupport wrote:
> Thanks for the reply.
> 
> I have something like
> 
> my $rs = $schema->resultset('Article')->search(
> {
>    "me.id <http://me.id>" => 1
> },
> {
>     join => [
>                {arti_author => "author"},
>                {arti_comnt => "comments"},
>               ],
>      select => [
>                  qw/ me.create_date author.author_name comments.comment 
> comments.comment_date comments.name <http://comments.name> /
>                   ],
>        as => [
>                   qw/ created author comment comment_date commented_by /
>                  ]
> }
> );
>          
> How can I use prefetch to simplify it?
> 

You can't - prefetch is something that you use when you need complete
related (but distinct) result objects fetched at once. What you are doing
instead is abandoning the strict separation and mixing data from several
sources into one data object (which kinda defeats part of the purpose of
DBIC). So no - you can not use prefetch here, as it has a different
design endgoal.

My advice is to stop thinking in SQL (I have N tables from which I retrieve
M columns) and instead think in interrelated row objects. When you do in
fact run across a bottleneck - we'll be happy to help you optimize a
particular query, but doing what you show above as a general rule is largely
missing the point of DBIC.

Hope this helps, cheers!



More information about the DBIx-Class mailing list