mysql上分组+求和查询的索引
我有一个具有以下架构的表:mysql上分组+求和查询的索引,mysql,indexing,group-by,Mysql,Indexing,Group By,我有一个具有以下架构的表: 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_unico
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`)
但我们在哪里使用;使用临时设备;在额外列上使用filesort。提前谢谢
您按列排序,不带索引,因此您可以使用临时索引获取信息;使用文件排序
使用where;-在where子句中使用带索引或不带索引的where进行搜索。
您使用的是ordering—“ORDER BY sTotal DESC”,因此sTotal字段上的索引可能会有所帮助。尝试不同的nGram键、nGramWord键和total键组合。为什么不使用来获取有关性能的有用信息,并优化您的查询 sTotal是SUMtotal列的名称。我认为您不能索引sTotal,对吗?查询返回了多少行?运行需要多长时间?我很好奇这两个查询会显示什么:从WordTrend中选择COUNTDISTINCT nGram,从WordTrend中选择COUNTDISTINCT nGram,nGramWord警告:如果没有nGram,nGramWord上的索引,它们可能会非常慢