Go Back   Garayed.com > PHP
Reply
 
LinkBack Thread Tools Search this Thread Display Modes

Re: Does the mysql_query function already fetch the data ?
  #11 (permalink)  
Old 02-09-2010, 06:01 PM
The Natural Philosopher
 
Posts: n/a
Default Re: Does the mysql_query function already fetch the data ?

Johannes Keßler wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 09/02/10 16:30, The Natural Philosopher wrote:
>> matt wrote:
>>> On Feb 9, 7:55 am, "C. (http://symcbean.blogspot.com/)"
>>> <colin.mckin...@gmail.com> wrote:
>>>> On Feb 8, 7:45 pm, matt <matthew.leonha...@gmail.com> wrote:
>>>>
>>>>
>>>>
>>>>> On Feb 8, 10:06 am, Johannes Keßler <m...@bananas-playground.net>
>>>>> wrote:
>>>>>> -----BEGIN PGP SIGNED MESSAGE-----
>>>>>> Hash: SHA1
>>>>>> Hello,
>>>>>> the documentation says:
>>>>>> The returned result resource should be passed to
>>>>>> mysql_fetch_array(), and other
>>>>>> functions for dealing with result tables, to access the returned data.
>>>>>> Does this mean that all the data is already fetched from the DB and
>>>>>> loaded into
>>>>>> RAM and I only need the mysql_fetch_* functions to extract it ?
>>>>>> eg.
>>>>>> $query = mysql_query("SELECT * FROM `table`");
>>>>>> If in table are rows worth some megaBytes, are those megabytes
>>>>>> already loaded
>>>>>> into RAM or is this only happening if if I use
>>>>>> mysql_fetch_assoc/array etc.
>>>>>> eg.
>>>>>> $query = mysql_query("SELECT * FROM `table`");
>>>>>> // is the data already here loaded into RAM
>>>>>> while($result = mysql_fetch_assoc($query)) {
>>>>>> $newArray[] = $result;
>>>>>> // or at this point ?
>>>>>> }
>>>>>> regards,
>>>>>> johannes keßler
>>>>> You could always run some tests with memory_get_usage()
>>>> No - the client libs will buffer the results (potentially the complete
>>>> result set even before any call to mysql_fetch_....) but outside the
>>>> PHP memory area - IIRC memory_get_usage only reports memory allocated
>>>> by PHP.
>>> I guess I assumed "loaded into RAM" in the OP to mean "loaded into
>>> PHP's memory space." Either way there should still be a way to
>>> interrupt the process to analyze system memory usage:
>>>
>>> $sth = mysql_query($queryWithLargeDataset);
>>>
>>> echo "Memory usage (mysql_query): " . memory_get_usage();
>>> fgets(STDIN);
>>>
>>> while ($result[] = mysql_fetch_assoc($sth));
>>>
>>> echo "Memory usage (mysql_fetch): " . memory_get_usage();
>>> fgets(STDIN);
>>>
>>> Using other system tools to determine if the memory is getting loaded
>>> outside of PHP after the mysql_query() call. If the result set is
>>> significant enough (and the system is quiet enough), it should be
>>> pretty easy to tell looking at top, etc.
>>>

>> how do you define 'outside of php' since its *client* libraries are
>> essentially 'owned' buy the php process anyway..
>>

>
> I was refering to matt. He came up with this idea.
>
> The only thing we can says for sure the mysql_fetch functions are loading the
> data into the PHP RAM space and not the query call.
>


which simply means that some kind of malloc() has already been done by
the library routines..and the ram usage doubles when its fetched.


I never use mysql_fetch. just mysql_result on what I need. As I need it.


> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.14 (GNU/Linux)
>
> iEYEARECAAYFAktxgOQACgkQE++2Zdc7EteIbACffAFSlh7twE aKloZpiWhLo30l
> 6iUAnjblWHGiDo16pW5atKhtZVUHDfFw
> =1J8T
> -----END PGP SIGNATURE-----

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Does the mysql_query function already fetch the data ?
  #12 (permalink)  
Old 02-09-2010, 06:17 PM
matt
 
Posts: n/a
Default Re: Does the mysql_query function already fetch the data ?

On Feb 9, 2:01Â*pm, The Natural Philosopher <t...@invalid.invalid>
wrote:
> Johannes Keßler wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1

>
> > On 09/02/10 16:30, The Natural Philosopher wrote:
> >> matt wrote:
> >>> On Feb 9, 7:55 am, "C. (http://symcbean.blogspot.com/)"
> >>> <colin.mckin...@gmail.com> wrote:
> >>>> On Feb 8, 7:45 pm, matt <matthew.leonha...@gmail.com> wrote:

