IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> mysql8的with查询和自查询对比 -> 正文阅读

[大数据]mysql8的with查询和自查询对比

-- string转时间戳
select unix_timestamp('2021-10-13 15:15:00');
-- 时间转时间
select from_unixtime(1634109300);
-- date转时间戳
select unix_timestamp('2021-10-13 15:15:00');
-- string转时区(最后得到的还是string类型)
select convert_tz('2021-10-13 15:15:00', '+00:00', '-08:00');
-- date转时区(最后得到的还是date)
select convert_tz('2021-10-13 15:15:00', '+00:00', '-08:00');
-- 计算时间 (参与计算的两个值可以是 时间类型 时间格式的string类型)
select TIMESTAMPDIFF(MINUTE, '2021-10-13 15:15:00', now());
-- 计算时间 (参与计算的两个值可以是 时间类型 时间格式的string类型)
select datediff('2021-10-11 15:15:00', now());
-- date转string
select date_format(now(), '%Y-%m-%d %H:%i:%s');
-- string转date
select str_to_date('2021-10-11 15:15:00','%Y-%m-%d %H:%i:%s');


-- 延迟报警with查询的sql
WITH log_time_tab as (
    SELECT substr(time, 1, 16) AS time,
           glib_id,
           hostname,
           metric_item,
           metric_origin,
           max(metric_value)   as metric_value
    FROM t_dhc_metric_20211014
    WHERE metric_origin = 11
      and TIMESTAMPDIFF(MINUTE, time, now()) < 60
      and metric_item in ('sdk_heatbeat', 'online')
    GROUP BY substr(time, 1, 16),
             glib_id,
             hostname,
             metric_item,
             metric_origin
),
     db_time_tab as (
         SELECT substr(time, 1, 16) AS time,
                glib_id,
                hostname,
                metric_item,
                metric_origin,
                max(metric_value)   as metric_value
         FROM t_dhc_metric_20211014
         WHERE metric_origin = 12
           and TIMESTAMPDIFF(MINUTE, time, now()) < 60
           and metric_item in ('sdk_heatbeat', 'online')
         GROUP BY substr(time, 1, 16),
                  glib_id,
                  hostname,
                  metric_item,
                  metric_origin
     ),
     delay_log_tab as (
         SELECT t1.time,
                t1.glib_id,
                t1.hostname,
                t1.metric_item,
                COALESCE(t1.metric_value, 0)                                as metric_value1,
                COALESCE(t2.metric_value, 0)                                as metric_value2,
                COALESCE(t1.metric_value, 0) - COALESCE(t2.metric_value, 0) AS diff
         FROM db_time_tab t1
                  left join
              log_time_tab t2
              ON t1.time = t2.time
                  AND t1.glib_id = t2.glib_id
                  AND t1.hostname = t2.hostname
                  AND t1.metric_item = t2.metric_item
         where t1.metric_value - t2.metric_value > 2
     )

select *
from delay_log_tab;

-- 延迟报警子查询查询的sql
SELECT t1.time,
       t1.glib_id,
       t1.hostname,
       t1.metric_item,
       COALESCE(t1.metric_value, 0)                                as metric_value1,
       COALESCE(t2.metric_value, 0)                                as metric_value2,
       COALESCE(t1.metric_value, 0) - COALESCE(t2.metric_value, 0) AS diff
FROM (
         SELECT substr(time, 1, 16) AS time,
                glib_id,
                hostname,
                metric_item,
                metric_origin,
                max(metric_value)   as metric_value
         FROM t_dhc_metric_20211014
         WHERE metric_origin = 12
           and TIMESTAMPDIFF(MINUTE, time, now()) < 60
           and metric_item in ('sdk_heatbeat', 'online')
         GROUP BY substr(time, 1, 16),
                  glib_id,
                  hostname,
                  metric_item,
                  metric_origin
     ) t1
         LEFT JOIN (
    SELECT substr(time, 1, 16) AS time,
           glib_id,
           hostname,
           metric_item,
           metric_origin,
           max(metric_value)   as metric_value
    FROM t_dhc_metric_20211014
    WHERE metric_origin = 11
      and TIMESTAMPDIFF(MINUTE, time, now()) < 60
      and metric_item in ('sdk_heatbeat', 'online')
    GROUP BY substr(time, 1, 16),
             glib_id,
             hostname,
             metric_item,
             metric_origin
) t2
                   ON t1.time = t2.time
                       AND t1.glib_id = t2.glib_id
                       AND t1.hostname = t2.hostname
                       AND t1.metric_item = t2.metric_item
