mysql_unbuffered_query

(PHP 4 >= 4.0.6, PHP 5)

mysql_unbuffered_query --  向 MySQL 发送一条 SQL 查询,并不获取和缓存结果的行

说明

resource mysql_unbuffered_query ( string query [, resource link_identifier] )

mysql_unbuffered_query() 向 MySQL 发送一条 SQL 查询 query,但不像 mysql_query() 那样自动获取并缓存结果集。一方面,这在处理很大的结果集时会节省可观的内存。另一方面,可以在获取第一行后立即对结果集进行操作,而不用等到整个 SQL 语句都执行完毕。当使用多个数据库连接时,必须指定可选参数 link_identifier

注: mysql_unbuffered_query() 的好处是有代价的:在 mysql_unbuffered_query() 返回的结果集之上不能使用 mysql_num_rows()mysql_data_seek()。此外在向 MySQL 发送一条新的 SQL 查询之前,必须提取掉所有未缓存的 SQL 查询所产生的结果行。

参见 mysql_query()


add a note add a note User Contributed Notes
silvanojr at gmail dot com
13-May-2006 05:02
Note:  The benefits of mysql_unbuffered_query() come at a cost: You cannot use mysql_num_rows() and...

but it looks like you can use SQL_CALC_ROWS on MySQL to get the total rows without the limit.
andre dot steffens at adress-research dot de
08-Oct-2004 09:18
If you use mysql_ping() to check the connection, the resultset from mysql_unbuffered_query() will be kill.
steve_stockman at mac dot symantec dot com
02-Apr-2004 06:19
If you are going to do a large query, but are concerned about blocking access to the table during an unbuffered query, why not go through a temporary table? (Of course, this is predicated on the current user having permission to create tables.)

$dbQuery = "SELECT something ...";
if (mysql_query ("CREATE TEMPORARY TABLE MyQuery $dbQuery")) {
   $numRows = mysql_affected_rows();
   if ($numRows == 0) {
       /* handle empty selection */
   } else {
       $result = mysql_unbuffered_query ('SELECT * FROM MyQuery');
       /* handle result */
   }
   mysql_query ('DROP TABLE MyQuery');
}
post at jfl dot dk
30-Nov-2003 09:57
If using optimized MyISAM tables I guess there is a big advantage with this function as it is possible to do selects and inserts on the same time as long as no rows in the table gets updated.
15-Jun-2003 01:35
The other hand should really be, that the table remains locked until all rows have been retrieved, right?  This is a very important thing to mention, you could tie up the whole database with a lock.
shaner at accretivetg dot com
22-May-2003 07:45
Regarding bailing on a really large result, while doing an unbuffered query, there _is_ a way to do this: kill the thread and exit your processing loop.  This, of course, requires having a separate database link.  Something like below does the trick:

// a db link for queries
$lh  = mysql_connect( 'server', 'uname', 'pword' );
// and a controller link
$clh = mysql_connect( 'server', 'uname', 'pword', true );

if ( mysql_select_db ( 'big_database', $lh ) )
{
  $began  =  time();
  $tout  = 60 * 5; // five minute limit
  $qry    = "SELECT * FROM my_bigass_table";
  $rh    = mysql_unbuffered_query( $qry, $lh );
  $thread = mysql_thread_id ( $lh );
  while ( $res = mysql_fetch_row( $rh ) )
  {
   /* do what you need to do
     * ...
     * ...
     */
   if ( ( time() - $began ) > $tout )
   {
     // this is taking too long
     mysql_query( "KILL $thread", $clh );
     break;
   }
  }
}
frappyjohn at dos2linux dot org
18-Feb-2003 02:21
Don't let the two hands confuse you, these are both advantages (they should really be on the same hand):

On the one hand, this saves a considerable amount of memory with SQL queries that produce large result sets.

On the other hand, you can start working on the result set immediately ...
david at php dot net
18-May-2002 12:25
You are absolutely required to retrieve all rows in the result set (option 'a' in the first comment). If you fail to do so, PHP will do so for you, and will emit a NOTICE warning you of the fact. From the MySQL API, "Furthermore, you must retrieve all the rows even if you determine in mid-retrieval that you've found the information you were looking for. ".

Also note that if you are using this function, you should be quick about processing the result set, or you will tie up the MySQL server (other threads will be unable to write to the tables you are reading from).

If you want to be able to 'abort' mid result-set or if you want to do lengthy processing on the results, you are misunderstanding the purpose of this function.

Also note that UPDATE queries etc return no result set, so this function is only useful for SELECT etc.
chabotc at reviewboard dot com
21-Aug-2001 05:21
Stefan,
unbuffered query sends a query to the server, and does not first download the results before sending them to the end-user (php in this case).

So what it means is that -normaly- you could do this:

$res1 = mysql_query("select some",$db_conn);
while ($row = mysql_fetch_row($res)) {
  $res2 = mysql_query("select some other",$db_conn);
  // do some other stuff
}

With an unbuffered query you could NOT do this, the result set from $res1 would be LOST on the second query.

However, it does not mean you -have- to fetch all rows ... just that the API does not save the result set in memory for you.

However, when using different db connections, it all works ofcource ...

For more information, please refer to the mysql manual, they have a lot of docs on query & unbuffered queries (the php api is just a basic wrapper around there native api's).