mysql - PHP 返回奇怪的行数

标签 mysql codeigniter mysql-num-rows

我正在处理一个非常大的数据集(准确地说是 786,432 行)。

因此,为了防止内存限制,我想循环遍历 50,000 行的数据集,因此为了测试这一点,我想我会尝试:

function test(){
    $start = 0;
    $end = 50000;

    $q = $this->db->select('uuid')->from('userRegionLink')->limit($end, $start)->get();
    $i = 0;
    while($q->num_rows() != 0){
        echo 'Round: '.++$i.'<br />';
        echo 'Rows: '.$q->num_rows().'<br />';
        echo 'Start: '.$start.'<br />';
        echo 'End: '.$end.'<hr />';

        $start = $end;
        $end = $end+50000;
        $q = $this->db->select('uuid')->from('userRegionLink')->limit($end, $start)->get();
    }
}

但我的结果非常奇怪:看看第 9 轮及以下。

这是什么原因造成的?

Round: 1
Rows: 50000
Start: 0
End: 50000


Round: 2
Rows: 100000
Start: 50000
End: 100000
Round: 3
Rows: 150000
Start: 100000
End: 150000
Round: 4
Rows: 200000
Start: 150000
End: 200000
Round: 5
Rows: 250000
Start: 200000
End: 250000
Round: 6
Rows: 300000
Start: 250000
End: 300000
Round: 7
Rows: 350000
Start: 300000
End: 350000
Round: 8
Rows: 400000
Start: 350000
End: 400000
Round: 9
Rows: 386432
Start: 400000
End: 450000
Round: 10
Rows: 336432
Start: 450000
End: 500000
Round: 11
Rows: 286432
Start: 500000
End: 550000
Round: 12
Rows: 236432
Start: 550000
End: 600000
Round: 13
Rows: 186432
Start: 600000
End: 650000
Round: 14
Rows: 136432
Start: 650000
End: 700000
Round: 15
Rows: 86432
Start: 700000
End: 750000
Round: 16
Rows: 36432
Start: 750000
End: 800000

最佳答案

看起来 $end 不是全局偏移量,只是要获取的记录数(相对于 $start 的偏移量)。尝试将 $end 始终设置为 50000 并仅更改 $start。

关于mysql - PHP 返回奇怪的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6195463/

相关文章:

php - utf8 到 utf8mb4 mysqli 字符集 php

php - 我的网站在庞大的数据库中搜索数据。我应该使用 Lucene 来搜索还是自己编写算法?

mysql - 如何通过每天和每周定义的层次结构汇总事件类型(使用 SQL 或 ETL)

php - 连接到同一网络上另一台电脑上的 mysql 数据库

CodeIgniter:找不到类 'CI_Controller'

javascript - 如何调用附加在div中的angularjs函数

php - 如何在 SQL、Codeigniter 中的一个查询中设置不同的限制不同列?

php - MySQL 行数总是返回 false

PHP mysql_num_rows() 在 INNER JOIN 查询中的用法

php - 如何构建 MySQL OR?