>
> >>>>> On Feb 8, 10:06 am, Johannes Keßler <m...@bananas-playground.net>
> >>>>> wrote:
> >>>>>> -----BEGIN PGP SIGNED MESSAGE-----
> >>>>>> Hash: SHA1
> >>>>>> Hello,
> >>>>>> the documentation says:
> >>>>>> The returned result resource should be passed to
> >>>>>> mysql_fetch_array(), and other
> >>>>>> functions for dealing with result tables, to access the returned data.
> >>>>>> Does this mean that all the data is already fetched from the DB and
> >>>>>> loaded into
> >>>>>> RAM and I only need the mysql_fetch_* functions to extract it ?
> >>>>>> eg.
> >>>>>> $query = mysql_query("SELECT * FROM `table`");
> >>>>>> If in table are rows worth some megaBytes, are those megabytes
> >>>>>> already loaded
> >>>>>> into RAM or is this only happening if if I use
> >>>>>> mysql_fetch_assoc/array etc.
> >>>>>> eg.
> >>>>>> $query = mysql_query("SELECT * FROM `table`");
> >>>>>> // is the data already here loaded into RAM
> >>>>>> while($result = mysql_fetch_assoc($query)) {
> >>>>>> Â* Â*$newArray[] = $result;
> >>>>>> Â* Â*// or at this point ?
> >>>>>> }
> >>>>>> regards,
> >>>>>> johannes keßler
> >>>>> You could always run some tests with memory_get_usage()
> >>>> No - the client libs will buffer the results (potentially the complete
> >>>> result set even before any call to mysql_fetch_....) but outside the
> >>>> PHP memory area - IIRC memory_get_usage only reports memory allocated
> >>>> by PHP.
> >>> I guess I assumed "loaded into RAM" in the OP to mean "loaded into
> >>> PHP's memory space." Â*Either way there should still be a way to
> >>> interrupt the process to analyze system memory usage:

>
> >>> $sth = mysql_query($queryWithLargeDataset);

>
> >>> echo "Memory usage (mysql_query): " . memory_get_usage();
> >>> fgets(STDIN);

>
> >>> while ($result[] = mysql_fetch_assoc($sth));

>
> >>> echo "Memory usage (mysql_fetch): " . memory_get_usage();
> >>> fgets(STDIN);

>
> >>> Using other system tools to determine if the memory is getting loaded
> >>> outside of PHP after the mysql_query() call. Â*If the result set is
> >>> significant enough (and the system is quiet enough), it should be
> >>> pretty easy to tell looking at top, etc.

>
> >> how do you define 'outside of php' since its *client* libraries are
> >> essentially 'owned' buy the php process anyway..

>
> > I was refering to matt. He came up with this idea.

>
> > The only thing we can says for sure the mysql_fetch functions are loading the
> > data into the PHP RAM space and not the query call.

>
> which simply means that some kind of malloc() has already been done by
> the library routines..and the ram usage doubles when its fetched.


Right. Actually, the RAM usage reported by memory_get_usage() seems
to never include memory allocated to a client library. Since
everything is loaded into an array, and very little memory is released
after the free() call, we can assume that the memory usage after the
query reflects the array itself (which is not unset at any point).
Hence my recommendation to interrupt these processes with fgets(STDIN)
so as to allow time to examine other pids between function calls.

If the overall system memory doesn't increase after the mysql_query()
call, then we have an answer to the OP. Clearly, based on the test he
ran, if the system memory does increase by the anticipated amount at
this time, then we have already fetched the data from the DB server.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Does the mysql_query function already fetch the data ?
  #13 (permalink)  
Old 02-09-2010, 06:18 PM
Michael Fesser
 
Posts: n/a
Default Re: Does the mysql_query function already fetch the data ?

..oO(The Natural Philosopher)

>Johannes Keßler wrote:
>>
>> The only thing we can says for sure the mysql_fetch functions are loading the
>> data into the PHP RAM space and not the query call.
>>

>
>which simply means that some kind of malloc() has already been done by
>the library routines..


Probably. But then there are buffered queries, unbuffered queries,
different interfaces and different DBMS, all with different ways of
working and caching.

>and the ram usage doubles when its fetched.


RAM is there to be used. But usually you only have to keep an eye on
what happens in your PHP scripts. What's used outside of them in some
library code doesn't really matter.

>I never use mysql_fetch. just mysql_result on what I need. As I need it.


mysql_result() is ugly and inefficient. I prefer clean code and easy
access to all the fields of a record, even if it might cost some more
memory. YMMV.

Micha
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:34 AM.


Powered by vBulletin® Version 3.5.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
| Home | FAQ | Members List | Calendar | Today's Posts | Search | New Posts |