I have a table of user-generated content that includes an ENUM format column for status. (Pending, Approved, Approved-auto, or Rejected) To keep en eye on the amount of recent content by status, I use the following query:
mysql> SELECT DATE(dt_submitted) AS date,
COUNT(*) AS count,
SUM(IF(status='Approved', 1, 0)) as approved,
SUM(IF(status='Approved-auto', 1, 0)) as approved_auto,
SUM(IF(status='Rejected', 1, 0)) as rejected,
SUM(IF(status='Pending', 1, 0)) as pending
FROM post
WHERE dt_submitted > DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY date;
+------------+-------+----------+---------------+----------+---------+
| date | count | approved | approved_auto | rejected | pending |
+------------+-------+----------+---------------+----------+---------+
| 2011-11-22 | 131 | 124 | 0 | 7 | 0 |
| 2011-11-23 | 116 | 114 | 0 | 2 | 0 |
...
| 2011-12-21 | 690 | 674 | 5 | 11 | 0 |
| 2011-12-22 | 80 | 75 | 0 | 4 | 38 |
+------------+-------+----------+---------------+----------+---------+
31 rows in set (0.60 sec)
This is nearly perfect, but I’m picky and want to see if I can make it faster. (0.6 sec is slow on this server, and the table changes too often to worry about passing a static date to cache results.)
If I EXPLAIN the query, it’s not using any indexes (status is indexed). (Is this because it’s referring to the temporary table created for the SUMs?)
explain SELECT DATE(dt_submitted) AS date, COUNT(*) AS count, SUM(IF(status='Approved', 1, 0)) as approved, SUM(IF(status='Approved-auto', 1, 0)) as approved_auto, SUM(IF(status='Rejected', 1, 0)) as rejected, SUM(IF(status='Pending', 1, 0)) as pending FROM post WHERE dt_submitted > DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY date;
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
| 1 | SIMPLE | post | ALL | NULL | NULL | NULL | NULL | 529902 | Using where; Using temporary; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+--------+----------------------------------------------+
1 row in set (0.00 sec)
So is there anything I can do to optimize the table or rewrite the query to make this faster? Or is this query simply limited by the speed of available system resources?
EDIT: dt_submitted was not indexed.
Double check that the appropriate columns are indexed.
(As noted in the edit, I failed to double-check my indexes. Once I indexed the appropriate column, the problem was solved.)