MashMashy has asked for the wisdom of the Perl Monks concerning the following question:

I have an extremely simple function that does the following - loads DBI, requires a common functions block, and then selects one row out of a tiny (6 record) MySQL database and prints it. the database has indexes, and what I mentioned is literally ALL that it does. I still get upwards of 6% CPU load from it, on a robust Apache server that severs a million html pages a month. yikes!

further details with associated questions:
- the common functions block (require "common_functions.pl") has 113 subroutines in it, only one of which i use. could loading all those subroutines be the problem? would it be better to split those up, and only call the ones I need - and if so, would (for example) five 'requires' be more ponderous than one require that has 5 subroutines in it?
- is use DBI; really that much of a memory hog? All i do is Selects (regular and 'count(*)'s) , Updates, Inserts, and Deletes - is there something small I could be loading?

Thank you so much for any help - I tried to type this as concisely as possible.

--- actual code:

#!/usr/bin/perl require "common_function.pl"; use DBI; use Digest::MD5 qw(md5 md5_hex md5_base64); ############# this is here to check their authentication - that line + removed from this code, and the load still happens without it here print "Content-type: text/html \n\n"; $dbh = DBI->connect('DBI:mysql:name-of-db', 'NOT COPIED INTO HERE', ' +NOT COPIED INTO HERE'); ######### this is the subroutine called from that common block, copied + into here for clarity ########## $query = 'SELECT flag_value FROM server_flags WHERE flag_name = ?' +; $sthcf2 = $dbh->prepare($query); $result=$sthcf2->execute("announcements_today"); ($currentnews) = $sthcf2->fetchrow(); $sthcf2->finish(); ################## if ($currentnews ne ""){ $currentnews=<<_MOOSE_; <table style="border-color:black;border-width:1px;border-style:solid;f +ont-size:14px" bgcolor=FFFFFF><tr><td align=center width=100> $currentnews </td></tr></table> _MOOSE_ $dbh->disconnect(); } print $currentnews; $dbh->disconnect();

--------

Replies are listed 'Best First'.
Re: huge CPU load on simple DBI function - reason?
by shmem (Chancellor) on Feb 01, 2009 at 17:34 UTC
    could loading all those subroutines be the problem? would it be better to split those up, and only call the ones I need - and if so, would (for example) five 'requires' be more ponderous than one require that has 5 subroutines in it?

    Yes, that could be the problem, and yes, that could be a solution. Make a module which manages all those functions (kept in separate files via AutoSplit) via AutoLoader. Have a look at POSIX which just uses that mechanism, so you can say

    use POSIX qw(strftime ceil execl);

    to import (which means load and compile) only those three functions into your program.

      excellent, thank you so much for your help!