[snip the problem code, comments eliminated for clarity]
if (mysql_num_rows($dbnpanxx) > 0){
   $npanxxcnt = 0;
      while($urownpanxx = mysql_fetch_object($dbnpanxx)){
                fputs($npanxxfile, $urownpanxx->o_npanxx);
                fputs($npanxxfile, $urownpanxx->t_npanxx);
                        for($spaces = 13; $spaces <= 66; $spaces++){
                                fputs($npanxxfile, " ");
                        }
                fputs($npanxxfile, "N");
                        for($spaces = 68; $spaces <= 86; $spaces++){
                                fputs($npanxxfile, " ");
                        }
                fputs($npanxxfile, "\n");
                $npanxxcnt++;
        }
mysql_free_result($dbnpanxx);
}
[/snip the problem code]

Please excuse the cross-post again, but this is still dealing with how
PHP and MySQL together handle memory allocation. The results being
placed into the output file are from a large MySQL database, returning a
large dataset for each output file.

The answer ends up being sort of a two parter, and there are still some
emalloc() errors, but I am working to correct them.

1. - MySQL does not automatically release the memory when use of a
dataset is completed. If you are looping through datasets (as I have
been based on the change of an array variable). Once you complete a loop
and the dataset is going to change go ahead and clear the results (and
subsequent memeory use) by using mysql_free_result().

2. Tis apparently better to place a single line of output in one fputs
statement as fputs is very resource intensive. The code above, written
by one of our new programmers who has about 2 weeks of PHP experience
looks good on the surface. It is organized, logical and precise. It also
calls fputs approximately 84 times per line of output when you take into
account the for loops. It is better to either create variables to hold
calced spaces or to literally call the fputs like;

fputs($npanxxfile, $urownpanxx->o_npanxx.$urownpanxx->t_npanxx."
number of spaces   N  spaces \n");

with all of the spaces in place.

Also, I attempted to use mysql_unbuffered_query which would have also
cut down on memory use, but it came back as an undefined function even
though I am running ver 4.1.1 (albeit from the command line, so I may
not have it configured properly or something.

I HTH others with mem allocation problems. If anyone can see anything
else that can be done to improve performace of this please let me know.
We are processing some very large datasets and anything we can do to
increase efficiency would be like pennies from heaven.

TIA!

Jay

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to