Sloppy and lazy database handling: PHP's shortcomings, part #47
Here's yet another reason why PHP is deficient when it comes to doing Real Work with Real Databases. We're currently working on a PostgreSQL-based database with six million book records. We want to walk the book table and slurp up the rows as we go. The code looks like this.
$sql = 'select field1, field2, ... field8 from book';
$set = pg_query( $sql );
while ( $row = pg_fetch_row( $set ) ) {
// Do some processing
}
pg_free_result( $set );
Nothing crazy, right? I'm just walking the table, one row at a time. However, the PHP PostgreSQL functions slurp up all six million rows, even though my calling code doesn't need to see all the rows at once. This eats up tons of RAM and makes us worry about memory allocation for the PHP process. too bad for us users! We can't even get to the first pg_fetch_row() call because the pg_query slurps up all the rows immediately.
Now, there IS functionality where you can retrieve a given row, like:
$row = pg_fetch_row( $set, $rowno );
In this case, it's necessary to iterate to that given $rowno, but even then you wouldn't have to get to the end of the dataset.
If that's not lazy enough coding on the back end, check out this little nugget from the docs for the pg_free_result function:
pg_free_result() frees the memory and data associated with the specified PostgreSQL query result resource.
This function need only be called if memory consumption during script execution is a problem. Otherwise, all result memory will be automatically freed when the script ends.
The PHP docs encourage sloppy coding. Rather than promoting good, safe programming behaviors, they're saying "Eh, no big deal." It's like the PHP team knows that PHP won't be used on large datasets, and it becomes a self-fulfilling prophecy.
PHP needs to be left to the ghetto of guestbooks and the odd bulletin board system. Leave the real work to the real languages.
0 TrackBacks
Listed below are links to blogs that reference this entry: Sloppy and lazy database handling: PHP's shortcomings, part #47.
TrackBack URL for this entry: http://perlbuzz.com/cgi-bin/mt/mt-tb.cgi/362
"Although PostgreSQL supports cursors, they have not been used in the current implementation. When DBD::Pg was defined, cursors in PostgreSQL could only be used inside a transaction block. Because only one transaction block at a time is allowed, this would have implied the restriction not to use any nested SELECT statements. Therefore the execute method *fetches all data at once* into data structures located in the front-end application. This fact must to be considered when selecting large amounts of data!"
so you have actually the same problem in perl when using DBD::Pg