我有以下查询和php代码:
$sql = "SELECT DISTINCT j.OrderID, DATE_FORMAT(j.StartDate,'%W %e %M %Y %H:%i:%s') AS StartDate,
j.Object, j.Signature, j.PrintedWithBSN, j.PrintedWithoutBSN,
p.ProjectID AS ProjectName, p.City, p.PostalCode, p.StreetName,
c.Name AS CustomerName
FROM JobOrder j
INNER JOIN Project p ON p.ProjectID = j.ProjectID
INNER JOIN Customer c ON c.CustomerID = p.CustomerID
INNER JOIN OrderEmployer oe ON j.OrderID=oe.OrderID
INNER JOIN Employer e ON oe.EmployerID=e.EmployerID
WHERE j.Removed = 0 AND Date(j.StartDate)=:StartDate
ORDER BY e.Firstname, p.WorkDone, j.Signature, j.StartDate DESC, c.Name";
$query = $database->prepare($sql);
$query->execute(array('StartDate' => $date));";
使用microtime php函数,我发现$query->execute
大约需要15秒才能完成。一年前,查询仅在几秒钟内执行。我不知道是什么原因导致了额外的等待时间,这是我写的不好的查询吗?
-------编辑: 我的解释结果:
------- Edit2: 我的索引是(每个表):
答案 0 :(得分:0)
按照Uueerdo和Barmar的提示,我修复了较长的查询时间。通过简单地向WHERE子句使用的字段添加索引,我将查询时间减少到少于0.1秒(我什至看到了0,00019192695617676秒)!