Craig R Morton
asked on
speed up slow Perl code with SQL statements
This is the guts of the code that I have written, however, when displayed in Firefox it takes a while to display. This seems to be down to the SQL hits i think, is there a way to optimize it as best as possible? I tried a JOIN which works, but is there any other ways to help speed it up?
I feel the speed is slowing at the @filescursor query.
I'm using use strict;
@groupscursor = $db->query("select distinct(filename),groups. receiverid ,groups.fi lelimit,gr oups.theow ner,groups .active,re ceivers.na me,receive rs.extensi on from hols.groups left join hols.receivers on groups.receiverid = receivers.id order by theowner asc, receiverid asc");
if ($groupscursor[1] ne "0E0") {
while ($groupsreturned = $groupscursor[0]->fetchrow _hashref) {
$receiverid = $groupsreturned->{receiver id};
$filename = $groupsreturned->{filename };
$theowner = $groupsreturned->{theowner };
$receivername = $groupsreturned->{name};
$extension = $groupsreturned->{extensio n};
next if ($receivername eq $tmpreceiver and ($filename.".".$extension) eq $tmpfile);
@filescursor = $db->query("select count(id) as results,id,filename,comple ted,max(cr eated) as datecreated from picco.files where theowner = '$theowner' and filename = '$filename' group by filename");
if ($filescursor[1] ne "0E0") {
while ($filesreturned = $filescursor[0]->fetchrow_ hashref) {
$fileid = $filesreturned->{id};
$created = $filesreturned->{datecreat ed};
$completed = $filesreturned->{completed };
$total += $filesreturned->{results};
($offers) = @{($db->query("SELECT count(id) as offers FROM picco.offers WHERE fileid = '$fileid'"))[0]->fetchrow_ arrayref};
if ($theowner ne $tmpsite) {
push (@list, ["", qq|<b>| . $theowner . qq|</b>|, " ", " ", " ", " ", " ", " ", " "]);
$total += 1;
}
$tmpsite = $theowner;
$colour = "black";
if ($completed eq "N") {
$colour = "red";
}
push (@list, [
" ",
" ",
qq|<font color='$colour'>| . $receivername . qq|</font>|,
qq|<font color='$colour'>| . ($filename .".". $extension) . qq|</font>|,
qq|<font color='$colour'>| . $groupsreturned->{filelimi t} . qq|</font>|,
qq|<font color='$colour'>| . $groupsreturned->{active} . qq|</font>|,
qq|<font color='$colour'>| . $created . qq|</font>|,
qq|<font color='$colour'>| . $completed . qq|</font>|,
qq|<font color='$colour'>| . $offers . qq|</font>|
]);
$tmpfile = ($filename.".".$extension) ;
$tmpreceiver = $receivername;
}
}
@filescursor = undef;
}
}
@groupscursor = undef;
I feel the speed is slowing at the @filescursor query.
I'm using use strict;
@groupscursor = $db->query("select distinct(filename),groups.
if ($groupscursor[1] ne "0E0") {
while ($groupsreturned = $groupscursor[0]->fetchrow
$receiverid = $groupsreturned->{receiver
$filename = $groupsreturned->{filename
$theowner = $groupsreturned->{theowner
$receivername = $groupsreturned->{name};
$extension = $groupsreturned->{extensio
next if ($receivername eq $tmpreceiver and ($filename.".".$extension)
@filescursor = $db->query("select count(id) as results,id,filename,comple
if ($filescursor[1] ne "0E0") {
while ($filesreturned = $filescursor[0]->fetchrow_
$fileid = $filesreturned->{id};
$created = $filesreturned->{datecreat
$completed = $filesreturned->{completed
$total += $filesreturned->{results};
($offers) = @{($db->query("SELECT count(id) as offers FROM picco.offers WHERE fileid = '$fileid'"))[0]->fetchrow_
if ($theowner ne $tmpsite) {
push (@list, ["", qq|<b>| . $theowner . qq|</b>|, " ", " ", " ", " ", " ", " ", " "]);
$total += 1;
}
$tmpsite = $theowner;
$colour = "black";
if ($completed eq "N") {
$colour = "red";
}
push (@list, [
" ",
" ",
qq|<font color='$colour'>| . $receivername . qq|</font>|,
qq|<font color='$colour'>| . ($filename .".". $extension) . qq|</font>|,
qq|<font color='$colour'>| . $groupsreturned->{filelimi
qq|<font color='$colour'>| . $groupsreturned->{active} . qq|</font>|,
qq|<font color='$colour'>| . $created . qq|</font>|,
qq|<font color='$colour'>| . $completed . qq|</font>|,
qq|<font color='$colour'>| . $offers . qq|</font>|
]);
$tmpfile = ($filename.".".$extension)
$tmpreceiver = $receivername;
}
}
@filescursor = undef;
}
}
@groupscursor = undef;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.