MySQL 存储过程 定时任务

drop procedure if exists move_track_data;
delimiter //
create procedure move_track_data()
begin
    declare i int default 0;
    start transaction;
    set i = 1;
    case
        when i = 1 then
            select ‘is 1‘;
        when i = 2 then
            select ‘is 2‘;
        else
            select ‘is 3‘;
    end case;
    commit;
end;
//

call move_track_data();
delimiter ;
select * from score_driving_track where trackid in( select trackid from score_driving_track where trackid % 10 = 1);

insert into score_driving_track_0 select * from score_driving_track where trackid in( select trackid from score_driving_track where trackid % 10 =0);

####################################################################################################3

set time_zone = ‘+8:00‘;
set GLOBAL event_scheduler = 1;
use test;
drop event if exists upload_to_sdmp;
DELIMITER //
create event upload_to_sdmp
on schedule every 1 week starts timestamp ‘2015-10-12 02:00:00‘
do call move_score_driving_track_data();

drop procedure if exists move_score_driving_track_data;
delimiter //
create procedure move_score_driving_track_data()
begin
    declare i int default 0;
    declare t_error integer default 0;
    declare continue handler for sqlexception set t_error = 1;
    start transaction;
    set i = 0;
    while i < 10 do
        set @sqlTrackids = concat(‘select group_concat(distinct trackid) into @trackids  from score_driving_track where trackid % 10 = ‘, i);
        select @sqlTrackids;
        prepare sqlTrackidsNormal from @sqlTrackids;
        execute sqlTrackidsNormal;
        select @trackids;

        set @sqlInsert = concat(‘insert into score_driving_track_‘, i,  ‘ select * from score_driving_track where find_in_set(trackid, ‘‘‘, @trackids, ‘‘‘)‘);
        select @sqlInsert;
        prepare sqlInsertNormal from @sqlInsert;
        execute sqlInsertNormal;

        set @sqlDelete = concat(‘delete from score_driving_track‘, ‘ where find_in_set(trackid ,‘‘‘, @trackids, ‘‘‘)‘);
        select @sqlDelete;
        prepare sqlDeleteNormal from @sqlDelete;
        #execute sqlDeleteNormal;

        set i = i + 1;
    end while;
    if t_error = 1 then
       rollback;
    else
       commit;
    end if;
end;
//
call move_score_driving_track_data();
SELECT @@global.sort_buffer_size;

select * from score_driving_track where trackid >= 2270;
select * from score_driving_track_0;
select * from score_driving_track_1;
select * from score_driving_track_2;
select * from score_driving_track_3;
select * from score_driving_track_4;
select * from score_driving_track_5;
select * from score_driving_track_6;
select * from score_driving_track_7;
select * from score_driving_track_8;
select * from score_driving_track_9;

delete from score_driving_track_0;
delete from score_driving_track_1;
delete from score_driving_track_2;
delete from score_driving_track_3;
delete from score_driving_track_4;
delete from score_driving_track_5;
delete from score_driving_track_6;
delete from score_driving_track_7;
delete from score_driving_track_8;
delete from score_driving_track_9;

SET GLOBAL group_concat_max_len=1024000; 

show variables like "group_concat_max_len";
#####################################################################################

call move_score_driving_track_data();
SELECT @@global.sort_buffer_size;

SET GLOBAL group_concat_max_len=1024000; 

show variables like "group_concat_max_len"; 

select group_concat(distinct trackid)  from score_driving_track where trackid % 10 = 0;
SELECT @@global.group_concat_max_len;
show warnings;
#[mysqld]
#explicit_defaults_for_timestamp=true
#group_concat_max_len=102400
select distinct trackid  from score_driving_track where trackid % 10 = 0;

select group_concat(distinct trackid)  from score_driving_track where trackid % 10 = 0 and trackid < 50;

delimiter ;
select concat(‘score_driving_track_‘, 1);

select * from score_driving_track_0;

delete from score_driving_track_0;

##############################################################################
set time_zone = ‘+8:00‘;
set GLOBAL event_scheduler = 1;
use test;
drop event if exists upload_to_sdmp;
DELIMITER //
create event upload_to_sdmp
on schedule every 1 second #starts timestamp ‘2014-07-30 10:00:00‘
do
begin
    insert into b values(3,‘222‘);
    insert into b values(2,‘222‘);
end
//
DELIMITER ;  

select * from b;

