【MySQL】Event事件与游标
MySQL的事件就像Linux系统上的定时任务,按照设置的时间或者间隔时间执行设置好的任务。
如果用SQLyog一类的写存储过程、触发器或者事件会省事一些,例如SQLyog就会生成一个大致的模板:
DELIMITER $$
CREATE EVENT `report`.`monitor_user4cx` ON SCHEDULE EVERY 15 MINUTE DO
BEGIN
DECLARE cx_id INT(10);
DECLARE t_query VARCHAR(500);
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT id FROM information_schema.PROCESSLIST WHERE `USER`='cx' AND `TIME` > 600 AND `Command`='Query';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop:LOOP
FETCH cur INTO cx_id;
IF done THEN
LEAVE read_loop;
END IF;
SELECT t.trx_query,t.trx_started,p.`USER` FROM information_schema.PROCESSLIST p INNER JOIN information_schema.innodb_trx t ON p.id=t.trx_mysql_thread_id WHERE p.id=cx_id INTO @t_query,@t_time,@p_user;
INSERT INTO test.monitor_user4cx(`p_id`,`start_time`,`user`,`time`,`query`) VALUES (cx_id,@t_time,@p_user,NOW(),@t_query);
KILL cx_id;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
游标的写法:
CREATE PROCEDURE `test`.`new_procedure` ()
BEGIN
-- 需要定义接收游标数据的变量
DECLARE a CHAR(16);
-- 声明游标的结束标志
DECLARE done INT DEFAULT FALSE;
-- 将所需数据赋予游标,游标必须定义在变量/条件后,handler前;否则会报错。
-- ERROR 1337 (42000): Variable or condition declaration after cursor or handler declaration
DECLARE cur CURSOR FOR SELECT i FROM test.t;
-- 将结束标志绑定到游标,如果捕获到not found异常时就会将变量done设置为TRUE,done=TRUE可以当作循环跳出条件
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur; -- 开始循环
read_loop: LOOP
-- 提取游标里的数据,这里只有一个,也可以有多个,例如fetch <游标名> into <变量1>,<变量2>
FETCH cur INTO a;
-- 声明结束的时候
IF done THEN
LEAVE read_loop;
END IF; -- 这里做你想做的循环的事件
sql; END LOOP;
-- 关闭游标
CLOSE cur; END
游标示例:
1、repeat循环(该循环用do while,先执行后判断)
drop procedure if exists test_proce2;
create procedure test_proce2()
begin
declare temp_id int(11);
declare temp_time datetime;
declare isFinished boolean default false;
declare test_cursor cursor for select id,time from test;
declare continue handler for not found set isFinished=true;
open test_cursor;
repeat
fetch test_cursor into temp_id,temp_time;
if not isFinished then
select concat(concat(temp_id,":"),temp_time);
end if;
until isFinished end repeat;
close test_cursor;
end
2、loop循环
drop procedure if exists test_proce3;
create procedure test_proce3()
begin
declare temp_id int(11);
declare temp_time datetime;
declare isFinished boolean default false;
declare test_cursor cursor for select id,time from test;
declare continue handler for not found set isFinished=true;
open test_cursor;
test_loop:loop
fetch test_cursor into temp_id,temp_time;
if isFinished then
leave test_loop;
end if;
//若该if语句放在fetch后面,该循环为while型;若该if语句紧接在end loop前该循环为do while型。
select concat(concat(temp_id,":"),temp_time);
end loop test_loop;
close test_cursor;
end
示例:
定期检查长时间执行的查询,记录并杀掉
DELIMITER $$
CREATE EVENT `report`.`monitor_user4cx` ON SCHEDULE EVERY 15 MINUTE DO
BEGIN
DECLARE cx_id INT(10);
DECLARE t_query VARCHAR(500);
DECLARE done INT DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT id FROM information_schema.PROCESSLIST WHERE `USER`='cx' AND `TIME` > 600 AND `Command`='Query';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop:LOOP
FETCH cur INTO cx_id;
IF done THEN
LEAVE read_loop;
END IF;
SELECT t.trx_query,t.trx_started,p.`USER` FROM information_schema.PROCESSLIST p INNER JOIN information_schema.innodb_trx t ON p.id=t.trx_mysql_thread_id WHERE p.id=cx_id INTO @t_query,@t_time,@p_user;
INSERT INTO test.monitor_user4cx(`p_id`,`start_time`,`user`,`time`,`query`) VALUES (cx_id,@t_time,@p_user,NOW(),@t_query);
KILL cx_id;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;
语法中变量的使用方法:
### 可以直接用set赋值
set @a=1;
set @b=(select count(*) from information_schema.processlist);
insert into test_db.table1 select @a,@b,now(); ### 可以用into将结果集赋值给变量
select id,name,create_time from test_db.table2 into @u_id,@u_name,@u_addtime;
同
select id,name,create_time into @u_id,@u_name,@u_addtime from test_db.table2;
select @u_id,@u_name,@u_addtime;
删除事件:
drop event event_name;
部分内容转自:
http://www.cnblogs.com/trying/p/3296793.html
http://blog.csdn.net/willchyis/article/details/7943467
【MySQL】Event事件与游标的更多相关文章
- Mysql event事件用法
公司的数据库需要进行定期删除数据,需要用到mysql event事件,学习和梳理这块知识. 1查看event是否开启 SHOW VARIABLES LIKE 'event_scheduler'; 2开 ...
- 如何查看Mysql event事件是否启用
mysql> show variables like 'event_scheduler';+-----------------+-------+| Variable_name | Value ...
- MySQL中函数、游标、事件、视图
MySQL中函数.游标.事件.视图基本应用举例(代码) MySQL中function用户自定义函数c,fun,fun是面向过程的实现方式只能传入参数,或不传入参数,不能传出参数,必有返回值函数中是不能 ...
- MySQL中函数、游标、事件、视图基本应用举例(代码)
MySQL中function用户自定义函数c,fun,fun是面向过程的实现方式只能传入参数,或不传入参数,不能传出参数,必有返回值函数中是不能有create table drop table之类的语 ...
- Mysql中event事件的入门
Mysql中event事件的入门 主要涉及的知识点:mysql的存储过程.mysql的event事件调度. 参考资料: Qiao_Zhi的博客:[周期性执行事件]MySQL事件(Event)& ...
- mysql的event(事件)用法详解
SELECT * FROM mysql.event;SET GLOBAL event_scheduler = 1; -- 开启定时器 0:off 1:on SHOW VARIABLES LIKE 'e ...
- mysql定时任务(event事件)
1.event事件 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器” 事件和触发器类似,都 ...
- mysql定时任务(event事件)
1.事件简介 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”. 事件和触发器类似,都是在 ...
- Mysql event学习
我们可能比较熟悉crond,但是mysql也有一个自己的叫event,oracle的叫job. 开启mysql的event有很多种方法,和临时开启.我们在配置文件里面添加参数,随着服务一起开启. 在[ ...
随机推荐
- BIP_开发案例08_BI Publisher图表示例 饼状图/直方图/折线图(案例)
2014-12-25 Created By BaoXinjian
- OpenGL 开始学习指南
近期需要做一个涌潮的预报与仿真模拟,为了使模型更具有真实感,且逼真,使用起来更灵活.感觉还是得从基础的OpenGL学习.鉴于Direct3D技术存在的众多不确定性,且评论不太好的原因,决定用OpenG ...
- 【java】简单的事件总线EventBus
public class EventBus { private static Map<String, EventListener> eventListeners = new HashMap ...
- JAVA 图形界面 JFrame容器
一.图像化界面必须引入包 //引入图形化界面包 import java.awt.*; import javax.swing.*; 二.源代码 //窗口 import java.awt.*; impor ...
- 关于 MySQL UTF8 编码下生僻字符插入失败/假死问题的分析
原文:http://my.oschina.net/leejun2005/blog/343353 目录[-] 1.问题:mysql 遇到某些中文插入异常 2.原因:此 utf8 非彼 utf8 3.解决 ...
- 修改VNC访问的密码
:vncserver :iptables -I INPUT -p tcp --dport -j ACCEPT 客户端方式 :iptables -I INPUT -p tcp --dport -j AC ...
- ruby字符串学习笔记5
1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...
- JAVA中的定时调度(Timer和TimerTask)
某些时候我们需要定时去完成一些任务,这里举一个例子:我们需要在3秒钟后打印当前系统时间,此后每隔5秒重复此操作.代码如下: import java.util.TimerTask; import jav ...
- ios开发下的点透处理
界面上有一个浮动的div,这个div下面有一个文本框,当给这个div绑定touchstar后,做了冒泡的处理代码,还是会出现点透现象,触发下面的的文本框事件.立马弹出一个输入法出来. 1.网上有一种方 ...
- Grid++Report的几点总结
一.同事解决方案: 1.在View文件夹下建立报表文件A,用来作为报表呈现的载体.这个页面负责加载报表模板与加载数据源.其中报表模板由于后缀名为grf,在MVC中不做任何修改的情况下,系统会做路由处理 ...