9

mysql 如何按时间维度每 5s 统计一次数据呢?

 2 years ago
source link: https://www.v2ex.com/t/803372
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
mysql 如何按时间维度每 5s 统计一次数据呢?

V2EX  ›  MySQL

mysql 如何按时间维度每 5s 统计一次数据呢?

  Geekerstar · 2 小时 19 分钟前 · 523 次点击

有个表有创建时间字段,按照这个字段,每隔 5 秒统计一次这期间的数据增量,如果没有新增,则为 0,这种 SQL 怎么写呢?

11 条回复    2021-09-22 16:15:37 +08:00

myd

myd   2 小时 14 分钟前

select count(*) from xxx group by floor(时间戳 /5)

Geekerstar

Geekerstar   2 小时 4 分钟前

@myd

2021-09-22 13:45:29 1
2021-09-22 13:45:30 2
2021-09-22 13:46:00 4
2021-09-22 13:46:07 1
2021-09-22 13:46:30 2

不是每秒钟都有数据,有没有办法能整齐的每五秒分割一次来进行统计呢?

myd

myd   1 小时 53 分钟前

@Geekerstar 在程序中补齐,不一定要在数据库中实现。

deltadawn

deltadawn   1 小时 46 分钟前

INTERVAL()

Pipecraft

Pipecraft   35 分钟前

试了一下,使用 `DATE_ADD(created_at, INTERVAL - SECOND(created_at) % 5 SECOND)` 可以按照每分钟的 0,5,10.. 秒为单位统计。没有数据的时间段需要在程序补齐。

由于无法贴出代码,我把完整的 SQL 贴在了这里 pzwd.net/d/31/2

wellsc

wellsc   28 分钟前

用 Prometheus 吧

adgad2

adgad2   21 分钟前

遇到个类似需求,最后建表写个定时任务完事

haochen2

haochen2   11 分钟前

这种统计的还是多余建个表来存储吧

Geekerstar

Geekerstar   8 分钟前

@myd 嗯嗯,在程序中处理了

关于   ·   帮助文档   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   3688 人在线   最高记录 5497   ·  

创意工作者们的社区

World is powered by solitude

VERSION: 3.9.8.5 · 19ms · UTC 08:22 · PVG 16:22 · LAX 01:22 · JFK 04:22
♥ Do have faith in what you're doing.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK