Re: exploiting the object index
[prev]
[thread]
[next]
[Date index for 2005/05/31]
On 5/31/05, Hartmaier Alexander <Alexander.Hartmaier@xxxxxxxxx.xx> wrote:
> I want to retrieve all objects of a class to avoid lots of
> single-row-returning sql queries against the db.=20
>=20
> e.g.:=20
>=20
> my @cds =3D Music::CD->retrieve_all;=20
>=20
> Then I want to lookup the object by a non-primary-key field which is also
> unique.=20
>=20
> e.g.:=20
>=20
> my $obj =3D Music::CD->search($name);=20
Are you sure you want to do that? Are you certain it will be faster
and worth the effort? You seem to be trying to ignore the whole
purpose of having a database in the first place by using perl to do
your searching. This is exactly what databases are good at.
Now if you really do want to do it, there are a few ways you can go
about it, the easiest would be to just use grep:
my @cds =3D Music::CD->retrieve_all;=20
my @subset =3D grep { $_->name eq $name } @cds;
Make sure that 'name' is in marked as an 'Essential' column in your
Class::DBI module, otherwise it will still do multiple calls to the
DB.
If you want to do this automatically behind the scenes in Class::DBI,
then you have a bit of work cut out for you :)
Cheers,
Cees
|
|
Re: exploiting the object index
Cees Hek 15:36 on 31 May 2005
|