mysql用视图和事件实现数据间隔插入数据到表
Posted idvcn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql用视图和事件实现数据间隔插入数据到表相关的知识,希望对你有一定的参考价值。
创建视图
CREATE ALGORITHM = UNDEFINED DEFINER = `root`@`192.168.*.*` SQL SECURITY DEFINER VIEW `perfdata` AS SELECT `nagios_hosts`.`display_name` AS `hostname`, `nagios_services`.`display_name` AS `servicename`, `nagios_servicechecks`.`end_time` AS `timeline`, `getPerfKey` ( `nagios_servicechecks`.`perfdata`, 1 ) AS `perfkey`, `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 1 ) AS `perfvalue` FROM ( ( `nagios_hosts` LEFT JOIN `nagios_services` ON ( ( `nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id` ) ) ) LEFT JOIN `nagios_servicechecks` ON ( ( `nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id` ) ) ) WHERE ( ( `nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE) AND now() ) AND `nagios_hosts`.`display_name` IN ( SELECT DISTINCT `nagios_hosts`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND `nagios_services`.`display_name` IN ( SELECT DISTINCT `nagios_services`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND ( `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 1 ) <> ‘‘ ) ) UNION SELECT `nagios_hosts`.`display_name` AS `hostname`, `nagios_services`.`display_name` AS `servicename`, `nagios_servicechecks`.`end_time` AS `timeline`, `getPerfKey` ( `nagios_servicechecks`.`perfdata`, 2 ) AS `perfkey`, `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 2 ) AS `perfvalue` FROM ( ( `nagios_hosts` LEFT JOIN `nagios_services` ON ( ( `nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id` ) ) ) LEFT JOIN `nagios_servicechecks` ON ( ( `nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id` ) ) ) WHERE ( ( `nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE) AND now() ) AND `nagios_hosts`.`display_name` IN ( SELECT DISTINCT `nagios_hosts`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND `nagios_services`.`display_name` IN ( SELECT DISTINCT `nagios_services`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND ( `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 2 ) <> ‘‘ ) ) UNION SELECT `nagios_hosts`.`display_name` AS `hostname`, `nagios_services`.`display_name` AS `servicename`, `nagios_servicechecks`.`end_time` AS `timeline`, `getPerfKey` ( `nagios_servicechecks`.`perfdata`, 3 ) AS `perfkey`, `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 3 ) AS `perfvalue` FROM ( ( `nagios_hosts` LEFT JOIN `nagios_services` ON ( ( `nagios_hosts`.`host_object_id` = `nagios_services`.`host_object_id` ) ) ) LEFT JOIN `nagios_servicechecks` ON ( ( `nagios_services`.`service_object_id` = `nagios_servicechecks`.`service_object_id` ) ) ) WHERE ( ( `nagios_servicechecks`.`end_time` BETWEEN (now() + INTERVAL -(6) MINUTE) AND now() ) AND `nagios_hosts`.`display_name` IN ( SELECT DISTINCT `nagios_hosts`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND `nagios_services`.`display_name` IN ( SELECT DISTINCT `nagios_services`.`display_name` FROM ( `nagios_services` LEFT JOIN `nagios_hosts` ON ( ( `nagios_services`.`host_object_id` = `nagios_hosts`.`host_object_id` ) ) ) ) AND ( `getPerfValue` ( `nagios_servicechecks`.`perfdata`, 3 ) <> ‘‘ ) )
创建表
CREATE TABLE `getPerfdata` ( `NO` bigint(20) NOT NULL AUTO_INCREMENT, `hostname` varchar(255) DEFAULT NULL, `servicename` varchar(255) DEFAULT NULL, `timeline` datetime DEFAULT NULL, `perfkey` varchar(255) DEFAULT NULL, `perfvalue` varchar(255) DEFAULT NULL, PRIMARY KEY (`NO`), KEY `NO` (`NO`) ) ENGINE=InnoDB AUTO_INCREMENT=16739 DEFAULT CHARSET=utf8;
打开全局事件调度
mysql> set global event_scheduler=on; Query OK, 0 rows affected (0.00 sec) mysql> select @@event_scheduler; +-------------------+ | @@event_scheduler | +-------------------+ | ON | +-------------------+ 1 row in set (0.00 sec
创建事件实现视图数据按照计划插入表
CREATE DEFINER = `root`@`192.168.*.*` EVENT `insertPerfdata` ON SCHEDULE EVERY 6 MINUTE STARTS ‘2019-01-29 11:24:16‘ ON COMPLETION PRESERVE ENABLE DO INSERT INTO nagios.getPerfdata ( hostname, servicename, timeline, perfkey, perfvalue ) SELECT * FROM nagios.perfdata
以上是关于mysql用视图和事件实现数据间隔插入数据到表的主要内容,如果未能解决你的问题,请参考以下文章