你好,有一个像这样的表:
DateTime Ip
2016-12-21 17:00:01 | 127.0.0.1
2016-12-21 17:00:01 | 127.0.0.1
2016-12-21 17:00:03 | 127.0.0.1
2016-12-21 17:00:05 | 127.0.0.2
2016-12-21 17:00:06 | 127.0.0.2
2016-12-21 17:00:06 | 127.0.0.1
2016-12-21 17:00:07 | 127.0.0.2
2016-12-21 17:00:08 | 127.0.0.2
2016-12-21 17:00:08 | 127.0.0.1
2016-12-21 17:00:08 | 127.0.0.1
目前在一段时间内计算每秒ip的请求,例如我做的5s:
SELECT Ip, total/diff_in_secs as Rps FROM (
SELECT Ip, count(*) as total, MAX(DateTime), MIN(DateTime), TIMESTAMPDIFF(SECOND, MIN(DateTime), MAX(DateTime)) as diff_in_secs
FROM requests WHERE DateTime >= '2016-12-21 17:00:01' AND DateTime <= '2016-12-21 17:00:05'
GROUP BY Ip
) as base
ORDER BY Rps Desc
我试图找出一种方法来在这段时间内做出每个ip的差异和标准差,从rps的任何想法?我试图在这个答案中应用这些概念,但收效甚微Calculate average, variance and standard deviation of two numbers in two different rows/columns with sql / PHP on specific dates
谢谢
答案 0 :(得分:1)
试试这个
select
requests_per_sec.ip,
avg(requests_per_sec.n) as Avg_Req_Per_Sec,
std(requests_per_sec.n) as Std_Req_Per_Sec
from
(select ip, count(ip) as n, datetime
from requests
group by datetime, ip) requests_per_sec
group by requests_per_sec.ip
要减少内部选择的大小,您还可以使用UNIX_TIMESTAMP(datetime) div 5
这样每隔5秒计算一次请求
select
requests_per_sec.ip,
avg(requests_per_sec.n) as Avg_Req_Per_Sec,
std(requests_per_sec.n) as Std_Req_Per_Sec
from
(select ip, count(ip) as n, datetime
from requests
group by UNIX_TIMESTAMP(datetime) div 5, ip) requests_per_sec
group by requests_per_sec.ip