Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving MySQL query performance when using SUM
    primarykey
    data
    text
    <p>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:</p> <pre><code>mysql&gt; 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 &gt; 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) </code></pre> <p>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.)</p> <p>If I EXPLAIN the query, it's not using any indexes (<code>status</code> is indexed). (Is this because it's referring to the temporary table created for the SUMs?)</p> <pre><code>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 &gt; 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) </code></pre> <p>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?</p> <p>EDIT: <code>dt_submitted</code> was not indexed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload