在mysql上通过+ SUM查询为组编制索引

时间:2011-08-26 14:08:42

标签: 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_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;使用临时;使用filesort”。提前谢谢。

4 个答案:

答案 0 :(得分:0)

  1. 您按列排序没有索引,因此您获取信息使用临时;使用filesort
  2. 使用地点; - 使用带或不带索引的地方搜索where子句。

答案 1 :(得分:0)

您使用排序 - 'ORDER BY sTotal DESC',因此sTotal字段上的索引可能有所帮助。

答案 2 :(得分:0)

尝试不同的KEY组合(nGramnGramWordtotal)。

答案 3 :(得分:0)

为什么不使用EXPLAIN获取有关效果的有用信息,并优化您的查询?!

相关问题