-- OpenTSDB P99 延迟周报 SQL(Hive)
-- 运行时间:每周一 02:00
-- 参数:${bizdate} 取上周日 yyyyMMdd,由调度平台传入
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
-- 1. 全局 P99
select
'global' as dimension,
'all' as user,
round(percentile_approx(latency_ms, 0.99, 10000)/1000, 2) as p99_sec
from opentsdb_query_log
where dt between date_format(date_sub(to_date(from_unixtime(unix_timestamp('${bizdate}','yyyyMMdd'))), 6), 'yyyyMMdd')
and '${bizdate}'
and status = 200
and latency_ms between 1 and 300000;
-- 2. 分业务线 P99 排行
select
'user' as dimension,
user,
round(percentile_approx(latency_ms, 0.99, 10000)/1000, 2) as p99_sec
from opentsdb_query_log
where dt between date_format(date_sub(to_date(from_unixtime(unix_timestamp('${bizdate}','yyyyMMdd'))), 6), 'yyyyMMdd')
and '${bizdate}'
and status = 200
and latency_ms between 1 and 300000
and user != 'opentsdb_monitor'
group by user
order by p99_sec desc
limit 50;