Re: Close MySQL Result

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

 



Shawn McKenzie wrote:
> Daevid Vincent wrote:
>>> -----Original Message-----
>>> From: Nathan Rixham [mailto:nrixham@xxxxxxxxx] 
>>>
>>> you'll also find a performance upgrade if you load all sql 
>>> results in to
>>> an array and close up the query / free the results before 
>>> working on them.
>>>
>>> query
>>> for() {
>>>   $results[] = $row;
>>> }
>>> close stuff
>>> work on results
>> Do you have any proof of this? 
>> What would be the logic here? 
>> Got some example benchmarks?
>> Got a URL of a whitepaper or something that discusses this?
>>
>> I would think the only things that the mysql calls are doing is holding
>> pointers to the next record and DB handle. I can't imagine they are using
>> that much resources, nor would they be just spinning and waiting tying up
>> CPU cycles either.
>>
>>
>> I've never heard this to be the case.
>>
> 
> I have proof to the contrary :-)
> 
> <?php
> echo number_format(memory_get_usage()) . " Bytes used before query\n";
> 
> $conn = mysql_connect('localhost', 'x', 'x');
> $result = mysql_query("SELECT * FROM table_name");
> $count = mysql_num_rows($result);
> 
> echo $count . " rows returned\n";
> 
> echo number_format(memory_get_usage()) . " Bytes used after query\n";
> 
> while($rows[] = mysql_fetch_assoc($result)) { }
> 
> echo number_format(memory_get_usage()) . " Bytes used after array
> creation\n";
> 
> mysql_free_result($result);
> 
> echo number_format(memory_get_usage()) . " Bytes used after
> mysql_free_result\n";
> ?>
> 
> 65,588 Bytes used before query
> 6940 rows returned
> 67,264 Bytes used after query
> 6,376,612 Bytes used after array creation
> 6,376,756 Bytes used after mysql_free_result
> 

yup appears i was talking bollocks :D lolol - confusion came on my part
from optimisation of a high traffic site over the past few years!

correction:

on simple scripts it is slower, marginally, (due to two for loops and
more data in memory)

however - in real world applications where such nasty practises as
escaping in and out of html to render results and secondary per row
queries or api calls are taken in to account this method of getting
everything out of the db in a single swift action gives the performance
boost - and more over on high traffic sites freeing up the connection /
query resources as quickly as possible makes a rather noticeable impact,
too many open connections and queries can be very nasty when your
getting a few hundred / thousand requests per second.

sorry 'bout that!

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux