[Dbix-class] DBIC hits DB thoguh using prefetch (repost)

Jess Robinson castaway at desert-island.me.uk
Sun Jul 27 09:18:41 BST 2008


On Sun, 27 Jul 2008, Moritz Onken wrote:

>>> 
>> 
>> Hi Moritz,
>> 
>> Nope, you're misunderstanding how it works.
>> 
>> Search with prefetch fetches data and stores it in the resultset when 
>> next/all are called on the resultset.
>> 
>> However find() doesnt use prefetched data at all, there is no caching for 
>> find.
>> 
>> $rs->first, doesnt run any extra queries. Since that's now not part of the 
>> resultset any more, any rels called on the result of first() also produce 
>> more queries.
>> 
>> In short, it only works on chained resultset objects.
>> 
>> Jess
>> 
>
> Thanks, that makes sense. But I recognized that the test case didn't exactly 
> show
> where my problem is. I edited the test:
>
> my @rs =
> $schema->resultset("Complex")
> ->search( undef, { prefetch => { parents => "parent" } } );
> for (@rs) {
> 	is( defined $_->parents->first->parent->id, 1 );
> }
>

As I attempted to explain (but likely failed). It only works across 
chained resultsets..

When you call ->first, you produce a row object, not a resultset. That row 
object is now standalone, not attached to the resultset that did the 
search/caching.. Therefore calling any related methods on it (eg "parent") 
will produce a new query.

Jess




More information about the DBIx-Class mailing list