2017-05-09 57 views
1

请帮我生成报告speedy.it需要这么多的时间来加载报告SQL选择数据加速

for ($i = 0; $i < sizeof($array_time); $i++) { 
    $pdf->Cell(.36, .25, $array_time[$i], 'LTRB', 0, "C", 1); 
    foreach ($array_day as $days) { 
     $datefinal = $datese . $days . $array_time[$i]; 
     $query2 = "select * from $this->dbName.$this->dbViewTable where DATETIME LIKE '$datefinal%' AND COMPANY_ID='$companyid' AND SITE_ID='$site_id' AND LOCATION_ID='$location_id'"; 
     $result = $conn->query($query2); 
     $row2 = $result->fetch_assoc(); 
     $date = $row2['DATETIME']; 
     $timec = substr($date, 6, 2) . ":" . substr($date, 8, 2) . ":" . substr($date, 10, 2); 
     if ($timec == "::") { 
      $imagefill = "images/colors/bg.png"; 
      $pdf->Cell(.36, .25, "" . $pdf->Image($imagefill, $pdf->GetX(), $pdf->GetY(), .36, .25), 'LTBR', 0, 'C'); 
     } else { 
      $pdf->Cell(.36, .25, $timec, 'LTRB', 0, "C", 1); 
     } 
    } 
    $pdf->Ln(); 
} 
+0

你对相关表的索引? – raz

回答

1

确保数字字段

INT(5) must be enough 

确保日期时间字段是

DATETIME 

为创建索引

COMPANY_ID 
SITE_ID 
LOCATION_ID 

不要使用 “LIKE” 的查询,但使用

DATE_FORMAT(`datetime`, '%Y-%m-%d') = '$datefinal' 
+0

谢谢你的回复,你可以解释一下如何为COMPANY_ID创建索引 SITE_ID LOCATION_ID –

+1

谢谢你的价值包回复我为上述字段创建索引现在它会在2,3秒内生成 –