On Mar 29, 2007, at 3:23 AM, James Masters wrote:
>
Thanks for your very helpful reply, Jonathan. It answered my general
>
question but I'm left with a couple of more specific ones.
>
>
Pl. see in line.
>
>
> I do that with ~20 tables that hold data I consider 'constants'. If
>
> this data is unchanging, there's no need to continually hit the db.
>
> ( note, i do this in a persistent environment , and i use that data
>
> nonstop. i think its easier putting config/constant data in a db
>
> than an xml / hash )
>
>
Yes, that's it. It's for data that is constant, for the purposes of
>
the
>
script in question. But I am v. interested to know how you do
>
this. Do you
>
do a Rose call and then iterate through all the objects creating a
>
hash?
>
I've now written something to do this.
Pretty much, yes.
Some of it is done via rose + a loop, but much is done via generic
sql queries too.
my applications are only ~50% rose
fwiw, i also do this: ( completely not rose related )
for (k,v) in objects:
hash{'id'}{ $key } = $value
hash{'name'}= reverse $MyApp::Config::fieldname{'id'}
( which works, because all the related tables have name as unique )
I develop under mod_perl -- so on startup i connect to the db, pull
all the 'static' info , and toss that into a series of hashes in a
MyApp::Config namespace. That info is then available to all the
other modules after apache forks.
I don't keep complex data in there - 90% of it is just key/value
pairs: states, dropdown select options, etc. maybe 50k worth of
stuff ( in terms of apache bloat ) -- but its all stuff that never
changes and there'es a 80% chance that a mod_perl child would have
requested it several times by termination , so i trade memory for
fewer db calls.
if you're under mod_perl, it might make sense to play around with the
cache version of rose objects.
rose is *really nice* under mod perl. it sucks a ton of memory for
all the pregeneration of sql , but if flys soooo fast once its running.
John addressed the rest.
// Jonathan Vanasco
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| SyndiClick.com
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Thread at a glance:
Previous Message by Date:
click to view message preview
Re: Best way to do multiple calls to database
On 3/29/07 6:27 AM, James Masters wrote:
> So now $products is an array of all product information, just
> like %product was a hash of all product info. But how do I
> elegantly get that stock quantity info for a particular location
> out of the $products array? I need something like:
>
> foreach $prod (@$products) {
> $prod->locations->$location->stockqty
> or
> $prod->locations(locationcode => $location)->stockqty
> }
>
> Is there a oneliner solution? Or do I just have to do another call to the DB
> or simply iterate through locations?
You need to loop, because objects related through a one-to-many relationship
are stored as (and are, in fact) a list, not a keyed collection of unique
entities. If you really want to do it in a single line, you can create a
convenience method to encapsulate the loop:
My::DB::Product;
...
use Carp;
...
sub location_by_code
{
my($self, $code) = @_;
my @locs = grep { $_->locationcode eq $code } $self->locations;
croak "Found more than one location with code '$code'" if(@locs > 1);
return $locs[0];
}
and then:
$qty = $product->location_by_code($loc_code)->stockqty;
-John
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Next Message by Date:
click to view message preview
Re: Best way to do multiple calls to database
> > do a Rose call and then iterate through all the objects creating a
> > hash? I've now written something to do this.
>
> Pretty much, yes.
> Some of it is done via rose + a loop, but much is done via generic
> sql queries too.
> my applications are only ~50% rose
> fwiw, i also do this: ( completely not rose related )
>
> for (k,v) in objects:
> hash{'id'}{ $key } = $value
> hash{'name'}= reverse $MyApp::Config::fieldname{'id'}
>
> ( which works, because all the related tables have name
> as unique )
Hmmm.. I see. Intriguing, thanks.
> I develop under mod_perl -- so on startup i connect to the db, pull
> all the 'static' info , and toss that into a series of hashes in a
> MyApp::Config namespace. That info is then available to all the
> other modules after apache forks.
> I don't keep complex data in there - 90% of it is just key/value
> pairs: states, dropdown select options, etc. maybe 50k worth of
> stuff ( in terms of apache bloat ) -- but its all stuff that never
> changes and there'es a 80% chance that a mod_perl child would have
> requested it several times by termination , so i trade memory for
> fewer db calls.
So when you say "startup", does this mean upon Apache startup or upon
your script startup? I can see that if it's in Apache's memory before
a script even runs, that would be hugely performance enhancing.
> if you're under mod_perl, it might make sense to play around
> with the
> cache version of rose objects.
Regretfully not. Although I use Apache, PERL & MySQL, it's all on
Windows; Maybe I just enjoy pain. Mod Perl sounds very tempting
but doesn't sound like it works too well with win32; Had enough
trouble getting Rose working on win32 - spent days on it!
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Previous Message by Thread:
click to view message preview
Re: Best way to do multiple calls to database
On 3/29/07 6:27 AM, James Masters wrote:
> So now $products is an array of all product information, just
> like %product was a hash of all product info. But how do I
> elegantly get that stock quantity info for a particular location
> out of the $products array? I need something like:
>
> foreach $prod (@$products) {
> $prod->locations->$location->stockqty
> or
> $prod->locations(locationcode => $location)->stockqty
> }
>
> Is there a oneliner solution? Or do I just have to do another call to the DB
> or simply iterate through locations?
You need to loop, because objects related through a one-to-many relationship
are stored as (and are, in fact) a list, not a keyed collection of unique
entities. If you really want to do it in a single line, you can create a
convenience method to encapsulate the loop:
My::DB::Product;
...
use Carp;
...
sub location_by_code
{
my($self, $code) = @_;
my @locs = grep { $_->locationcode eq $code } $self->locations;
croak "Found more than one location with code '$code'" if(@locs > 1);
return $locs[0];
}
and then:
$qty = $product->location_by_code($loc_code)->stockqty;
-John
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Next Message by Thread:
click to view message preview
Re: Best way to do multiple calls to database
> > do a Rose call and then iterate through all the objects creating a
> > hash? I've now written something to do this.
>
> Pretty much, yes.
> Some of it is done via rose + a loop, but much is done via generic
> sql queries too.
> my applications are only ~50% rose
> fwiw, i also do this: ( completely not rose related )
>
> for (k,v) in objects:
> hash{'id'}{ $key } = $value
> hash{'name'}= reverse $MyApp::Config::fieldname{'id'}
>
> ( which works, because all the related tables have name
> as unique )
Hmmm.. I see. Intriguing, thanks.
> I develop under mod_perl -- so on startup i connect to the db, pull
> all the 'static' info , and toss that into a series of hashes in a
> MyApp::Config namespace. That info is then available to all the
> other modules after apache forks.
> I don't keep complex data in there - 90% of it is just key/value
> pairs: states, dropdown select options, etc. maybe 50k worth of
> stuff ( in terms of apache bloat ) -- but its all stuff that never
> changes and there'es a 80% chance that a mod_perl child would have
> requested it several times by termination , so i trade memory for
> fewer db calls.
So when you say "startup", does this mean upon Apache startup or upon
your script startup? I can see that if it's in Apache's memory before
a script even runs, that would be hugely performance enhancing.
> if you're under mod_perl, it might make sense to play around
> with the
> cache version of rose objects.
Regretfully not. Although I use Apache, PERL & MySQL, it's all on
Windows; Maybe I just enjoy pain. Mod Perl sounds very tempting
but doesn't sound like it works too well with win32; Had enough
trouble getting Rose working on win32 - spent days on it!
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV