Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 c.name as ColumnName, t.name as ColumnType from syscolumns c, systypes t where object_name(c.id)=\"$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.


In reply to Optimization for Speed w HTML to Database processing by raflach

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-05-22 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?