以下查询非常慢(大约需要1秒钟),但只搜索大约2500条记录(+内连接表)。 如果我删除ORDER BY,查询将在更短的时间内运行(0.05或更少) 或者,如果我删除嵌套的部分,请选择以下#34;#用于选择没有ProfilePhoto指定的位置"它也运行得很快,但我需要包括这两个。
我有索引(或主键):tPhoto_PhotoID,PhotoID,p.Enabled,CustomerID,tCustomer_CustomerID,ProfilePhoto(bool),u.UserName,e.PrivateEmail,m.tUser_UserID,Enabled,Active,m.tMemberStatuses_MemberStatusID, e.tCustomerMembership_MembershipID,e.DateCreated (我的索引太多了吗?我的理解是将它们添加到我使用WHERE或ON的任何地方)
查询:
SELECT e.CustomerID,
e.CustomerName,
e.Location,
SUBSTRING_INDEX(e.CustomerProfile,' ', 25) AS Description,
IFNULL(p.PhotoURL, PhotoTable.PhotoURL) AS PhotoURL
FROM tCustomer e
LEFT JOIN (tCustomerPhoto ep INNER JOIN tPhoto p ON (ep.tPhoto_PhotoID = p.PhotoID AND p.Enabled=1))
ON e.CustomerID = ep.tCustomer_CustomerID AND ep.ProfilePhoto = 1
# used to select where no ProfilePhoto specified
LEFT JOIN ((SELECT pp.PhotoURL, epp.tCustomer_CustomerID
FROM tPhoto pp
LEFT JOIN tCustomerPhoto epp ON epp.tPhoto_PhotoID = pp.PhotoID
GROUP BY epp.tCustomer_CustomerID) AS PhotoTable) ON e.CustomerID = PhotoTable.tCustomer_CustomerID
INNER JOIN tUser u ON u.UserName = e.PrivateEmail
INNER JOIN tmembers m ON m.tUser_UserID = u.UserID
WHERE e.Enabled=1
AND e.Active=1
AND m.tMemberStatuses_MemberStatusID = 2
AND e.tCustomerMembership_MembershipID != 6
ORDER BY e.DateCreated DESC
LIMIT 12
我有类似的查询,但运行得更快。
任何意见都将不胜感激:
答案 0 :(得分:1)
直到我们更清楚地了解您在其他查询等工作之间的问题。在MySQL客户端中尝试EXPLAIN {YourSelectQuery}
并查看改善性能的建议。