Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Optimization for Speed w HTML to Database processing

by raflach (Pilgrim)
on Jun 28, 2000 at 20:17 UTC ( #20199=perlquestion: print w/replies, xml ) Need Help??
raflach has asked for the wisdom of the Perl Monks concerning the following question:

I have an application that is taking data from a very large HTML form and processing it into a database.

It does this using one table of the database that maps HTML tags to tables and columns etc. The mapping table (called FieldMap) has the following columns:
DBTable (the table name)
DBCol (the column name)
PDFTag (the name of the element on the html form)
FormID (identifier for which form is being processed)
FType (info on the type of field used in generating the blank html form)
ListID (identifies the listid of another table which has listbox options for the form)
SectionText (headings to pretty up the html)
Seq (Order fields appear on the form)
DBCompNum (Used in certain tables to seperate out multiple pieces of information belonging to the same virtual record, but stored as seperate records in the database)

I have a pre build HTML document which has tags of the form (~ tagname ~) throughout it, and I sub in values for those tags when generating the form before posting the template

I have a function which does what I want and I have decreased the time it takes processing by half since my original draft. However, it still takes 5-10 seconds to load a blank form, and 15-20 seconds to process a form when everything is local. I need to reduce this time if at all possible, especially the form processing phase. I am enclosing the code for the function that Processes the form info. It is fairly large, so I'm not sure how well it will work. Please attack it and give me advice on optomizing my code.

Note: I am using a couple of pre-build modules to ease database handling and html generation procedures, and I am packaging the current project as well

# Package wide code, and package initialization stuff. ###################################################### #!/usr/local/bin/perl package ProcSupForm; use bpoStub; use CGI; use Pictures; use HTMLTemplate; use Release; my $C = new CGI; my $D = new bpoStub; my $P = new Pictures; my $T ; my $R = new Release; open LOG, ">>/home_dir/flachr/cgi.log"; sub new { my ($class) = @_; my $self = {}; bless $self, $class; $self->getparam; # This just grabs CGI params and stuffs them into +self $self->{ScriptOnTheFly} = ""; print LOG "FormID = $self->{FormID}\n"; return bless $self, $class; } # Function used to process Form and populate database. ###################################################### # Note: I have tried to keep lines short, but my res is 16X12, so I'm + not sure they're short enough for some. sub ProcSupForm { my ($self) = @_; my $FormID = $self->{FormID}; my $OrderID = $self->{OrderID}; my $sql; my @tables; my @rowsmap; my @rowsthere; my @rowscomp; my @rowstype; my $DBTable; my %Type; my $count1; my $count2; my $countt; my $countc; my %VarList; my $DBCompNum; my @columns; my @values; my $valuestext; my $columnstext; $sql = "select DBTable from FieldMap where FormID = $FormID and DBTable not in (\"Contact\",\"Company\",\"Orders\") group by DBTable"; @tables = $D->select($sql); for $countt ( 1 .. $#tables ) { $DBTable = $tables[$countt]->{DBTable}; %Type = (); $sql = "select DBCompNum from FieldMap where DBTable=\"$DBTable\" and FormID = $FormID group by DBCompNum"; @rowscomp = $D->select($sql); for $countc ( 1 .. $#rowscomp ) { @columns = ("OrderID"); @values = ($OrderID); $DBCompNum = $rowscomp[$countc]->{DBCompNum}; if ( $DBCompNum != 0 ) { push @columns , "CompNumber"; push @values , $DBCompNum; } $sql = "select * from FieldMap where DBTable=\"$DBTable\" and FormID = $FormID and DBCompNum = $DBCompNum"; @rowsmap = $D->select($sql); for $count2 ( 1 .. $#rowsmap ) { my $DBCol = $rowsmap[$count2]->{DBCol}; my $PDFTag = $rowsmap[$count2]->{PDFTag}; if ( $self->{$PDFTag} ) { if ( ! %Type ) { $sql = "select as ColumnName, as ColumnType from syscolumns c, systypes t where object_name(\"$DBTable\" and c.usertype=t.usertype"; @rowstype = $D->select($sql); for $count1 ( 1 .. $#rowstype) { $Type{$rowstype[$count1]->{'ColumnName'}} = $rowstype[$c +ount1]->{'ColumnType'}; } } push @columns, "$DBCol"; if ( $Type{$DBCol} =~ /numeric|float|int|money/ ) { # Process Digit print LOG "variable $PDFTag, value $self->{$PDFTag}, type +$Type{$DBCol}, This is numeric\n"; push @values, $self->{$PDFTag}; } else { # Process Text print LOG "variable $PDFTag, value $self->{$PDFTag}, type +$Type{$DBCol}, This is text"; push @values, "\"$self->{$PDFTag}\""; } } } if ( $DBCompNum != 0 ) { $sql = "select count(*) as numrows from $DBTable where OrderID=$OrderID and CompNumber = $DBCompNum"; } else { $sql = "select count(*) as numrows from $DBTable where OrderID=$OrderID"; } @rowsthere = $D->select($sql); if ( $rowsthere[1]{numrows} < 1 ) { #Do Insert if ($columns[$#columns] ne "OrderID" && $columns[$#columns] ne + "CompNumber") { $columnstext = join ",",@columns; $valuestext = join ",",@values; $sql = "insert into $DBTable($columnstext) values($valuestex +t)"; print LOG $sql. "\n"; $D->execute($sql); } } else { #Do Update my @update = (); for $count1 ( 0 .. $#columns ) { next if ($columns[$count1] eq 'OrderID' || $columns[$count1] + eq 'CompNumber'); push @update, join("",$columns[$count1],"=",$values[$count1] +); } my $where = ( $DBCompNum == 0 ) ? "" : " and CompNumber = $DBC +ompNum"; $sql = join "","update $DBTable set ",(join ', ', @update)," w +here OrderID = $OrderID",$where; print LOG $sql. "\n"; $D->execute($sql); } } } if ( $self->{Process} eq "Submit" ) { # Update the Orders table to reflect the completion of the form by + the Supplier # enabling the ability to view the form via PDF. (NOT IMPLEMENTE +D) print LOG "The order was completed and submited\n"; $self->{ScriptOnTheFly} = "alert('Form has been submitted');"; } else { # Don't do anything... We already saved it. print LOG "The order was saved for later completion...'$self->{Pro +cess}'\n"; $self->{ScriptOnTheFly} = "alert('Form has been saved. Do not forget that you must retu +rn and complete form later');"; } }

Any help rendered would be greatly appreciated! And if something is not easily understandable, let me know that too, as readability is important too, since I'm not the sole maintainer of this code.

Replies are listed 'Best First'.
Re: Optimization for Speed w HTML to Database processing
by Ovid (Cardinal) on Jun 28, 2000 at 21:48 UTC
    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.

      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
      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>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://20199]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-05-27 02:03 GMT
Find Nodes?
    Voting Booth?