Howdy,

I am seeing some issues when trying to render a web page that contains a large table of data. I chatted a bit yesterday evening about this in the chat box and did some checking to see where the most time is spent.

It appears the time hog is occuring when processing my foreach loop. I have simplified my code and it is below.

In summary, I have three pieces of SQL I run (see Lori713's scratchpad), and the total rows returned for all three is approx. 75,000. On smaller project numbers (say 500 rows), once I get through my foreach loop, the three datasets are nicely smooshed together to form one row for each project number and works beautifully if a bit more slowly than I would like:

project ID        Bud amount       Act amount        Enc amount

I noticed that it is taking a very long time to process each row. Could this be because of the sort in the foreach? Does putting
 foreach $key ( sort keys %ctrl_tbl )
cause it to sort each time through the control table, or is the cost for the sort observed only the first time through?
#!/usr/local/bin/perl5_8 use strict; use ncw_com_library; # contains common subs (commify, timeo +ut, etc.) use HTML::Template; use Time::Local; use DBI; use CGI ':standard'; my $CGI = CGI->new; # Clear buffers and set up web page (required) $|=1; open STDERR, ">&STDOUT"; # SNIP there are ~25,000 unique keys in my control table and I have +snipped # out the SQL and database calls that generate my control table datase +t for # those since they are returning results in a timely fashion based on # timestamps printed while fetching and executing SQL. my ($key, %col_cbud, %col_encu, %col_fytd, %col_proj, %ctrl_tbl, @loop +_data); foreach $key ( sort keys %ctrl_tbl ) { my %row_data; # Gimme a timestamp so I can see how long each row takes my ( $sec1,$min1,$hour1,$mday1,$mon1,$year1,$wday1,$yday1,$isdst1 ) = +localtime; print "during foreach loop hour min sec $hour1 $min1 $sec1 xxx key is +$key <br>"; $row_data{col_proj} = $key; $row_data{col_cbud} = $col_cbud{$key}; $row_data{col_fytd} = $col_fytd{$key}; $row_data{col_encu} = $col_encu{$key}; # Add each hash row to loop for template push(@loop_data, \%row_data); } # Pass parameters and variable values from @loop arrays to template; p +rint report $template->param( passdata => \@loop_data, ); print $template->output();
So far, as I try to run the report, my timestamp prints look like this (the first 100 rows):
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbefore foreach loop hour min sec +11 13 44 during foreach loop hour min sec 11 13 45 xxx key is 649001 during foreach loop hour min sec 11 14 43 xxx key is 649001-60426-F SNIP during foreach loop hour min sec 12 3 44 xxx key is 660062-05279 during foreach loop hour min sec 12 4 19 xxx key is 660062-15279
We are beginning to see more and more issues with pages timing out as our co-workers ask for more sophisticated programs (I work at a public university). I think we need to start looking into alternative ways to address this issue and come up with a coding method to incorporate into the applications we're writing.

I would appreciate any suggestions or ideas as to what might be causing such a large block of time being spent on each row, and for any ideas about future coding that can take into account rendering large data tables on the browser.

Thanks!

In reply to Foreach loop takes a long time to process and my report times out before page is rendered by Lori713

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.