It will take up to 4.5 sec on a fast server to make this query to be finished and output the result on a table with 400K rows
the output
Month Week Views
02 27 581
SELECT
DATE_FORMAT(`DataDateTime`, '%m') AS `Month`,
DATE_FORMAT(`DataDateTime`, '%d') AS `Day`,
COUNT(DISTINCT(`DataUserID`)) AS `Views`
FROM `la_data`
WHERE
`DataLayerName` = 'layar'
AND `DataDateTime` > ''
GROUP BY `Day`, `Month`
HAVING `Day` = 27 AND `Month` = 02
ORDER BY `Views` DESC
The HAVING clause is meant to be used to limit the results once the ORDER BY and
GROUP BY clauses have been processed. Since they are processed after the main query has taken place they can’t be optimized which is why the query is taking so long. As the Day and Month functions are not affected by either of these clauses, they should be in the WHERE clause instead. Try putting them in there instead (note you must use the full expressions in the WHERE):