我如何保证从SELECT查询中获得1000行? MySQL的

时间:2016-07-01 13:01:22

标签: php mysql

有关指示使用LIMIT的答案的问题对我的情况不起作用,但可能是我误解了LIMIT的局限性和不完善之处。或者,也许我在查询错误,这使得这个问题合法。

我有一个名为“child_pages”的表,其中包含一个名为“url”的字段,其值是应该被删除的url。在抓取属于该URL的页面时,结果内容html存储在称为“内容”的字段中。 child_pages表有200,000条记录。

该表还有一个“扫描”和“已处理”字段,两者都是tinyint,因此我可以说“1”=是,此行已扫描,“1”,此行已处理。

我设置为本地服务(Windows)的一个脚本将读取child_pages表并从url字段读取值,然后执行scrape,最后将生成的html存储到content字段中。完成后,“扫描”字段将标记为“1”。

现在另一个脚本也单独运行,它查询child_pages表,查找扫描的所有记录='1',但处理='0'。从那个结果集中我将从未处理的记录中读取内容字段的html值,最后对我从“内容”字段html中提取的数据做一些事情。

这是我的疑问:

$sql = "SELECT id,content FROM child_pages WHERE scanned='1' AND processed='0' LIMIT 1000";

我注意到处理速度非常慢。我得到1到几个每五秒处理一次的记录。我想,当我一次选择1000行时,怎么可能呢?

所以我在while循环中输出了一个循环计数器,我发现它没有返回$ counting = 1000,而是像$ counting = 60。

我查询了child_pages表,发现95%的记录都被处理='0',因此有足够的记录来容纳LIMIT 1000。

有没有办法强制我的查询返回1000行?

完整查询循环:

$start = "<div id=\"detailtable\">";
$stop = "</table></td></tr></table></div>";
$sql = "SELECT id,content FROM child_pages WHERE scanned='1' AND processed='0' LIMIT 1000";
$stmt = $db->query($sql);
$new = 0;
$lookedat = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  $lookedat++;
  $content = $row['content'];
  $cid = $row['id'];
  $mark1 = strpos($content,$start);
  $mark2 = strpos($content,$stop);
  //echo $mark1 . ", " . $mark2;
  $segment = substr( $content,$mark1, ($mark2 - $mark1) + strlen($stop) );
  $doc = new simple_html_dom($segment);
  if ( ! is_null($doc->find("div[id=detailtable]", 0)) ){
    $detailtable = $doc->find("div[id=detailtable]", 0);
    if(count($detailtable) == 1){
        $e = $detailtable->children();
        $children = $e[0]->find('.data');
        $count = 0;
        $insert['processed_thru']       = trim($children[0]->plaintext);
        $insert['document_number_j']    = trim($children[1]->plaintext);
        $insert['status']               = trim($children[2]->plaintext);
        $insert['case_number']          = trim($children[3]->plaintext);
        $insert['name_of_court']        = trim($children[4]->plaintext);
        $insert['file_date']            = trim($children[5]->plaintext);
        $insert['date_of_entry']        = trim($children[6]->plaintext);
        $insert['expiration_date']      = trim($children[7]->plaintext);
        $insert['amount_due']           = trim(str_replace("$","",$children[8]->plaintext));
        $insert['interest_rate']        = trim($children[9]->plaintext);
        $insert['plaintiff']            = trim($children[10]->plaintext);

        $insert['defendant'] = "";

        for($iii=11;$iii<count($children) ;$iii++){
            $insert['defendant'] .= trim($children[$iii]->plaintext);
        }

        if( $insert['status'] !== "TERMINATED" &&
            strpos($insert['plaintiff'],"STATE OF FLORIDA") == false &&
            strpos($insert['plaintiff'],"DEPARTMENT OF REVENUE") == false &&
            strpos($insert['plaintiff'],"DEPARTMENT OF ENVIRONMENTAL PROTECTION") == false){

            //net elements here

            /*echo "<pre>";
            print_r($insert);*/

            // table: cases2 columns:  id,processed_thru,document_number_j,status,case_number,name_of_court,file_date,date_of_entry,expiration_date,amount_due,interest_rate,plaintiff,defendant
            $colstring = "processed_thru,document_number_j,status,case_number,name_of_court,file_date,date_of_entry,expiration_date,amount_due,interest_rate,plaintiff,defendant";
            $prepareColString = ":processed_thru,:document_number_j,:status,:case_number,:name_of_court,:file_date,:date_of_entry,:expiration_date,:amount_due,:interest_rate,:plaintiff,:defendant";
            $table = "cases";

            foreach($insert as $k=>$v){
                ${"$k"} = trim(preg_replace( '/\h+/', ' ', $v ));
            }

            $stmt2 = $db->prepare("INSERT INTO $table ($colstring) VALUES ($prepareColString)");
            $stmt2->bindParam(':document_number_j', $document_number_j);
            $stmt2->bindParam(':processed_thru', $processed_thru);
            $stmt2->bindParam(':status', $status);
            $stmt2->bindParam(':case_number', $case_number);
            $stmt2->bindParam(':name_of_court', $name_of_court);
            $stmt2->bindParam(':file_date', $file_date);
            $stmt2->bindParam(':date_of_entry', $date_of_entry);
            $stmt2->bindParam(':expiration_date', $expiration_date);
            $stmt2->bindParam(':amount_due', $amount_due);
            $stmt2->bindParam(':interest_rate', $interest_rate);
            $stmt2->bindParam(':plaintiff', $plaintiff);
            $stmt2->bindParam(':defendant', $defendant);
            $stmt2->execute();

            $new++;
        }
    }
  }
  $processed = 1;
  $stmt3 = $db->prepare("UPDATE child_pages SET processed=:processed WHERE id=:id");
  $stmt3->bindParam(':id', $cid);
  $stmt3->bindParam(':processed', $processed);
  $stmt3->execute();
}

附件数据:

RECORDS SCANNED : 60

NEW CASE RECORDS : 8

COMPUTATIONS IN ms : 422
SYSTEM CALLS IN ms : 15

Total execution time in seconds: 129.66131019592

输出附件数据的代码: (这些放在脚本的顶部)

// At start of script
$time_start = microtime(true);
$rustart = getrusage();

function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
 -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}


echo "<p>RECORDS SCANNED : $lookedat </p>";
echo "<p>NEW CASE RECORDS : $new </p>";

$ru = getrusage();

echo "<p>COMPUTATIONS IN ms : " . rutime($ru, $rustart, "utime") . "</p>";
echo "SYSTEM CALLS IN ms : " . rutime($ru, $rustart, "stime") . "</p>";

// Anywhere else in the script
echo '<p>Total execution time in seconds: ' . (microtime(true) - $time_start) . "</p>";

1 个答案:

答案 0 :(得分:1)

如果您的目标是在一个页面中显示所有1000行,则可以按部分加载。当使用AJAX向下滚动时,页面打开显示为100行时显示。

第1部分

$sql = "SELECT id,content FROM child_pages WHERE scanned='1' AND processed='0' LIMIT 0,100";

第二部分

$sql = "SELECT id,content FROM child_pages WHERE scanned='1' AND processed='0' LIMIT 100,200";

等...

希望它有所帮助。