简体   繁体   English

通过使用限制或使用程序来提取特殊记录的SQL选择

[英]SQL select by using limit or using program to fetch special records

I am using php to get special records from Database. 我正在使用php从数据库获取特殊记录。
which one is better? 哪一个更好?
1. 1。

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

or 2. 或2。

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

In this case, which one should I use? 在这种情况下,我应该使用哪一个?
Which one using DB with less resources? 哪一个使用的DB的资源较少?

which one is better? 哪一个更好?

Too vague: what is "better"? 太含糊:什么是“更好”?

Which one using DB with less resources? 哪一个使用的DB的资源较少?

You're much better off with the first approach. 第一种方法会使您的生活变得更好。 It's efficient to select as little data as you need and no more. 选择所需的数据少而不需要多的数据非常有效。 Selecting the whole table will force your script to use a lot more memory because all that data needs to be kept live 选择整个表将迫使您的脚本使用更多的内存,因为所有这些数据都需要保持活动状态

The best answer you'll get is: test! 您将获得的最佳答案是: 测试! You can run your queries multiple times in multiple ways and see for yourself. 您可以通过多种方式多次运行查询,并亲自查看。 Just use SELECT SQL_NO_CACHE... instead of the generic SELECT... to force the DB to restart the work from scratch. 只需使用SELECT SQL_NO_CACHE...而不是通用的SELECT...即可强制DB从头开始工作。 Measure how long it takes to run the query and process results 测量运行查询和处理结果所需的时间

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');

You can then compare the two times. 然后,您可以比较两次。 Generally (not always) a process that takes significantly less time uses fewer resources 通常(并非总是),花费大量时间的过程使用较少的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM