Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2007, 06:27 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default 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 way?

I'm using
Code:
use strict;
Code:
@groupscursor = $db->query("select distinct(filename),groups.receiverid,groups.filelimit,groups.theowner,groups.active,receivers.name,receivers.extension 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->{receiverid};
        $filename        = $groupsreturned->{filename};
        $theowner        = $groupsreturned->{theowner};
        $receivername    = $groupsreturned->{name};
        $extension        = $groupsreturned->{extension};    

        next if ($receivername eq $tmpreceiver and ($filename.".".$extension) eq $tmpfile);


        @filescursor = $db->query("select count(id) as results,id,filename,completed,max(created) 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->{datecreated};
                $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>|, "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;", "&nbsp;"]);
                    $total += 1;
                }
                $tmpsite = $theowner;

                $colour = "black";

                if ($completed eq "N") {
                    $colour = "red";
                }                

                push (@list,    [
                                    "&nbsp;",                
                                    "&nbsp;", 
                                    qq|| . $receivername . qq||, 
                                    qq|| . ($filename .".". $extension) . qq||, 
                                    qq|| . $groupsreturned->{filelimit} . qq||, 
                                    qq|| . $groupsreturned->{active} . qq||, 
                                    qq|| . $created . qq||,
                                    qq|| . $completed . qq||,
                                    qq|| . $offers . qq||
                                ]);
                $tmpfile = ($filename.".".$extension);
                $tmpreceiver = $receivername;
            }
        }
        @filescursor = undef;
    }
}
@groupscursor = undef;
www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old April 3rd, 2007, 05:52 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I'd put the picco.tables and hols.tables into the same database.
Then you'll be able to get rid of all those nested loops and do a
four way join between picco.files, picco.offers, hols.groups and
hols.receivers.

Something like:
Code:
  SELECT count(offers.id) AS offers, ...etc
    FROM groups, 
         receivers, 
         offers,
         files
   WHERE groups.receiverid = receivers.id
     AND offers.fileid     = files.id
     AND files.filename    = groups.filename ...etc
ORDER BY groups.theowner, groups.receiverid
GROUP BY files.filename
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old May 9th, 2007, 08:00 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice clean sql code (coming from a DBA).

Liked your site, particularly the windows stuff....

Pics of your g-friend says she is way too hot for you!!!

J/K on the last one......

 
Old May 11th, 2007, 05:21 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

*hehe* Thanks robprell ;)

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed up code - looping and copy / paste vba_user Excel VBA 6 March 23rd, 2011 05:27 PM
killall -9 apache-perl slow server at the moment crmpicco Linux 1 May 11th, 2007 05:23 AM
optimise it or speed up execution time perl script crmpicco Perl 2 November 22nd, 2006 08:31 AM
Slow Speed In Query Amanjain75 SQL Server 2000 1 December 12th, 2005 10:47 AM
How to speed up looping ADO code? llowwelll Pro VB Databases 7 October 24th, 2004 11:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.