[英]mysql query - optimizing existing MAX-MIN query for a huge table
I have a more or less good working query (concerning to the result) but it takes about 45seconds to be processed. 我有一个或多或少良好的工作查询(关于结果),但大约需要45秒的时间来处理。 That's definitely too long for presenting the data in a GUI. 对于在GUI中显示数据绝对太长了。
So my demand is to find a much faster/efficient query (something around a few milliseconds would be nice) My data table has something around
3000
~2,619,395 entries and is still growing. 所以,我的要求是找一个更快/有效的查询(大约几毫秒的东西就好了)我的数据表中有大约
3000〜2619395
项的东西,还在不断增加。
Schema: 架构:
num | station | fetchDate | exportValue | error
1 | PS1 | 2010-10-01 07:05:17 | 300 | 0
2 | PS2 | 2010-10-01 07:05:19 | 297 | 0
923 | PS1 | 2011-11-13 14:45:47 | 82771 | 0
Explanation 说明
Working query: 工作查询:
select
YEAR(fetchDate), station, Max(exportValue)-MIN(exportValue)
from
registros
where
exportValue > 0 and error = 0
group
by station, YEAR(fetchDate)
order
by YEAR(fetchDate), station
Output: 输出:
Year | station | Max-Min
2008 | PS1 | 24012
2008 | PS2 | 23709
2009 | PS1 | 28102
2009 | PS2 | 25098
My thoughts on it: 我对此的看法:
Additional Info: 附加信息:
I'm using the query in a JAVA Application. 我在JAVA应用程序中使用查询。 That means, it would be possible to do some post-processing on the resultset if necessary. 这意味着,如有必要,可以对结果集进行一些后处理。 (JPA 2.0) (JPA 2.0)
Any help/approaches/ideas are very appreciated. 任何帮助/方法/想法都非常感谢。 Thanks in advance. 提前致谢。
Adding suitable indexes will help. 添加合适的索引会有所帮助。 2 compound indexes will speed things up significantly: 2个复合索引将显着加快处理速度:
ALTER TABLE tbl_name ADD INDEX (error, exportValue);
ALTER TABLE tbl_name ADD INDEX (station, fetchDate);
This query running on 3000 records should be extremely fast. 在3000条记录上运行的此查询应该非常快。
Suggestions: 意见建议:
Since your table is rapidly growing (every 15 minutes) You should take the last suggestion into account. 由于您的桌子正在快速增长(每隔15分钟),因此您应该考虑最后一个建议。 Probably, there is no need to keep detailed history at one place. 可能不需要将详细的历史记录保存在一个地方。 Archiving data is process that should be done as part of maintenance. 归档数据是应在维护过程中完成的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.