Re: Simple MySQL sample code runs out of memory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Oct 27, 2011 at 9:36 PM,  <php@xxxxxxxxxxxxx> wrote:
> On Thu, Oct 27, 2011 at 09:17:21PM -0500, tamouse mailing lists wrote:
>>
>> That said, I think there must be a way to do this in SQL.
>
> Absolutely, there's a way to do this in SQL;
>
> "select contract, sum(1) from test_table group by contract"
>
>
> But as I said, simply counting is not the intent of the actual
> processing that will be done.
>
> I need to find a method that can iterate through a large number
> of records and process each record (in whatever way I choose to
> define) without running out of RAM.  I only need to access one
> record at a time, so I thought this would be pretty trivial,
> and was surprised to find that the routine ran out of memory.
>
> This simple counting exercise is merely a test of the loop
> algorithm so that I can do a 'cmp' or 'diff' comparison of the
> output of the PHP versus the output of the SQL to verify that in
> fact the PHP algorithm is indeed finding and processing each
> record (and thus arriving at the same per-contract counts that
> the SQL statement does).
>
> I will experiment with your suggestions.
>
> Thank you,
>
> Jim
>
>

Ah, okay, did not get that -- sorry.

Still, an inner and outer loop don't really seem necessary here, as
you're spinning through one set of data, there doesn't seem a need to
run through it that way. A more traditional method is to do something
like:

$last_contract = '';
while ($row = $msql_fetch_assoc($results) {
    if ($last_contract == $row['contract]) {
        // do what ever you'd do with the same contract
    } else {
       $last_contract = $row['contract'];
       // do what ever you'd do with the new contract
    }
}

assuming you are ordering by contract as in the example select.

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux