Apr 21 2010

Speed up MySQL Call from PHP using mysql_unbuffered_query

MySQL_unbuffered_query can dramatically speed up your code

When you make a call to mysql_query, all of the results get buffered to the server before continuing through the code. If you have a large result set, mysql_unbuffered_query() can speed up your process. Here is what php.net says:

mysql_unbuffered_query() sends the SQL query query to MySQL without automatically fetching and buffering the result rows as mysql_query() does. This saves a considerable amount of memory with SQL queries that produce large result sets, and you can start working on the result set immediately after the first row has been retrieved as you don’t have to wait until the complete SQL query has been performed. To use mysql_unbuffered_query() while multiple database connections are open, you must specify the optional parameter link_identifier to identify which connection you want to use.

You can use this in your code like this: Do not forget to specify the link_identifier if using multiple connections.
$result= mysql_unbuffered_query ("select SQL_CALC_ROWS id as number_of_rows, name from test");
One other note: Note: With mysql_unbuffered_query(), You cannot use mysql_num_rows() but you can use SQL_CALC_ROWS on MySQL to get the total rows without the limit.