select group_concat(trackid) into @temp  from score_driving_track where trackid < 100;
select @temp;
select * from score_driving_track where find_in_set(trackid, @temp);
select REPLACE(@temp,‘‘‘‘‘‘‘‘,‘‘);

select CHAR_LENGTH(@temp)-CHAR_LENGTH(REPLACE(@temp,‘‘,‘‘)) + 1;

select * from score_driving_track where trackid in (select group_concat(trackid)  from score_driving_track where trackid < 50);

select * from score_driving_track where find_in_set(trackid, (select group_concat(distinct trackid)  from score_driving_track where trackid < 50));

select * from users;
时间: 2024-10-07 12:05:00

MySQL 存储过程 定时任务的相关文章

Mysql 存储过程+定时任务,完成分区自动维护

建表: drop table if exists terminal_parameter; CREATE TABLE `terminal_parameter` ( `terminal_parameter_id` int(11) NOT NULL AUTO_INCREMENT, .................... `createtime` datetime NOT NULL, `userid` int(8) NOT NULL, `terminal_id` int(8) DEFAULT NULL

MySQL存储过程定时任务

一.基本概念 mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存储过程使用,在某一特定的时间点,触发相关的SQL语句或存储过程. 二.适用范围 对于每隔一段时间就有固定需求的操作,如创建表,删除数据等操作,可以使用event来处理.例如:使用event在每月的1日凌晨1点自动创建下个月需要使用的三张表. 三.使用权限 单独使用event调用SQL语句时,查看和

MySQL创建定时任务

一.前言 自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作系统的计划任务来执行的工作.更值得 一提的是MySQL的事件调度器可以精确到每秒钟执行一个任务,而操作系统的计划任务(如:Linux下的CRON或Windows下的任务计划)只能精 确到每分钟执行一次.对于一些对数据实时性要求比较高的应用(例如:股票.赔率.比分等)就非常适合. 事件调度器有时也可称

如何写mysql的定时任务

什么是事件: 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 查看是否开启: show variables like 'event_scheduler'; 如果显示OFF,则输入以下语句开启: set global event_scheduler = on; 提醒:虽然这里用set global event_scheduler = on语句开启了事件,但是每次重启电脑.或重启mysql服务后,会发现,事件自动关

MySQL存储过程总结(一)

最近比较有兴趣研究MySQL定时任务存储过程,也开始学习MySQL几款查询管理工具,主要有Navicat for MySQL.SQLyog.MySQL Workbench 6.0.Toad for MySQL 6.0几款工具,都非常强大,正在陆续的学习中,下面先对MySQL存储过程做些总结.    一.存储过程 MySQL存储过程是从MySQL5.0开始增加的新功能,存储过程的优点有很多,不过最主要的还是执行效率和SQL代码封装.特别是SQL代码封装,存储过程易于维护,执行效率高.   二.简单

MySQL job/定时任务/event 学习

参考文章: https://blog.csdn.net/qq_21108311/article/details/82589850 https://blog.csdn.net/qq_27238185/article/details/56271877 https://blog.csdn.net/lxpbs8851/article/details/8019965 概要简述 原文链接:https://blog.csdn.net/qq_21108311/article/details/82589850 M

MySQL存储过程定义及调用

#MySQL存储过程 ##最简单的存储过程 DROP PROCEDURE IF EXISTS sp1; CREATE PROCEDURE sp1() COMMENT '最简单的存储过程' SELECT 1; ##调用 CALL sp1 (); ##带输入参数的存储过程 DROP PROCEDURE IF EXISTS sp2; CREATE PROCEDURE sp2(IN p INT) COMMENT '带输入参数的存储过程' SELECT p; ##调用 CALL sp2(1); #带输出参

MySQL存储过程详解 mysql 存储过程

MySQL存储过程详解  mysql 存储过程 (2010-12-13 10:59:38) 转载▼ 标签: it 分类: mysql mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它. 一个存储过程是一个可编程的函数,它在数据库中创建并保

MySQL 存储过程检测表是否存在

SQL语句实现: SELECT COUNT(*) FROM information_schema.`TABLES` WHERE TABLE_NAME='youTableName'; where子句中,表名是字符串类型,要加单引号. MySQL存储过程中 函数实现: CREATE DEFINER = `root`@`localhost` FUNCTION `tableExists`(`tName` varchar(30))  RETURNS tinyint(4) BEGIN DECLARE tab