【mysql 视图】10分钟教你每天自动更新视图

MySQL 视图是一个强大的工具,可以简化复杂的查询操作,并且保护敏感数据。在 MySQL 中,可以通过 CREATE VIEW 语句创建视图,在查询中使用视图,并且可以使用 DROP VIEW 语句删除视图。需要注意的是,MySQL 视图通常是只读的。
假设我有如下语句,需要给下面语句创建视图,并自动每天更新,这样每次查询视图看到的就是最新的结果了。

select  
substr(create_time,1,10) as 'cjsj',
ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1'  then calciner_temperature  end),3),0) as 'calciner_temperature_1',
ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature  end),3),0) as 'calciner_temperature_0',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then two_temperature end),3),0) as 'two_temperature_1',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end),3),0) as 'two_temperature_0',
ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1'  then yao_tou_pressure end),3),0) as 'yao_tou_pressure_1',
ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end),3),0) as 'yao_tou_pressure_0',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then f2_p  end),3),0) as 'f2_p_1',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end),3),0) as 'f2_p_0',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then shu_liao_temperature  end),3),0) as 'shu_liao_temperature_1',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end),3),0) as 'shu_liao_temperature_0',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then aqc_temperature  end),3),0) as 'aqc_temperature_1',
ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature  end),3),0) as 'aqc_temperature_0',
ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1'  then fcao    end),3),0) as 'fcao_1',
ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end),3),0) as 'fcao_0'
from kiln_system_dcs_switch
group  by substr(create_time,1,10)

这个 SQL 查询语句的作用是查询 kiln_system_dcs_switch 表中指定时间段内(可通过 WHERE 子句控制)的各项数据的标准差,结果按照日期分组展示。如果需要自动更新查询结果,可以使用事件调度器(Event Scheduler)实现,定期执行该查询语句并将结果存储到指定表中。

可以使用以下 SQL 语句将上述查询结果创建为视图:

CREATE VIEW kiln_system_data_stddev AS
SELECT  
    substr(create_time,1,10) AS 'cjsj',
    ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1'  then calciner_temperature  end),3),0) as 'calciner_temperature_1',
    ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature  end),3),0) as 'calciner_temperature_0',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then two_temperature end),3),0) as 'two_temperature_1',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end),3),0) as 'two_temperature_0',
    ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1'  then yao_tou_pressure end),3),0) as 'yao_tou_pressure_1',
    ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end),3),0) as 'yao_tou_pressure_0',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then f2_p  end),3),0) as 'f2_p_1',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end),3),0) as 'f2_p_0',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then shu_liao_temperature  end),3),0) as 'shu_liao_temperature_1',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end),3),0) as 'shu_liao_temperature_0',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1'  then aqc_temperature  end),3),0) as 'aqc_temperature_1',
    ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature  end),3),0) as 'aqc_temperature_0',
    ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1'  then fcao    end),3),0) as 'fcao_1',
    ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end),3),0) as 'fcao_0'
FROM kiln_system_dcs_switch
GROUP BY substr(create_time,1,10);

这个 SQL 语句将查询结果以视图的形式保存到数据库中,方便后续查询和分析。视图名为 kiln_system_data_stddev,包含了和之前查询语句中相同的列和数据。创建视图后,可以直接通过以下 SQL 语句来查询视图内容:

SELECT * FROM kiln_system_data_stddev;

创建视图后,视图中的查询结果并不会自动更新,需要手动执行 SQL 查询语句或者使用事件调度器(Event Scheduler)来定期更新视图内容。

如果你需要每天自动更新视图内容,可以考虑使用事件调度器。例如,以下是一个每天凌晨 3:00 自动更新视图的例子:

CREATE EVENT update_kiln_system_data_stddev
ON SCHEDULE EVERY 1 DAY STARTS '2023-04-26 03:00:00'
DO
    REPLACE INTO kiln_system_data_stddev
    SELECT  
        substr(create_time,1,10) AS 'cjsj',
        ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '1' then calciner_temperature end), 3), 0) AS 'calciner_temperature_1',
        ifnull(round(STDDEV_SAMP(case tail_coal_dcs_switch when '0' then calciner_temperature end), 3), 0) AS 'calciner_temperature_0',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then two_temperature end), 3), 0) AS 'two_temperature_1',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then two_temperature end), 3), 0) AS 'two_temperature_0',
        ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '1' then yao_tou_pressure end), 3), 0) AS 'yao_tou_pressure_1',
        ifnull(round(STDDEV_SAMP(case tou_pai_dcs_switch when '0' then yao_tou_pressure end), 3), 0) AS 'yao_tou_pressure_0',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then f2_p end), 3), 0) AS 'f2_p_1',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then f2_p end), 3), 0) AS 'f2_p_0',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then shu_liao_temperature end), 3), 0) AS 'shu_liao_temperature_1',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then shu_liao_temperature end), 3), 0) AS 'shu_liao_temperature_0',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '1' then aqc_temperature end), 3), 0) AS 'aqc_temperature_1',
        ifnull(round(STDDEV_SAMP(case one_bisu_dcs_switch when '0' then aqc_temperature end), 3), 0) AS 'aqc_temperature_0',
        ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '1' then fcao end), 3), 0) AS 'fcao_1',
        ifnull(round(STDDEV_SAMP(case head_coal_dcs_switch when '0' then fcao end), 3), 0) AS 'fcao_0'
    FROM kiln_system_dcs_switch
    GROUP BY substr(create_time, 1, 10);

这个 SQL 语句创建了一个名为 update_kiln_system_data_stddev 的事件调度器,每天凌晨 3:00 执行一次查询语句,将结果更新到视图 kiln_system_data_stddev 中。需要注意的是,这个事件调度器的开始时间为 ‘2023-04-26 03:00:00’,需要根据实际情况进行修改。

创建好事件调度器后,可以执行以下 SQL 语句查看当前所有的事件调度器:

SHOW EVENTS;

如果需要停止某个事件调度器,可以使用以下 SQL 语句:

DROP EVENT update_kiln_system_data_stddev;

要让上面的事件调度器工作起来,首先得查看事件有没有开启,必须开启状态才能工作。
可以使用以下 SQL 语句查看 MySQL 中是否启用了事件调度器:

SELECT @@event_scheduler;

如果返回值为 ON,则表示事件调度器已经启用;如果返回值为 OFF,则表示事件调度器尚未启用。
如果返回值为 OFF,可以通过执行以下 SQL 语句来开启事件调度器:

扫描二维码关注公众号,回复: 15684989 查看本文章
SET GLOBAL event_scheduler = ON;

此时再执行 SELECT @@event_scheduler; 应该就能返回 ON 了。

猜你喜欢

转载自blog.csdn.net/u013421629/article/details/130379884
今日推荐