[Dbix-class] Rapid Data Extraction

Ovid publiustemp-dbic at yahoo.com
Tue Jan 12 10:26:41 GMT 2010


Relevant facts:

1.  bbc.co.uk is the fifth most popular web site in the UK (http://www.alexa.com/siteinfo/bbc.co.uk)
2.  We use DBIx::Class extensively on our iPlayer backend.
3.  Performance is critical.

Code in our Web templates is not allowed to touch the database.  Thus, for each DBIx::Class result object, we extract the relevant data and put it into a simple object.  The relevant code looks like this:

  sub _init_from_real_thing {
    my $self = shift;

    # from _real_thing
    $self->$_( $self->_dbic->$_ ) for $self->attributes;

    return $self;
  }

Creating one of these objects takes approximately 50 milliseconds. Each of the individual channels or episodes you see on http://www.bbc.co.uk/iplayer/ represents one of these blocks.  If we create 50 of these objects, that's 2.5 seconds -- way too long.

As a performance hack, I'd like to experiment with something like this:

  sub _init_from_real_thing {
    my $self = shift;

    my @attributes = $self->attributes;
    @{$self}{@attributes} = @{$self->_dbic->{_column_data}}{@attributes};

    return $self;
  }

Using the slices is a horrible encapsulation violation.  I can deal with that on our side, but not on the dbic side.  Is there a clean way of fetching all of that at once?

If you're curious, here's my benchmark:

             Rate methods   slice
  methods 26954/s      --    -71%
  slice   94340/s    250%      --

Using the slice is almost four times faster than the "clean" strategy.

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6




More information about the DBIx-Class mailing list