I'm using perl, MySQL and perlmagic to do some work on images from a MySQL database and would like some feedback as to the most efficient way to go about something, I'll do my best to explain. I don't believe this qestion matters much what language or tools I'm using, just the fact that it is a matter of doing a duplicate database query or not.
I have multiple images stored in a database, let's say I need to display 10 of them depending on some variable but some of those images may be duplicates of ones already read in. Should I save copies of the images in memory and check before doing another query to the database, and if I already have the image do not do the query and use that copy, or just go ahead and read it in with another query.
Code looks like this to do as I explain above and check for a duplicate:
# This outer loop I simplified for display here
for ($number = 0; $number < 10; $number++)
{
#############
#loop looking for an image already fetched from the
#database, and if so use it
#############
$match = 0;
for($y = 0; $y < @place; $y++)
{
if ($number eq $place[$y])
{
$image->BlobToImage($holdimages[$y]);
$match = 1;
}
}
###############
if not fetched yet, then go ahead with another query
###############
if (!($match))
{
$sth = $dbh->prepare(qq{select * from table_name where code = '$number'});
$sth->execute();
@data = $sth->fetchrow_array();
$sth->finish();
# $data[1] is the image from the database
$image->BlobToImage($data[1]);
# Saving the image
push (@holdimages, $data[1]);
# Saving it's place in the list and $number
push (@place, $number);
}
}
Any suggestions as to code changes for improved efficiency greatly appreciated or logic changes.
Thx
I have multiple images stored in a database, let's say I need to display 10 of them depending on some variable but some of those images may be duplicates of ones already read in. Should I save copies of the images in memory and check before doing another query to the database, and if I already have the image do not do the query and use that copy, or just go ahead and read it in with another query.
Code looks like this to do as I explain above and check for a duplicate:
# This outer loop I simplified for display here
for ($number = 0; $number < 10; $number++)
{
#############
#loop looking for an image already fetched from the
#database, and if so use it
#############
$match = 0;
for($y = 0; $y < @place; $y++)
{
if ($number eq $place[$y])
{
$image->BlobToImage($holdimages[$y]);
$match = 1;
}
}
###############
if not fetched yet, then go ahead with another query
###############
if (!($match))
{
$sth = $dbh->prepare(qq{select * from table_name where code = '$number'});
$sth->execute();
@data = $sth->fetchrow_array();
$sth->finish();
# $data[1] is the image from the database
$image->BlobToImage($data[1]);
# Saving the image
push (@holdimages, $data[1]);
# Saving it's place in the list and $number
push (@place, $number);
}
}
Any suggestions as to code changes for improved efficiency greatly appreciated or logic changes.
Thx