本文共 5643 字,大约阅读时间需要 18 分钟。
-
- ##SELECT TABLE_NAME,TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'bdata' ORDER BY TABLE_ROWS DESC
- ##select count(*) from (SELECT TABLE_NAME,TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'bdata' ORDER BY TABLE_ROWS DESC) as temp;
- /*
- DELIMITER $$
- drop procedure if exists deleteTables $$
- create procedure deleteTables ()
- begin
- declare tablename varchar(255);
- declare done int default false;
- declare continue handler for not found set done = true;
- declare rs cursor for select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'bdata' and TABLE_ROWS<100;
- open rs;
-
- read_loop: LOOP
-
- FETCH cur INTO a;
- SELECT a;
-
-
- IF done THEN
- LEAVE read_loop;
- END IF;
-
-
- END LOOP ;
-
- CLOSE cur;
-
-
-
- end $$
- DELIMITER ;
-
- call deleteTables;
-
- */
- delimiter $$
- DROP PROCEDURE IF EXISTS deletetables $$
- CREATE PROCEDURE deletetables ()
- BEGIN
- DECLARE tablename VARCHAR(255);
- DECLARE done INT DEFAULT FALSE;
-
- DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = 'bdata' AND table_rows<100;
-
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
- OPEN cur;
-
-
- READ_LOOP: LOOP
-
- FETCH cur INTO tablename;
-
-
- IF done THEN
- LEAVE READ_LOOP;
- END IF;
-
-
- SELECT tablename;
-
- SET @sqlstr=concat("drop table ", tablename);
- prepare stmt from @sqlstr;
- EXECUTE stmt;
- deallocate prepare stmt;
- END LOOP ;
-
- CLOSE cur;
- END $$
- delimiter ;
-
- create event if not exists event_deletetables_rowslessthan100
- on schedule every 1 day
- on completion preserve
- do call deletetables();
2. 事件查看
- #查看是否开启事件
- show variables like '%sche%';
-
- ##开启事件
- set global event_scheduler = 1;
-
- /*
-
- create event if not exists e_deletetables_rowsless100
- on schedule every 1 day
- on completion preserve
- do call deletetables();
- */
-
-
-
- ##注意分隔符的使用
- /*
- DELIMITER $$
- CREATE PROCEDURE test ()
- begin
- update student set name = now() where NO = '2012001';
- end $$
- DELIMITER ;
- */
- ##创建事件
- /*
- create event if not exists e_test
- on schedule every 5 second
- on completion preserve
- do call test();
-
- */
- ##关闭事件任务 定时任务
- #alter event e_test ON COMPLETION PRESERVE DISABLE;
- ##开启事件任务
- #alter event e_test ON COMPLETION PRESERVE ENABLE;
3. 定时任务测试
- ##查看是否开启事件
- show variables like '%sche%';
-
- ##开启事件
- set global event_scheduler = 1;
-
- ##注意分隔符的使用
- /*
- DELIMITER $$
- CREATE PROCEDURE test ()
- begin
- update student set name = now() where NO = '2012001';
- end $$
- DELIMITER ;
- */
- ##创建事件
- /*
- create event if not exists e_test
- on schedule every 5 second
- on completion preserve
- do call test();
-
- */
- ##关闭事件任务 定时任务
- #alter event e_test ON COMPLETION PRESERVE DISABLE;
- ##开户事件任务
- #alter event e_test ON COMPLETION PRESERVE ENABLE;
-
- #select table_name from information_schema.tables where table_schema='bdatadd'
-
- #select TABLE_NAME from (select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'bdata' and TABLE_ROWS<100) as temp limit 0,1;
- /*
- DELIMITER $$
-
- #drop procedure if exists new_procedure $$
-
- CREATE PROCEDURE `bdata`.`new_procedure` ()
- BEGIN
- DECLARE a VARCHAR(255);
-
-
- DECLARE done INT DEFAULT FALSE;
-
-
-
- DECLARE cur CURSOR FOR SELECT name FROM bdata.student;
-
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-
-
- OPEN cur;
-
-
- read_loop: LOOP
-
- FETCH cur INTO a;
- SELECT a;
-
-
- IF done THEN
- LEAVE read_loop;
- END IF;
-
-
- END LOOP ;
-
- CLOSE cur;
-
- END $$
- DELIMITER ;
-
- call new_procedure;
- */
4. 查看表
- #show tables like 'mkk_data_%';
- #use ptimpdb;
- #select table_name,table_rows from information_schema.columns where table_schema = "ptimpdb" and table_name like'mkk_data_%' ORDER BY TABLE_ROWS DESC;
- #select table_name from information_schema.columns where table_schema = "ptimpdb" and table_name like'mkk_data_%'group by table_name;
- #select count(*) as res from (select table_name from information_schema.columns where table_schema = "pdb" and table_name like'mkk_data_%'group by table_name)
- #select count(*) as res from mkk_data_10
-
- ##数据库查询表的结果
- #SELECT TABLE_NAME,TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'pdb' AND TABLE_NAME LIKE'mkk_data_%' AND TABLE_ROWS < 100 ORDER BY TABLE_ROWS DESC;
-
-
- #SELECT TABLE_NAME,TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'pdb' AND TABLE_NAME LIKE'mkk_data_%' AND TABLE_ROWS < 100 ORDER BY TABLE_ROWS DESC;
-
-
- delimiter $$
- DROP PROCEDURE IF EXISTS deletetables $$
- CREATE PROCEDURE deletetables ()
- BEGIN
- DECLARE tablename VARCHAR(255);
- DECLARE done INT DEFAULT FALSE;
-
- DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.tables WHERE table_schema = 'pdb' AND table_rows<100;
-
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
- OPEN cur;
-
-
- READ_LOOP: LOOP
-
- FETCH cur INTO tablename;
-
-
- IF done THEN
- LEAVE READ_LOOP;
- END IF;
-
-
- SELECT tablename;
-
- /* ## 动态sql语句的构建
- SET @sqlstr=concat("drop table ", tablename);
- prepare stmt from @sqlstr;
- EXECUTE stmt;
- ddeletetableseallocate prepare stmt;
- */
-
- END LOOP ;
-
- CLOSE cur;
- END $$
- delimiter ;
-
-
-
-
转载于:https://www.cnblogs.com/givemelove/p/8328828.html