[CDBI] quantity of DB queries

Aaron Trevena aaron.trevena at gmail.com
Fri Jul 28 11:29:40 BST 2006

On 28/07/06, Oliver Jeeves <oj at defuturo.co.uk> wrote:
> I'm concerned about the number of queries Class::DBI would need to make
> to a database. My understanding is that if I were to do something like this:
> my $obj = My::DB::Thing->retrieve(1);
> my $some_value = $obj->fk->value;
> Then CDBI would be doing something like the following:
> Look up primary key values for row in Thing table
> Look up fk value in Thing table for previously fetched primary keys
> Look up primary key values for row in whatever table fk represents
> Look up value in the fk table
> Is that correct?

Depends on how you've set up your relationships and columns.

I believe that normally, with a sane schema and relationships when you
retrieve you will get the PK column, any FK values specified for
relationships and anything you specified in the 'essential' column

So that should be 1 query for the 2 values you've specified.

> The trouble is, for a web application, I generally want to get something
> from the database, and then just work with those values without having
> to go back to the DB unless I need to look up a foreign key in another
> table. Does CDBI allow me to do this?
> To clarify, in the above example, I would expect the following DB queries:
> Look up all values for row in thing table.
> Look up all values for row in table referenced by fk.

Look at the documentation for essential columns, possibly look at
Class::DBI::Sweet to handle optimised joins and queries for your

Hope that helps


More information about the ClassDBI mailing list