SLS 内置了 compare 函数(内部转化为 join 统计分析),可以轻松实现 PV/UV 的同环比分析。实现环比监控的 SQL 示例如下:
* | select diff[1] as today, round((diff[3]-1.0)*100, 2) as growth
from (
select compare(pv, 86400) as diff
from (select count(distinct remote_addr) as pv from log)
)
其中 86400 表示与前一天(86400 秒)进行对比。实现同比监控的 SQL 则需要按时间粒度分组:
* | select t, diff[1] as today, diff[2] as yestoday, diff[3] as percentage
from (
select t, compare(pv, 86400) as diff
from (
select count(1) as pv, date_format(from_unixtime(__time__), '%H:%i') as t
from log group by t
) group by t order by t
)
这些查询结果可以直接配合 SLS 仪表盘功能制作业务监控大盘和运营报表。