100行需要大约20秒才能列出

时间:2016-04-22 05:56:58

标签: php mysql indexing

我正在创建一个内部患者日历,该诊所在数据库中拥有超过<40,000名患者。

我正在尝试列出 9000 行,但这需要很长时间,我尝试使用 100 行,需要 20秒,我怎样才能让它更快?

这是我的代码:

$getPatients = $db->query("set names 'utf8'");
$q = "SELECT id, p_type, clinic_id, recommended_doctor, hdyhau, partnership_companies, p_auto_control_date, first_name, last_name, company, mobile, p_city, p_state, p_country, saved_by  FROM dg_patients_patients WHERE clinic_id = {$defaultClinic} ORDER BY first_name ASC LIMIT 100";
$getPatients = $db->query($q);

$patientList = "";

while ($row = mysql_fetch_array($getPatients)) {

    //Get Patient Type
    $getPatientType = $db->query("set names 'utf8'");
    $q = "SELECT * FROM dg_patient_type WHERE id = {$row['p_type']}";
    $getPatientType = $db->query($q);
    $patientType = mysql_fetch_array($getPatientType);

    //Get Partnership Company
    if($row['partnership_companies'] != '' && $row['partnership_companies'] > 0) {
        $getPC = $db->query("set names 'utf8'");
        $q = "SELECT * FROM dg_partnership_companies WHERE id = {$row['partnership_companies']}";
        $getPC = $db->query($q);
        $pc = mysql_fetch_array($getPC);
        $pcname = $pc['pc_name'];
    } else {
        $pcname = '';
    }

    if(!empty($row['saved_by'])){
        //Get User
        $getUser = $db->query("set names 'utf8'");
        $q = "SELECT * FROM dg_users WHERE id = {$row['saved_by']}";
        $getUser = $db->query($q);
        $user = mysql_fetch_array($getUser);

        $savedby = $user['first_name'];
    } else {
        $savedby = '';
    }

    //Get Total Appointments
    $q1 = "SELECT * FROM dg_appointments WHERE (appointment_type = 1 OR appointment_type =2 ) AND patient_id = {$row['id']}";
    $getApps = $db->query($q1);
    $totalAppointments = mysql_num_rows($getApps);

    //Get Latest Appointment Date
    $q11 = "SELECT * FROM dg_appointments WHERE appointment_status = 4 AND patient_id = {$row['id']} ORDER BY start_date DESC, start_time DESC LIMIT 1";
    $getLastesApp = $db->query($q11);
    $lastesApp = mysql_fetch_array($getLastesApp);

    //Get Clinic
    $getClinic = $db->query("set names 'utf8'");
    $q = "SELECT * FROM dg_clinics WHERE id = {$row['clinic_id']}";
    $getClinic = $db->query($q);
    $clinic = mysql_fetch_array($getClinic);

    //Get Doctor
    if($row['recommended_doctor'] != '' && $row['recommended_doctor'] > 0) {
        $getDoctor = $db->query("set names 'utf8'");
        $q = "SELECT * FROM dg_users WHERE department = 2 AND id = {$row['recommended_doctor']}";
        $getDoctor = $db->query($q);
        $doctor = mysql_fetch_array($getDoctor);
        $doctorID = $doctor['first_name'].' '.$doctor['last_name'];
    } else {
        $doctorID = '-';
    }

    //Get HDYHAU
    if($row['hdyhau'] != '' && $row['hdyhau'] > 0){
        $q = "SELECT * FROM dg_hdyhau WHERE id = {$row['hdyhau']}";
        $getHDYHAU = $db->query($q);
        $HDYHAU = mysql_fetch_array($getHDYHAU);
        $HDYHAUID = $HDYHAU['hdyhau_name'];
    } else {
        $HDYHAUID = '-';
    }

    //Get Country
    if($row['p_country'] != '' && $row['p_country'] > 0){
        $getCountry = $db->query("set names 'utf8'");
        $sql = "SELECT * FROM dg_ulke WHERE Id = {$row['p_country']}";
        $getCountry = $db->query($sql);
        $country = mysql_fetch_array($getCountry);
        $countryID = $country['tr_TR'];
    } else {
        $countryID = '-';
    }

    //Get Cities
    if($row['p_state'] != '' && $row['p_state'] > 0){
        $getState = $db->query("set names 'utf8'");
        $sql = "SELECT * FROM dg_il WHERE Id = {$row['p_state']}";
        $getState = $db->query($sql);
        $state = mysql_fetch_array($getState);
        $stateID = $state['IlAdi'];
    } else {
        $stateID = '-';
    }

    //Get Streets
    if($row['p_city'] != '' && $row['p_city'] > 0){
        $getCity = $db->query("set names 'utf8'");
        $sql = "SELECT * FROM dg_ilce WHERE Id = {$row['p_city']}";
        $getCity = $db->query($sql);
        $city = mysql_fetch_array($getCity);
        $cityID = $city['IlceAdi'];
    } else {
        $cityID = '-';
    }

    $btn1 = "<a href='/apps/patients/patient-file.php?patientid=".$row['id']."#treatment_finance' target='_blank'><img src='/assets/images/Letter-T-blue-icon.png' width='24' height='24'></a>";

    $btn2 = "<a href='/apps/patients/patient-file.php?patientid=".$row['id']."#patient_information' target='_blank'>".$row['first_name']." ".$row['last_name']."</a>";

    if($lastesApp['start_date']){
        $latestAppDate = date('d.m.Y', strtotime($lastesApp['start_date']));
    } else {
        $latestAppDate = '-';
    }

    if($row['p_auto_control_date'] != '' && $row['p_auto_control_date'] != '0000-00-00'){
         $pacd = date('d.m.Y', strtotime($row['p_auto_control_date']));
    } else {
        $pacd = '-';
    }

    $btn5 = "<div class='checkbox checkbox-primary'><input id='checkboxPatients".$row['id']."' class='styled checkAllPatients' type='checkbox' name='checkAllPatients[]' value='".$row['id']."'><label for='checkboxPatients".$row['id']."'></label></div>";

    $patientList .= "<tr>";
        $patientList .= "<td>".$btn5."</td>";
        $patientList .= "<td>".$clinic['clinic_name']."</td>";
        $patientList .= "<td>".$btn1."</td>";
        $patientList .= "<td>".$btn2."</td>";
        $patientList .= "<td>".$row['mobile']."</td>";
        $patientList .= "<td>".$cityID."</td>";
        $patientList .= "<td>".$stateID."</td>";
        $patientList .= "<td>".$row['company']."</td>";
        $patientList .= "<td>".$pcname."</td>";
        $patientList .= "<td>".$totalAppointments."</td>";
        $patientList .= "<td>".$latestAppDate."</td>";
        $patientList .= "<td>".$pacd."</td>";
        $patientList .= "<td>".$savedby."</td>";
        $patientList .= "<td>".$doctorID."</td>";
        $patientList .= "<td>".$HDYHAUID."</td>";
        $patientList .= "<td>".$countryID."</td>";
    $patientList .= "</tr>";
}
echo $patientList;

