Skip to content
Advertisement

Improving the performance of a MySQL left join sub query

I have the following MySQL query which calculates the total number of orders for each month within a given date range e.g. a year. The query works correctly, but the performance is slow (around 250ms).

Any ideas on how to rewrite it to make it more efficient?

UPDATE: Some have suggested using two left joins, however if I do that then the country_id filter is not applied:

Thanks!

Advertisement

Answer

After much tinkering, I produced the following which operates in under 40ms, which is good enough for my needs. I still think it’s not ideal and would welcome any improvements…

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement