【发布时间】:2020-08-09 13:20:51
【问题描述】:
我有一张桌子,上面有某些日志及其响应时间。我可以通过以下方式查询表以获取最近 1000 条记录的平均响应时间:
SELECT timestamp, count(*), avg(response_time) FROM table ORDER BY timestamp DESC LIMIT 1000
-- timestamp count(*) avg(response_time)
-- 2020-03-17 11:58:37 1000 0.27
但是,我希望在过去每千条记录中获得 N 条记录(以查看响应时间随时间的进展,以 1000 个请求为单位),即:
SELECT timestamp, count(*), avg(response_time) FROM table ORDER BY timestamp DESC LIMIT 1000
UNION
SELECT timestamp, count(*), avg(response_time) FROM table ORDER BY timestamp DESC LIMIT 1000, 1000
UNION ...
-- timestamp count(*) avg(response_time)
-- 2020-03-17 11:58:37 1000 0.27
-- 2020-03-17 11:38:09 1000 0.52
-- 2020-03-17 11:04:11 1000 1.04
-- and keep going, in groups of 1000 records...
是否有一种更简洁的方法来做到这一点,我可以按 1000 个块对事物进行分组?
【问题讨论】:
标签: mysql sql date datetime group-by