MySQL按时间拆分千万级大表的实现代码
作者:攻城狮悠扬
这篇文章主要介绍了MySQL按时间拆分千万级大表,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.创建存储过程
#-----创建存储过程 CREATE PROCEDURE ctrlcctrlv_table(var VARCHAR(20)) BEGIN SET @param1 = concat('ALTER TABLE ', var, ' RENAME ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt FROM @param1; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @param2 = concat('CREATE table ', var, ' like ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt1 FROM @param2; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; END;
2.创建EVEN事件
#-----创建存储过程 CREATE PROCEDURE ctrlcctrlv_table(var VARCHAR(20)) BEGIN SET @param1 = concat('ALTER TABLE ', var, ' RENAME ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt FROM @param1; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @param2 = concat('CREATE table ', var, ' like ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt1 FROM @param2; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; END;
3.启用EVEN
#-----创建存储过程 CREATE PROCEDURE ctrlcctrlv_table(var VARCHAR(20)) BEGIN SET @param1 = concat('ALTER TABLE ', var, ' RENAME ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt FROM @param1; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @param2 = concat('CREATE table ', var, ' like ', var, DATE_FORMAT(NOW(), '%Y_%m_%d')); PREPARE stmt1 FROM @param2; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; END;
4.测试存储过程
#--测试存储过程执行情况 call ctrlcctrlv_table("zong_biao");
来点干货在mysql里面测试一下
创建一张测试表
CREATE TABLE `test_cut_demo` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `create_time` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, `day` datetime DEFAULT NULL COMMENT '日期', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2997 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
添加一个循环插入的EVENT
#--创建循环测试插入数据 delimiter $$ create event event_insert_SECOND on schedule every 2 SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE COMMENT 'xiaowu create' do BEGIN insert into new_cut.test_cut_demo(username,password,create_time,day) values("李四","tomcat",now(),curdate()); END $$ delimiter ; #----红色库名,蓝色表明
添加事件启动
alter event event_insert_SECOND on completion preserve enable;
查看数据库表中是否开始循环插入数据
调用存储过程测试是否可以进行表切割
#--测试存储过程执行情况 call ctrlcctrlv_table("test_cut_demo");
到此这篇关于MySQL按时间拆分千万级大表的文章就介绍到这了,更多相关MySQL千万级大表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!