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

Jess Robinson castaway at desert-island.me.uk
Sun Jul 27 00:46:25 BST 2008



On Sun, 20 Jul 2008, Moritz Onken wrote:

> Am 20.07.2008 um 02:53 schrieb Matt S Trout:
>
> I've put together a test case. I tried trunk (0.08) but had no luck.
> Unpack the attachment and run "perl complex01.t".
>
> If you have DBIC_TRACE enabled you'll see something like this:
>
> [lots of inserts and creates and sql stuff]
> SELECT me.title, me.id, parents.child, parents.parent, parent.title, 
> parent.id FROM complex me LEFT JOIN complex_map parents ON ( parents.child = 
> me.id ) LEFT JOIN complex parent ON ( parent.id = parents.parent ) WHERE ( ( 
> me.id = ? ) ) ORDER BY parents.child: '3'
> # which is fine because it prefetches all data it should need
> SELECT me.child, me.parent FROM complex_map me WHERE ( me.child = ? ): '3'
> # this data is already loaded!
> SELECT me.title, me.id FROM complex me WHERE ( ( ( me.id = ? ) ) ): '1'
> # this one too
> ok 1

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




More information about the DBIx-Class mailing list