gpt4 book ai didi

mysql - 在 mysql 上为 group by + SUM 查询建立索引

转载 作者:行者123 更新时间:2023-11-30 23:36:09 25 4
gpt4 key购买 nike

我有一个具有以下架构的表:

    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`)

但是我们在额外的列上得到了“Using where; Using temporary; Using filesort”。提前致谢。

最佳答案

  1. 您按没有索引的列排序,因此您可以使用临时的方式获取信息;使用文件排序
  2. 在哪里使用; - 您可以使用带或不带索引的 where 来在 where 子句中进行搜索。

关于mysql - 在 mysql 上为 group by + SUM 查询建立索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7205905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com