5 个答案:

答案 0 :(得分:1)

  1. 在表中使用索引,索引在where子句中使用的字段。
  2. 而不是使用select *指定列名
  3. 而不是交互数据库的多种类型,一次从数据库中获取数据,然后使用php显示数据。

答案 1 :(得分:1)

  1. 您需要为clinic_id和first_name创建索引。
  2. 请参阅此文档http://www.w3schools.com/sql/sql_create_index.asp将有所帮助

    1. 您在此代码中使用了太多查询。减少查询会更好。使用join将条件与多个表信息连接一次。 http://www.w3schools.com/sql/sql_join_left.asp

    2. set names utf8每个代码只需要一次。如果在获取其他语言时出现问题,请首先检查my.cnf以使用服务器和连接定义默认字符集。

答案 2 :(得分:0)

请尝试不使用SELECT *....来提供您需要的列名称,并在这些列上创建Index
警告:仅仅因为indexing有助于不在所有列上创建index,导致index使用额外空间

答案 3 :(得分:0)

检查如何提供索引。我为你附上了图片。

<强>语法

ALTER TABLE `Table_name` ADD INDEX ( `Column_name` ) ;

enter image description here

答案 4 :(得分:0)

以下是我的建议:

1)您有11个选择查询。其中,10个在循环中,其中至少有一半在循环中运行。

2)这意味着对于顶部查询中的每个记录,您运行的是6个查询。对于100行,它是600次查询(600次数据库交互)。因此,您的页面至少需要20秒。

3)同样,每个SQL都有SELECT *,这需要更多的时间,因为数据库表可能有多个字段。

<强>解决方案

1)每个SQL都有$q = "SELECT * FROM dg_users WHERE id = {$row['saved_by']}";,只使用必填字段。

2)尽量不要在循环中触发查询。

3)相反,在循环之前获取所有记录并通过键值数组访问它们。

4)例如,sql: key

您可以将数组中包含user id的{​​{1}}和value作为user name的所有用户数组

在循环中,从这个数组中获取值:

$savedby = isset($users[$row['saved_by']]) ? $users[$row['saved_by']] : '';

对循环中的所有查询重复相同的操作。

希望,它会对你有帮助。