[Dbix-class] Prefetch question

Bernhard Graf dbic4 at augensalat.de
Wed Dec 16 19:59:45 GMT 2009


Bill Moseley schrieb:

> 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.

Hm, no. Well at least not here (DBIx::Class 0.08115).

With DBIC_TRACE=1 it shows me, that for every user the related country
data is fetched, even if it has been fetched for that user resultset before.

My test code looks like this:

  my $rs = $schema->resultset('User');

  while (my $row = $rs->next) {
    printf "email: %s, country: %s\n",
      $row->email, $row->country->shortname;
  }


If you say, you disable this caching, then maybe you can tell me how to
/enable/ it.

> 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.

There is Benchmark.pm. ;^)
But I assume, that one fetch is faster than 21...
Of course things would improve, when fetching country data only once per
country id - in my case it is nearly always 'DE' and sometimes 'AT' or 'CH'.

Bernhard Graf



More information about the DBIx-Class mailing list