我在数据库中有超过148494封电子邮件。我需要使用foreach或while循环来获取并显示复选框值属性。问题是它花了这么多时间。我想减少时间。我尝试了不同的方法,并尽可能规范化查询,但仍面临同样的问题。
我尝试了什么
方法一:
我制作了函数并将所有记录存储到数组中,然后再添加for-each循环以显示该数组的记录(带有html)。我觉得这很糟糕,因为它比其他时间花费太多时间。
final function getActiveEmailArr(){
$getEmailArr = array();
$start = (isset($_GET['start']))? (int)$_GET['start']:0;
$end = (isset($_GET['end']))? (int)$_GET['end']:100;
$u_id = (isset($_SESSION['USERID']))? (int)$_SESSION['USERID']:0;
$EmailSts=$this->pdoConnection->prepare("select * from `ws_email` where `u_id` = :u_id and `we_status` = :we_status ORDER BY we_email ASC limit 0, 74247 ");
$EmailSts->bindParam(':u_id', $u_id);
$EmailSts->bindParam(':we_status', $we_status);
$we_status = 1;
$u_id = $u_id;
$EmailSts->execute();
while ($row = $EmailSts->fetch(PDO::FETCH_ASSOC)) {
$getEmailArr[] = $row;
}
return $getEmailArr;
}
方法二:
在这种方法中,我尝试将带有html的电子邮件值存储到$ html变量中,然后只需调用该函数,但它也需要花费很多时间来显示记录。
final function getActiveEmailArr(){
$html = '';
$getEmailArr = array();
$u_id = (isset($_SESSION['USERID']))? (int)$_SESSION['USERID']:0;
$EmailSts=$this->pdoConnection->prepare("select * from `ws_email` where `u_id` = :u_id and `we_status` = :we_status ORDER BY we_email ASC limit 100, 148300 ");
$EmailSts->bindParam(':u_id', $u_id);
$EmailSts->bindParam(':we_status', $we_status);
$we_status = 1;
$u_id = $u_id;
$EmailSts->execute();
$cc = 0;
while ($row = $EmailSts->fetch(PDO::FETCH_ASSOC)) {
if ($cc % 50 == 0){ $html .='<b>'.$cc.'</b>';}
$html .='<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" class="checkbox_j" type="checkbox" name="sendEmails[]" value="'.$row['we_email'].'">
'.$row['we_email'].'</label>
</div>
</div>';
$cc++;}
return $html;
}
我也尝试使用Limit
它是好的但是当我在查询Limit 100, 148300
中尝试这个逻辑时,它也花费了很多时间。因为查询计数148300记录然后在148300之后选择100所以我认为这就是为什么它在这种情况下花时间,这是我在想但我不确定因为我不是专家而且还在学习所以请原谅如果我在这个问题上犯了错误。