'Maria DB SQL group by and sum query is slow
My My SQl query is taking 6 sec to execute, I have added index on the where field, but its still same speed, then I remove the where clause and check the speed, the different is very small. So the problem is in SUM and Group By, I have read to optimize Group by adding index on Group BY fields but they are on different columns. Not an Expert in Mysql im a java developer.
SELECT
IF(
categories_1.NAME IS NULL,
categories.name,
categories_1.name
) AS PARENTCAT,
categories.NAME AS SUBCAT,
products.REFERENCE AS PRODREF,
products.NAME AS PRODNAME,
SUM(ticketlines.UNITS) AS UNITS,
SUM(ticketlines.PRICE * ticketlines.UNITS) AS SALES
FROM
taxes taxes
INNER JOIN ticketlines ticketlines ON (taxes.ID = ticketlines.TAXID)
INNER JOIN products products ON (products.ID = ticketlines.PRODUCT)
RIGHT OUTER JOIN categories categories ON (categories.ID = products.CATEGORY)
LEFT OUTER JOIN categories categories_1 ON (categories.PARENTID = categories_1.ID)
INNER JOIN tickets tickets ON (tickets.ID = ticketlines.TICKET)
INNER JOIN receipts receipts ON (tickets.ID = receipts.ID)
WHERE
(
receipts.DATENEW BETWEEN DATE('2021-12-01 00:00:00.000')
AND DATE('2022-02-18 00:21:00.000')
)
GROUP BY
categories.NAME,
products.REFERENCE
ORDER BY
PARENTCAT ASC,
SUBCAT ASC,
PRODNAME ASC
The Index in on receipts.DATENEW
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

