http://www.perlmonks.org?node_id=20217


in reply to Optimization for Speed w HTML to Database processing

This is just a guess, as I am not sure what bpoStub is. I looked for on CPAN and other places and couldn't find a reference to it, so I am assuming that it's an in-house module.

When I use the DBI module, executing SQL takes two or three steps: creating the SQL, preparing it (I can combine the "create" and "prepare" into one step) and executing it. It looks like you are combining the "prepare" and "execute" steps with bpoStub:

my $D = new bpoStub; . . . $sql = "select DBCompNum from FieldMap where DBTable=\"$DBTable\" and FormID = $FormID group by DBCompNum"; @rowscomp = $D->select($sql); for $countc ( 1 .. $#rowscomp ) { . . . }
If that is the case, I suspect that part of your problem is how you are doing the sql. Consider the following:
use DBI; . . . my $sql = 'INSERT INTO ' . $table . '(title, description, filename, pa +th) VALUES (?, ?, ?)'; my $sth = $dbh->prepare($sql); while ($some_condition) { $sth->execute( $title, $description, $filename, $storagePath ); # do stuff which modifies condition and perhaps # the insertion values }
The question marks (VALUES (?, ?, ?)) in the SQL are placeholders. When it is "prepared", DBI prepares the sql and then inserts whatever values you pass to it with $sth->execute. Because the "prepare" statement has a high overhead, you should never prepare SQL inside of a loop if you can avoid it. It looks like your SQL is static, except for the variables that you pass. If that is the case, prepare all of your SQL outside of the loops you have and execute inside of the loops.

On a side note, I also spotted this regex in a loop:

if ( $Type{$DBCol} =~ /numeric|float|int|money/ ) {...}
That regex will be recompiled every time it is encountered, also increasing your run time. Since it is also static, add the /o modifier to force the regex compiler to compile it only once during the run, thus saving time.

Further, I am guessing that $type{$DBCol} is not likely to have a space prior to the column type. Using ^ at the beginning of the regex will force it to match at the beginning. Otherwise, it will try every character after the first for a match, thus increasing run time.

if ( $Type{$DBCol} =~ /^numeric|float|int|money/o ) {...}
Hope this helps! Cheers.

Replies are listed 'Best First'.
RE: Re: Optimization for Speed w HTML to Database processing
by raflach (Pilgrim) on Jun 28, 2000 at 22:35 UTC

    Thanks for the point about the regex. Done

    bpoStub is an inhouse module, and it uses the Sybase::DBlib module to do it's processing of database info.

    I could break out of it and do my own as it is a very simple module or rewrite it to take advantage of seperate prep and executes(preferred) if I was sure of what I was doing, but I can't find much documentation of the Sybase module. It appears that
    $sybdblibvariable->dbcmd("sql here");
    is used to prep, and then
    $sybdblibvariable->dbsqlexec;
    is used for execution.

    Can anyone tell me if the ? ? ? placeholders will work in a dbcmd/dbexecute pair in Sybase::DBLib as opposed to a DBI prepare/execute pair?

    Finally, the only loops that the database insert/update commands are inside of are the loops through the tables and through the compnumbers. It appears from your code here that the table cannot be specified as a ? var, which loses me that option for the tables, and the compnum is used as a part of a where statement when doing updates. Can you use ?'s there as well? Am I understanding this correctly?

      Not having used the Sybase::DBLib module, I can't say what it's capabilities are. I found a bit of information about here.

      You can use Sybase with DBD and therefore should be able to use it with DBI. That will allow you to use the syntax I have listed above. I have no idea how its performance will compare with Sybase::DBlib.

      My code was just an example. You should have no problem substituting the table with ? and then passing the table name in the $sth->execute statement.

      <RANT> Naturally, I tried to submit when PM went down. Why does it seem to mostly go down when I submit something? Don't get me wrong: I love this place, but I'll be very happy when it's ready for production (no slight intended towards vroom's fantastic job here)</RANT>