Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL 1 millon row query speed
    primarykey
    data
    text
    <p>I'm having trouble getting a decent query time out of a large MySQL table, currently its taking over 20 seconds. The problem lies in the GROUP BY as MySQL needs to run a filesort but I don't see how I can get around this</p> <p><strong>QUERY</strong>:</p> <pre><code>SELECT play_date, COUNT(DISTINCT(email)) AS count FROM log WHERE type = 'play' AND play_date BETWEEN '2009-02-23' AND '2009-02-24' GROUP BY play_date ORDER BY play_date desc </code></pre> <p><strong>EXPLAIN</strong>:</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE log ALL type,type_2 NULL NULL NULL 530892 Using where; Using filesort </code></pre> <p><strong>TABLE STRUCTURE</strong></p> <pre><code>CREATE TABLE IF NOT EXISTS `log` ( `id` int(11) NOT NULL auto_increment, `email` varchar(255) NOT NULL, `type` enum('played','reg','friend') NOT NULL, `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP, `play_date` date NOT NULL, `email_refer` varchar(255) NOT NULL, `remote_addr` varchar(15) NOT NULL, PRIMARY KEY (`id`), KEY `email` (`email`), KEY `type` (`type`), KEY `email_refer` (`email_refer`), KEY `type_2` (`type`,`timestamp`,`play_date`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=707859 ; </code></pre> <p>If anyone knows how I could improve the speed I would be very greatful</p> <p>Tom</p> <p><strong><em>EDIT</em></strong></p> <p>I've added the new index with just play_date and type but MySQL refuses to use it</p> <pre><code>id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE log ALL play_date NULL NULL NULL 801647 Using where; Using filesort </code></pre> <p>This index was created using ALTER TABLE <code>log</code> ADD INDEX (<code>type</code>, <code>play_date</code>);</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