我有folowwing SQL查询的MySQL组由非常缓慢
SELECT CustomerID FROM sales WHERE `Date` <= '2012-01-01' GROUP BY CustomerID
该查询超过1140万行执行和运行速度非常慢。它需要3分多钟才能执行。如果我删除组,部分会在1秒以内运行。这是为什么?
MySQL服务器的版本是 '5.0.21社区-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
你可以发表表架构(创建表的脚本) – 2012-04-23 10:34:31
不知道你是否发布了实际的查询。但是在这个查询中,如果没有分组函数,那么'GROUP BY'需要什么? – 2012-04-23 10:38:54
Aziz,我需要返回customerID的唯一值 – Treach 2012-04-23 10:44:43