我有一个查询,它根据所涉及的销售、销售项目和员工构建一行销售信息。它在销售量为 10-20 的开发服务器上运行良好,但在销售量超过 5,000 的终端服务器上运行时,它会使整个系统停止运行。
SELECT Sales.SaleID,
Sales.StaffID,
Sales.CustomerID,
Sales.Timestamp,
Sales.Refunded,
Sales.PaymentType,
Staff.Forename AS staffForename,
Staff.Surname AS staffSurname,
itemList, sumOfCost,
sumOfPrice,
a.sumOfPrice - a.sumOfCost AS sumOfProfit
FROM Sales
INNER JOIN Staff ON Sales.StaffID = Staff.StaffID
LEFT JOIN (
SELECT SaleID,
GROUP_CONCAT(Quantity, ' x ', Name) itemList,
SUM(Cost*Quantity) sumOfCost, SUM(Price*Quantity) sumOfPrice
FROM SaleItems GROUP BY SaleID
) a
ON a.SaleID = Sales.SaleID
WHERE Sales.Deleted = '0'
AND Sales.ShopID = '0'
ORDER BY Sales.Timestamp DESC LIMIT 0,15
任何帮助将不胜感激!此查询的最后一次尝试耗时 65 秒...