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. |