简体   繁体   English

使用什么索引或其他数据库来加快聚合查询的速度

[英]What index or other database to use to speed this aggregating query up

I have stored a time series in a MySQL table [int(11) int(11)] The first column is epoch time in milliseconds, the second column is the value at that time. 我已将时间序列存储在MySQL表中[int(11)int(11)]第一列是以毫秒为单位的纪元时间,第二列是当时的值。

There are 25920000 records. 有25920000条记录。 When I want to plot the data, I don't want to plot every millisecond but only at a certain resolution, I use the following query: 当我想绘制数据时,我不想每毫秒绘制一次,而只是以某个分辨率绘制,我使用以下查询:

Average per day 每天平均

 SELECT AVG(value) FROM measurements GROUP BY ts DIV (1000*60*60*24)

This takes already 1,5 minutes which is way longer than I want to achieve. 这已经花费了1.5分钟,比我想要的要长。

Is there a certain index I might be able to use to speed up this query? 是否可以使用某个索引来加速此查询?

Or is there maybe another DBMS that is better suited for this. 或者,也许还有另一个更适合于此的DBMS。

as requested in comments: 根据评论中的要求:

Original CREATE TABLE 原始创建表

CREATE TABLE measurements (ts INT(11), value INT(11))

Sample Values 样本值

15151,11
15152,15
15153,50
15154,100
....

Note that I'm currently experimenting with integer data, in the future it will be floating point data 请注意,我目前正在实验整数数据,将来它将是浮点数据

You can also use PERSISTENT Columns that will be generate the date on the fly and also has an index 您还可以使用PERSISTENT列,这些列将即时生成日期并具有索引

CREATE TABLE `measurements ` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `datum` date AS (DATE(ts)) PERSISTENT,
  `ts` int(11) DEFAULT NULL,
  `value` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `datum` (`datum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

to speed up things (and to make sure an index might be used successful) you could add different columns for your resolutions and precalculate those from the timestamp value. 为了加快处理速度(并确保可以成功使用索引),您可以为分辨率添加不同的列,并根据时间戳值对其进行预先计算。 After that, add indexes to this (these) columns and it should be faster since you do not have to calculate all the values before grouping with it. 之后,将索引添加到此(这些)列,它应该更快,因为您不必在与之分组之前计算所有值。

i know its not the best flexible way but probably a good tradeoff. 我知道这不是最好的灵活方式,但可能是一个很好的权衡。

Edit: If you fill this table you can also precalculate those values right off, so no need to recalculate lots of data. 编辑:如果您填写此表,则还可以立即预先计算这些值,因此无需重新计算大量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM