【发布时间】:2011-08-26 14:08:41
【问题描述】:
我有一个具有以下架构的表:
CREATE TABLE `wordtrend` (
`oid` bigint(20) NOT NULL AUTO_INCREMENT,
`monitorId` bigint(20) DEFAULT NULL,
`nGram` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nGramWord` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`negatives` bigint(20) DEFAULT NULL,
`neutrals` bigint(20) DEFAULT NULL,
`positives` bigint(20) DEFAULT NULL,
`total` bigint(20) DEFAULT NULL,
`trendCut` datetime DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=MyISAM
AUTO_INCREMENT=358539
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
是否可以创建索引以有效地运行以下查询?
SELECT nGram
, nGramWord
, SUM(total) AS sTotal
, SUM(positives)
, SUM(negatives)
, SUM(neutrals)
FROM WordTrend
WHERE monitorId = 21751021
AND trendCut >= '2011-01-01 00:00:00'
GROUP BY nGram
, nGramWord
ORDER BY sTotal DESC
我们已经尝试了以下方法:
KEY `RollupIndex` (`monitorId`,`trendCut`)
KEY `RollupIndex2` (`monitorId`,`nGram`,`trendCut`)
但我们在额外的列上得到“使用 where;使用临时;使用文件排序”。提前致谢。
【问题讨论】:
-
查询返回了多少行?运行需要多少时间?
-
我很好奇这两个查询会显示什么:
SELECT COUNT(DISTINCT nGram) FROM WordTrend和SELECT COUNT(DISTINCT nGram, nGramWord) FROM WordTrend -
(警告:如果没有
(nGram,nGramWord)上的索引,它们可能真的很慢