php Zend_Db: fetchAll() 或 query()/fetch() 获取大量记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10465944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zend_Db: fetchAll() or query()/fetch() for a huge number of records
提问by peidiam
Assuming I have
假设我有
$db is an instance of Zend_Db_Adapter_Abstract and
$sql = 'SELECT blah blah FROM table' will return a huge number of records.
There are two code fragments to process the returned data as follows.
有两个代码片段来处理返回的数据,如下所示。
// Code fragment 1 (let's call it C1).
$results = $db->fetchAll($sql);
foreach ($results as $row) {
// Process $row
}
// Code fragment 2 (let's call it C2).
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
// Process $row
}
My understanding is that C1 will load all returned data to $results. So, a huge data is loaded to PHP memory. Below are my questions.
我的理解是 C1 会将所有返回的数据加载到 $results。因此,大量数据被加载到 PHP 内存中。以下是我的问题。
- Does C2 load all data to PHP memory or does it process one by one like prepare/execute?
- Assuming there is no other option, is C1 or C2 a better option?
- C2 是将所有数据加载到 PHP 内存还是像准备/执行一样一一处理?
- 假设没有其他选择,C1 或 C2 是更好的选择吗?
Thanks!
谢谢!
回答by timdev
Your hunch is correct. At least if you're using the PDO driver, ->fetch() reads the results unbuffered, whereas ->fetchAll() returns all the data in a big array.
你的预感是对的。至少如果您使用 PDO 驱动程序,->fetch() 读取未缓冲的结果,而 ->fetchAll() 返回大数组中的所有数据。
Be aware that if you're using ->fetch(), you have to be careful about what you try to do inside your loop. You can't run additional queries on the same connection while you've still got an unbuffered result set.
请注意,如果您使用 ->fetch(),则必须小心在循环中尝试执行的操作。当您仍然获得未缓冲的结果集时,您无法在同一连接上运行其他查询。
So, if your plan is to update those same rows inside the loop, you'll need to find a way to delay executing the updates (by queuing then up somehow) until you've exited the loop.
因此,如果您的计划是在循环内更新那些相同的行,您将需要找到一种方法来延迟执行更新(通过排队然后以某种方式)直到您退出循环。
回答by The Alpha
To retrieve one row from the result set, use the fetch() method of the statement object. Reference
要从结果集中检索一行,请使用语句对象的 fetch() 方法。参考
$sql = 'SELECT blah blah FROM table';
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
// Process $row
}
In above example $stmt = $db->query($sql);retrieved the resultsetin the memory and fetchis being used to fetch the current row in the loop from the resultset, which moves the cursor to the next row until it reaches the the last row in the resultset.
在上面的示例中,$stmt = $db->query($sql);检索了resultset内存中的 ,fetch并用于从 中获取循环中的当前行resultset,这将光标移动到下一行,直到到达 中的最后一行resultset。
To retrieve all the rows of the result set in one step, use the fetchAll() method. This is equivalent to calling the fetch() method in a loop and returning all the rows in an array.
要在一个步骤中检索结果集的所有行,请使用 fetchAll() 方法。这相当于在循环中调用 fetch() 方法并返回数组中的所有行。
$sql = 'SELECT blah blah FROM table';
$stmt = $db->query($sql);
$rows = $stmt->fetchAll();
echo $rows[0]['col1']; // The first field/column from the first row
Alternatively you can use
或者你可以使用
....
$table = new Mytable();
// Find a single row Returns a Rowset
$rows = $table->find(1234);
// Find multiple rows Also returns a Rowset
$rows = $table->find(array(1234, 5678));
Reference:Zend_Db_Table..
参考:Zend_Db_Table。.
For more:Fetching a Row..
更多信息:获取一行。.
I think fetchAll()is faster because it retrieves all the data in one step and returns an array but consumes more memory but fetch()consumes less memory but retrieves the data one by one.
我认为fetchAll()更快,因为它一步检索所有数据并返回一个数组,但消耗更多内存但fetch()消耗更少内存但逐个检索数据。
The API for fetch operations has been superseded to allow a Zend_Db_Table_Select object to modify the query. However, the deprecated usage of the fetchRow() and fetchAll() methods will continue to work without modification.
用于获取操作的 API 已被取代,以允许 Zend_Db_Table_Select 对象修改查询。但是,已弃用的 fetchRow() 和 fetchAll() 方法将继续有效,无需修改。
More Reference:Here.
更多参考:这里。

