【问题标题】:CodeIgniter ActiveRecord one row at a timeCodeIgniter ActiveRecord 一次一行
【发布时间】:2013-01-24 11:20:15
【问题描述】:

我正在运行一个选择许多行的查询。当我循环遍历结果时,我想一次获取一行(因为一次获取​​它们的内存太多)。

问题是,CodeIgniter 的活动记录 $query->row 的所有变体都会将数据库中的所有记录提取到内存中(并返回单行)。我查看了 DB_result.php 的来源,发现:

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() 内部:

...
while ($row = $this->_fetch_assoc())
{
    $this->result_array[] = $row;
}

这会将数据库中的所有记录提取到内存中。

是否有一种解决方法可以让我在不将整个结果集加载到内存的情况下获取单行?

【问题讨论】:

  • 您似乎研究得很好,我想您已经考虑过这一点,但为什么不使用带 LIMIT 的原始 SQL 一次选择一行?
  • 在 inside_result_object 中添加带有数字的 $i?还有 $this->result_array[$i] 这样你就可以指向特定的行?
  • @Calle 这是一个非常糟糕的解决方案,这意味着运行超过 100,000 个查询
  • @edwardmp 也一样
  • @galchen 是的,这很糟糕,但我认为这就是你想要做的......?一次提取一行是什么意思?

标签: php mysql codeigniter activerecord


【解决方案1】:

解决方案

$query = $this->db->get();
while($r = $query->_fetch_object()){
    ...
}

可能是他们将其公开的原因......

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 2014-01-20
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2010-12-31
    • 2012-11-12
    相关资源
    最近更新 更多