where t1.metric_value - t2.metric_value > 2;


-- 心跳with查询sql
with max_item_value_tab as (
    SELECT glib_id,
           hostname,
           metric_item,
           metric_origin,
           max(metric_value) as max_metric_value
    FROM t_dhc_metric_20211014
    WHERE metric_origin = 11
      and TIMESTAMPDIFF(MINUTE, time, now()) < 60
      and metric_item not in ('sdk_heatbeat', 'online')
    GROUP BY glib_id,
             hostname,
             metric_item,
             metric_origin
),
     max_time_tab as (
         select *
         from t_dhc_metric_20211014
         where metric_origin = 11
           and TIMESTAMPDIFF(MINUTE, time, now()) < 60
           and metric_item not in ('sdk_heatbeat', 'online')
     ),
     max_data_tab as (
         select a.glib_id,
                a.hostname,
                a.metric_item,
                a.metric_origin,
                a.max_metric_value,
                b.metric_value,
                b.time
         from max_item_value_tab as a
                  left join
              max_time_tab as b
              on a.glib_id = b.glib_id and
                 a.hostname = b.hostname and
                 a.metric_item = b.metric_item and
                 a.metric_origin = b.metric_origin and
                 a.max_metric_value = b.metric_value
     ),
     distinct_max_time_tab as (
         select glib_id,
                hostname,
                metric_item,
                metric_origin,
                max_metric_value,
                metric_value,
                max(time) as time
         from max_data_tab
         group by glib_id,
                  hostname,
                  metric_item,
                  metric_origin,
                  max_metric_value,
                  metric_value
     ),
     heartbeat_tab as (
         select glib_id,
                hostname,
                metric_item,
                metric_origin,
                max_metric_value,
                metric_value,
                time
         from distinct_max_time_tab
         where unix_timestamp(time) - max_metric_value > 2
     )
select *
from heartbeat_tab;


-- 心跳子查询sql
select glib_id,
       hostname,
       metric_item,
       metric_origin,
       max_metric_value,
       metric_value,
       time
from (
         select glib_id,
                hostname,
                metric_item,
                metric_origin,
                max_metric_value,
                metric_value,
                max(time) as time
         from (
                  select a.glib_id,
                         a.hostname,
                         a.metric_item,
                         a.metric_origin,
                         a.max_metric_value,
                         b.metric_value,
                         b.time
                  from (
                           SELECT glib_id,
                                  hostname,
                                  metric_item,
                                  metric_origin,
                                  max(metric_value) as max_metric_value
                           FROM t_dhc_metric_20211013
                           WHERE metric_origin = 11
                             and TIMESTAMPDIFF(MINUTE, time, now()) < 60
                             and metric_item not in ('sdk_heatbeat', 'online')
                           GROUP BY glib_id,
                                    hostname,
                                    metric_item,
                                    metric_origin
                       ) a
                           left join
                       (select *
                        from t_dhc_metric_20211013
                        where metric_origin = 11
                          and TIMESTAMPDIFF(MINUTE, time, now()) < 60
                          and metric_item not in ('sdk_heatbeat', 'online')) b
                       on a.glib_id = b.glib_id and
                          a.hostname = b.hostname and
                          a.metric_item = b.metric_item and
                          a.metric_origin = b.metric_origin and
                          a.max_metric_value = b.metric_value
              ) as b2
         group by glib_id,
                  hostname,
                  metric_item,
                  metric_origin,
                  max_metric_value,
                  metric_value) as d
where unix_timestamp(time) - max_metric_value > 2;











  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-10-15 11:51:47  更:2021-10-15 11:53:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 1:11:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码