I have a query here:
SELECT MAX(Sales.Revenue-Sales.Costs), Company.Name
FROM Sales
INNER JOIN Company
ON Company.ID = Sales.ID
WHERE Sales.Date <= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH);
My question: where should I use an index and why?
I have a query here:
SELECT MAX(Sales.Revenue-Sales.Costs), Company.Name
FROM Sales
INNER JOIN Company
ON Company.ID = Sales.ID
WHERE Sales.Date <= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH);
My question: where should I use an index and why?
For this query:
SELECT MAX(s.Revenue - s.Costs), c.Name
FROM Sales s INNER JOIN
Company c
ON c.ID = s.ID
WHERE s.Date <= DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)
GROUP BY c.Name;
The optimal indexes are composite index (i.e. having more than one column): sales(date, id, revenue, costs)
and company(id, name)
. These indexes cover the query. That means that all the data is in the indexes, so the original data pages do not need to be accessed. As the query is structured, MySQL will still need to do an aggregation using a filesort.
Notes:
group by
so the query makes sense.If yours sales and company tables have a lot of data then you may need to create following indexes:-
Sales -> Sales.ID
Sales -> Sales.Date
Company -> Company.ID
You need these indexes as your are using these columns in joins and WHERE clauses. In case tables have large number of records absence of indexes will slow down your queries.