ClickHouse常用SQL

今日PV

  1. with (
  2. select count(1) from ingress_stdout where toDate(_time_second_) = addDays(toDate(now()), -1)
  3. ) as yesterday_pv
  4. select yesterday_pv, count(1) as today_pv, round((today_pv - yesterday_pv) / yesterday_pv, 2) as diff
  5. from ingress_stdout
  6. where toDate(_time_second_) = toDate(now());

img.png

流入流出

  1. select concat(formatDateTime(_time_second_,'%Y-%m-%d %H'),':00') as hour ,
  2. SUM(if(body_bytes_sent is null, 0, body_bytes_sent)) as net_out,
  3. SUM(if(request_length is null, 0, request_length)) as net_in
  4. from ingress_stdout
  5. where toDate(_time_second_) = toDate(now())
  6. group by hour
  7. order by hour limit 24;

img.png

TOP10 访问path

  1. select path(url) as _path, count(1) as pv
  2. from ingress_stdout
  3. where _time_second_ >= addMinutes(now(),-15)
  4. and _path is not null
  5. group by _path
  6. order by pv desc limit 10;

img.png

PV趋势同比昨日(按时段)

  1. select today._hour as hour,
  2. yesterday.pv as yesterday_pv,
  3. today.pv as today_pv,
  4. round((today_pv - yesterday_pv) / yesterday_pv,2) as diff
  5. from
  6. (select count(1) as pv, formatDateTime(_time_second_,'%H') as _hour from ingress_stdout where toDate(_time_second_) = toDate(now()) group by _hour) today
  7. left join
  8. (select count(1) pv, formatDateTime(_time_second_,'%H') as _hour from ingress_stdout where toDate(_time_second_) = addDays(toDate(now()), -1) group by _hour) yesterday
  9. on today._hour=yesterday._hour
  10. order by today._hour
  11. ;

img.png

PV、UV展示

  1. select
  2. concat(formatDateTime(_time_second_,'%Y-%m-%d %H'),':00') as hour,
  3. count(1) as pv,
  4. count(distinct x_forward_for) as uv
  5. from ingress_stdout
  6. where toDate(_time_second_) = toDate(now())
  7. group by hour
  8. order by hour
  9. ;

img.png

请求状态展示

  1. select concat(formatDateTime(_time_second_,'%Y-%m-%d %H'),':00') as hour,
  2. count(1) as pv,
  3. status
  4. from ingress_stdout
  5. where toDate(_time_second_) = toDate(now())
  6. and status is not null
  7. group by hour,status
  8. order by hour;

img.png

请求数和各状态码比例

  1. with (
  2. select count(1) from ingress_stdout where toDate(_time_second_) = addDays(toDate(now()), -1) and status >= 400
  3. ) as yesterday
  4. select
  5. yesterday as yesterday_cnt,
  6. count(if(status >= 400, 1, null)) as today_cnt,
  7. count(1) as today_total,
  8. if(yesterday_cnt=0,0,round((today_cnt-yesterday_cnt)/yesterday_cnt, 2)) as error_diff,
  9. round(today_cnt/today_total, 5) as error_percent,
  10. round(sum(if(status >= 200 and status < 300, 1, 0)) / today_total, 5) as "2xx_percent",
  11. round(sum(if(status >= 300 and status < 400, 1, 0)) / today_total, 5) as "3xx_percent",
  12. round(sum(if(status >= 400 and status < 500, 1, 0)) / today_total, 5) as "4xx_percent",
  13. round(sum(if(status >= 500 and status < 600, 1, 0)) / today_total, 5) as "5xx_percent"
  14. from ingress_stdout
  15. where toDate(_time_second_) = toDate(now())
  16. and status is not null

img.png