MySQL performance problem using indexed datetime column
I have three suggestions
SUGGESTION #1 : Rewrite the query
You should rewrite the query as follows
SELECT http,
COUNT( http ) AS count
FROM reqs
WHERE date >= ( DATE(NOW() - INTERVAL 1 DAY) + INTERVAL 0 SECOND )
GROUP BY http
ORDER BY count;
or
SELECT * FROM
(
SELECT http,
COUNT( http ) AS count
FROM reqs
WHERE date >= ( DATE(NOW() - INTERVAL 1 DAY) + INTERVAL 0 SECOND )
GROUP BY http
) A ORDER BY count;
The WHERE should not have a function on both sides of the equal sign. Having date on the left side of the equals sign makes it easier for the Query Optimizer to use an index against it.
SUGGESTION #2 : Supporting Index
I would also suggest a different index
ALTER TABLE reqs ADD INDEX date_http_ndx (date,http); -- not (http,date)
I suggest this order of columns because the date
entries would all be contiguous in the index. Then, the query simply collects http
values without skipping gaps in http
.
SUGGESTION #3 : Bigger Key Buffer (Optional)
MyISAM only uses index caching. Since the query should not touch the .MYD
file, you should use a slightly bigger MyISAM Key Buffer.
To set it to 256M
SET @newsize = 1024 * 1024 * 256;
SET GLOBAL key_buffer_size = @newsize;
Then, set it in my.cnf
[mysqld]
key_buffer_size = 256M
Restart of MySQL not required