mysql group by 慢_MySQL分组方式很慢

bd96500e110b49cbb3cd949968f18be7.png

I have the folowwing SQL query

SELECT CustomerID FROM sales WHERE `Date` <= '2012-01-01' GROUP BY CustomerID

The query is executed over 11400000 rows and runs very slow. It takes over 3 minutes to execute. If I remove the group-by part, this runs below 1 second. Why is that?

MySQL Server version is '5.0.21-community-nt'

Here is the table schema:

CREATE TABLE `sales` (

`ID` int(11) NOT NULL auto_increment,

`DocNo` int(11) default '0',

`CustomerID` int(11) default '0',

`OperatorID` int(11) default '0',

PRIMARY KEY (`ID`),

KEY `ID` (`ID`),

KEY `DocNo` (`DocNo`),

KEY `CustomerID` (`CustomerID`),

KEY `Date` (`Date`)

) ENGINE=MyISAM AUTO_INCREMENT=14946509 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

解决方案

Try putting an index on (Date,CustomerID).

Have a look at the mysql manual for optimizing group by queries:- Group by optimization

You can find out how mysql is generating the result if you use EXPLAIN as follows:-

EXPLAIN SELECT CustomerID FROM sales WHERE `Date` <= '2012-01-01' GROUP BY CustomerID

This will tell you which indexes (if any) mysql is using to optimize the query. This is very handy when learning which indexes work for which queries as you can try creating an index and see if mysql uses it. So even if you don't fully understand how mysql calculates aggregate queries you can create a useful index by trial and error.

你可能感兴趣的:(mysql,group,by,慢)