codeIgniter的ActiveRecord一行的时间 [英] CodeIgniter ActiveRecord one row at a time
问题描述
我运行的选择很多行的查询。当我遍历结果,我想取一个单行的时间(因为它是太多的内存来获取它们一次全部)。
I'm running a query that selects many rows. When I loop over the results, I want to fetch a single row at a time (since it's too much memory to fetch them all at once).
但问题是,codeIgniter的活动记录$查询 - >行所有它的变化从数据库中的所有记录将获取到内存(并返回一个单行)。我看着DB_result.php的来源,结果发现:
Problem is, CodeIgniter's Active Record $query->row for all it's variations will fetch all the records from the database into the memory (and return a single row). I looked into the source of DB_result.php and found:
public function row_object($n = 0)
{
$result = $this->result_object();
if (count($result) == 0)
{
return $result;
}
if ($n != $this->current_row AND isset($result[$n]))
{
$this->current_row = $n;
}
return $result[$this->current_row];
}
和内result_object():
and inside result_object():
...
while ($row = $this->_fetch_assoc())
{
$this->result_array[] = $row;
}
这获取所有从数据库中的记录到内存中。
This fetches ALL of the records from the database into the memory.
有一种解决方法,让我去取单行,而无需加载整个结果到内存?
Is there a workaround that will allow me to fetch a single row without loading the entire resultset into the memory?
推荐答案
解决方案:
$query = $this->db->get();
while($r = $query->_fetch_object()){
...
}
可能就是这个道理,他们离开它公...
probably the reason they left it public...
这篇关于codeIgniter的ActiveRecord一行的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!