shadedecho
Programmer
The following questions are for the advanced readers out there, who really understand at the core level how PHP and MySQL interoperate through the MySQL API in PHP, and how they manage communication and memory resources:
When I issue a mysql_query() command, and let's say it has 10,000,000 rows to return... does MySQL create the whole result set, all at once, and send it back to PHP in one big chunk, and the PHP deals with that by sticking the whole chunk into memory and returning back to my application a pointer to it?
Or is there some sort of paging/cache'ing going on where the database only returns parts of the result set at a time, as needed/requested, so that PHP doesn't have to stick it all into memory? (and if so, how do you tune, adjust this stuff for performance)?
Is there a way for performance sake to go through like maybe some arbitrary chunk size at a time, like 100 rows, and have the database just keep streaming that data back to PHP as it's requested?
Is this how mysql_unbuffered_query() works? I'm curious how you write a loop to process row-by-row when PHP doesn't have all the rows at the start of your loop... does each successive call to mysql_fetch_row() sit and wait for either the next row or a false from the DB to mean the result set is done? or does it immediately return false if the database is still executing and has no more rows to return yet?
Also, is the way that PHP stores the result set in memory as a "resource" as they call it, MORE efficient than munging through the whole result set, row by row, sticking it in an 2d array, and then calling mysql_free_result() to free the resource memory? Or are they stored exactly the same in memory (ie, is the resource result set compressed or anything like that, like over the communicate channel from the DB)?
And can you modify the information IN the resource result set, or do you have to grab it all out of the resource into a PHP array before modifying it?
When I issue a mysql_query() command, and let's say it has 10,000,000 rows to return... does MySQL create the whole result set, all at once, and send it back to PHP in one big chunk, and the PHP deals with that by sticking the whole chunk into memory and returning back to my application a pointer to it?
Or is there some sort of paging/cache'ing going on where the database only returns parts of the result set at a time, as needed/requested, so that PHP doesn't have to stick it all into memory? (and if so, how do you tune, adjust this stuff for performance)?
Is there a way for performance sake to go through like maybe some arbitrary chunk size at a time, like 100 rows, and have the database just keep streaming that data back to PHP as it's requested?
Is this how mysql_unbuffered_query() works? I'm curious how you write a loop to process row-by-row when PHP doesn't have all the rows at the start of your loop... does each successive call to mysql_fetch_row() sit and wait for either the next row or a false from the DB to mean the result set is done? or does it immediately return false if the database is still executing and has no more rows to return yet?
Also, is the way that PHP stores the result set in memory as a "resource" as they call it, MORE efficient than munging through the whole result set, row by row, sticking it in an 2d array, and then calling mysql_free_result() to free the resource memory? Or are they stored exactly the same in memory (ie, is the resource result set compressed or anything like that, like over the communicate channel from the DB)?
And can you modify the information IN the resource result set, or do you have to grab it all out of the resource into a PHP array before modifying it?