[Dbix-class] Prefetch question

Peter Rabbitson rabbit+dbic at rabbit.us
Wed Dec 16 19:47:24 GMT 2009

Bill Moseley wrote:
> On Wed, Dec 16, 2009 at 8:43 AM, Bernhard Graf <dbic4 at augensalat.de
> <mailto:dbic4 at augensalat.de>> wrote:
>     Prefetching is a convenient thing, but may also need a lot of resources.
>     E.g. I have a users table, that belongs_to("country"). Table 'country'
>     has more information (e.g. full name, int. phone code, time zones, etc).
>     The users table may have zillions of rows, whereas the the country table
>     is quite small (and nearly all users belong to a handful of countries).
>     With prefetching I get the same country data over and over again.
>     Without prefetching DBIC delays country fetches until I access
>     $user->country, but then also fetches the same country data all the
>     time.
>     Is it possible not to prefetch country, but tell DBIC to cache delayed
>     fetches of related data, so that each related country row is really only
>     fetched once for this user resultset?
> Isn't this what Class::DBI attempted to do with it's object index?  That
> is, if it needed to fetch $user->county but that $country had already
> been fetched then it used the in-memory copy.  I always disabled that
> feature as it seems to cause more breakage than not.
> I assume you are fetching a page of users to display?   Say 20 rows? I'd
> be interested to see a benchmark between doing it in one query (with
> prefetch) and then one query and then twenty additional queries to fetch
> each country.

Moreover paged prefetch now works correctly.

More information about the DBIx-Class mailing list