0

This is in the documentation:

The MySQL Native Driver manages memory different than the MySQL Client Library. The libraries differ in the way memory is allocated and released, how memory is allocated in chunks while reading results from MySQL, which debug and development options exist, and how results read from MySQL are linked to PHP user variables.

All memory allocation and deallocation is done using the PHP memory management functions. Therefore, the memory consumption of mysqlnd can be tracked using PHP API calls, such as memory_get_usage().

I am using PHP 5.4 so MySQL extensions are compiled with use of mysqlnd driver. Even that documentation says that all memory management for mysqlnd is done via PHP I can't see any memory usage after query to table which returns cca 1MB BLOB result set. The query is used in buffered (mysql_store_result()) mode:

$result = $conn->query('SELECT * FROM test');
echo memory_get_usage()/1024;

Why memory_get_usage() doesnt show memory usage boost after the query ?

I thought that query() returns whole result set from MySQL server to mysqlnd when buffered mode is used.

Dusan Cani
  • 93
  • 9
  • 2
    query() will typically execute the query and prepare the resultset for returning, but not actually return the results to PHP.... a resultset is normally a set of pointers to the records, not the records themselves – Mark Baker May 26 '14 at 14:49
  • C-API MySQL reference says: _There are two ways for a client to process result sets. One way is to retrieve the entire result set all at once by calling mysql_store_result(). This function acquires from the server all the rows returned by the query and stores them in the client. The second way is for the client to initiate a row-by-row result set retrieval by calling mysql_use_result(). This function initializes the retrieval, but does not actually get any rows from the server._ – Dusan Cani May 26 '14 at 15:23
  • _In both cases, you access rows by calling mysql_fetch_row(). With mysql_store_result(), mysql_fetch_row() **accesses rows that have previously been fetched from the server.** With mysql_use_result(), mysql_fetch_row() actually retrieves the row from the server._ [link]http://dev.mysql.com/doc/refman/5.0/en/c-api-function-overview.html So it seems that mysqli PHP extension's query('SELECT * FROM test') should initiate mysql_query() and mysql_store_result() C functions in mysqlnd which should fetch whole result-set to the client. Or Do I understand it uncorrectly ? Thanks – Dusan Cani May 26 '14 at 15:35
  • Mark Baker: `$result = $conn->query('SELECT * FROM test');sleep(40);$result->fetch_all();` - If I stop MySQL service in the time of sleep I am still able to fetch the results. So the query() in buffered mode immediately transfers result to the client, as documentations says. – Dusan Cani May 26 '14 at 15:59
  • In that case, if you're right and the data is stored in PHP memory, then I'm obviously very, very wrong.... which still leaves you to explain why there's no change in the PHP memory usage, otherwise there's magic involved – Mark Baker May 26 '14 at 16:31

0 Answers0