mysql查询与group by的性能问题

时间:2011-02-01 14:13:09

标签: mysql query-optimization

我遇到了性能问题。我有一个名为Alarmes的表,有20列。在此表中,我只对2列(ALM_TAGNAME和ALM_LOGNODENAME)感兴趣。该表是从另一个程序自动创建和更新的,我只想进行统计。 好吧,我在两列ALM_TAGNAME和ALM_LOGNODENAME上添加了一个键,但即使使用此键,select也会使用超过40秒来执行。我只是减少了行数,但它仍然超过100万行。

SELECT ALM_TAGNAME as 'tag', count(*) as  'cnt'    FROM Alarmes WHERE ALM_LOGNODENAME='plantid' GROUP BY ALM_TAGNAME; 
  

创建表Alarmes
  ALM_NATIVETIMEIN datetime NOT NULL,   ALM_NATIVETIMELAST日期时间默认值   NULL,ALM_LOGNODENAME varchar(50)   字符集utf8默认为NULL,
  ALM_PHYSLNODE varchar(50)字符   设置utf8默认为NULL,ALM_TAGNAME   varchar(50)字符集utf8默认值   NULL,ALM_TAGDESC varchar(50)   字符集utf8默认为NULL,
  ALM_VALUE varchar(50)字符集   utf8默认为NULL,ALM_UNIT   varchar(50)字符集utf8默认值   NULL,ALM_MSGTYPE varchar(50)   字符集utf8默认为NULL,
  ALM_DESCR varchar(255)字符集   utf8默认为NULL,ALM_ALMSTATUS   varchar(50)字符集utf8默认值   NULL,ALM_ALMPRIORITY varchar(50)   字符集utf8默认为NULL,
  ALM_ALMAREA varchar(50)字符   设置utf8默认为NULL,
  ALM_ALMEXTFLD1 varchar(50)字符   设置utf8默认为NULL,
  ALM_ALMEXTFLD2 varchar(50)字符   设置utf8默认为NULL,ALM_OPNAME   varchar(50)字符集utf8默认值   NULL,ALM_OPFULLNAME varchar(50)   字符集utf8默认为NULL,
  ALM_OPNODE varchar(50)字符集   utf8默认为NULL,ALM_PERFNAME   varchar(50)字符集utf8默认值   NULL,ALM_PERFFULLNAME varchar(50)   字符集utf8默认为NULL,
  ALM_PERFBYCOMMENT varchar(50)   字符集utf8默认为NULL,
  ALM_VERNAME varchar(50)字符   设置utf8默认为NULL,
  ALM_VERFULLNAME varchar(50)   字符集utf8默认为NULL,
  ALM_VERBYCOMMENT varchar(50)   字符集utf8默认为NULL,
  ALM_DATEIN varchar(50)字符集   utf8默认为NULL,ALM_TIMEIN   varchar(50)字符集utf8默认值   NULL,ALM_DATELAST varchar(50)   字符集utf8默认为NULL,
  ALM_TIMELAST varchar(50)字符   设置utf8默认为NULL,KEY   Alarmes_tagnameALM_TAGNAME),
  KEY Alarmes_lognodname   (ALM_LOGNODENAME))

EXPLAIN SELECT ALM_TAGNAME as 'tag', count(ALM_TAGNAME) as  'cnt'    FROM monitor.Alarmes   WHERE ALM_LOGNODENAME='bogota'   GROUP BY ALM_TAGNAME;    +----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+
| id | select_type | table   | type | possible_keys      | key                | key_len | ref   | rows  | Extra                                        |
+----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+
|  1 | SIMPLE      | Alarmes | ref  | Alarmes_lognodname | Alarmes_lognodname | 153     | const | 86143 | Using where; Using temporary; Using filesort |
+----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+

 DESCRIBE SELECT ALM_TAGNAME as 'tag', count(ALM_TAGNAME) as  'cnt'    FROM monitor.Alarmes   WHERE ALM_LOGNODENAME='bogota'   GROUP BY ALM_TAGNAME;
+----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+
| id | select_type | table   | type | possible_keys      | key                | key_len | ref   | rows  | Extra                                        |
+----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+
|  1 | SIMPLE      | Alarmes | ref  | Alarmes_lognodname | Alarmes_lognodname | 153     | const | 86154 | Using where; Using temporary; Using filesort |
+----+-------------+---------+------+--------------------+--------------------+---------+-------+-------+----------------------------------------------+

2 个答案:

答案 0 :(得分:1)

如果您说“嗯,我在ALM_TAGNAME和ALM_LOGNODENAME两栏上添加了一个键”,您是否在ALM_TAGNAME + ALM_LOGNODENAME或两个索引上添加 INDEX

如果您添加ALM_TAGNAME + ALM_LOGNODENAME,请尝试在ALM_TAGNAME上添加一个索引

答案 1 :(得分:0)

数据在您需要COUNT(*)的位置变化的频率...,您是否考虑过每晚(或甚至每小时)预聚合表来从THAT表中获取您的查询/计数..然后,您的查询标记名称/日志节点名称上的THAT表将是一条记录并完成..