SQL select 通过使用限制或使用程序来获取特殊记录

SQL select by using limit or using program to fetch special records

我正在使用 php 从数据库中获取特殊记录。
哪个更好?
1.

Select * From [table] Limit 50000, 10;
while($row = $stmt->fetch()){
     //save in array, total 10 times
}

或 2.

Select * From [table];
$start = 50000;
$length = 10;
while($row = $stmt->fetch()){
     if($i < $start+$length && $j >=$start){
          //save in array, total 50010 times
     }
}

在这种情况下,我应该使用哪一个?
哪一个使用资源较少的数据库?

which one is better?

太模糊:什么是 "better"?

Which one using DB with less resources?

第一种方法效果更好。 select 只需要您需要的数据即可高效。选择整个 table 将迫使您的脚本使用更多内存,因为所有数据都需要保持实时状态

您将获得的最佳答案是:测试!您可以通过多种方式多次运行您的查询并亲自查看。只需使用 SELECT SQL_NO_CACHE... 而不是通用的 SELECT... 来强制数据库从头开始重新启动工作。测量 运行 查询和处理结果

需要多长时间
function wayOne(){
    //  execute your 1st query and loop through results
}
function wayTwo(){
    // execute 2nd query and loop through results
}

//Measures # of milliseconds it takes to execute another function
function timeThis(callable $callback){
    $start_time = microtime();
    call_user_func($callback);

    $microsecs = microtime()-$start_time; //duration in microseconds    
    return round($microsecs*1000);//duration in milliseconds
}

$wayOneTime = timeThis('wayOne');
$wayTwoTime = timeThis('wayTwo');

然后你可以比较这两个时间。通常(并非总是)一个花费更少时间的过程使用更少的资源