1. 程式人生 > >MySQL之按月拆分主表並按月分表寫入數據提高數據查詢速度

MySQL之按月拆分主表並按月分表寫入數據提高數據查詢速度

tom auto begin rda rim 日期 pre tran create

使用場景:

主表數據量特別大,為了提高查詢的速度,可以考慮按月進行分表,要求就是當月的數據到當月表查詢,上月的數據到上月表查詢,當天的數據到主表來查詢。這樣在一定程度上也是提高了數據的查詢速度

過程演示:

1.創建總表:

CREATE TABLE `zong_biao` (
  `id` int(8) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `create_time` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
 `day` date NOT NULL DEFAULT ‘0000-00-00‘ COMMENT ‘日期‘,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

2.創建定時器:

當前時間1分鐘後,每2分鐘寫入一條數據到zong_biao,為了演示,要保證主表裏面一直有數據定時在寫入

delimiter $$
create event event_1 
on schedule every 2 MINUTE  STARTS   CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
COMMENT ‘xiaowu create‘
do 
    BEGIN
           insert into test.zong_biao(username,password,create_time,day) values("李四","tomcat",now(),curdate());
    END $$
delimiter ;

3.按月對主表進行分表拆分數據

3.1建立上月表和本月表

mysql> create table biao_lastmonth like zong_biao;

Query OK, 0 rows affected

mysql>  create table biao_month like zong_biao;

Query OK, 0 rows affected

3.2.創建一個表來記錄是否是1號,數據寫入是否成功:

Create Table: CREATE TABLE `dr_call_time` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `c_datetime` datetime NOT NULL COMMENT ‘執行時間‘,
  `type` tinyint(4) NOT NULL DEFAULT ‘0‘ COMMENT ‘是否為1號,默認為0,即不是1號‘,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

mysql> select * from dr_call_time;
+----+---------------------+------+
| id | c_datetime | type |
+----+---------------------+------+
| 1 | 2018-09-24 02:00:00 | 0 |
| 2 | 2018-09-25 02:00:00 | 0 |
| 3 | 2018-09-26 02:00:00 | 0 |
| 4 | 2018-09-27 02:00:00 | 0 |
| 5 | 2018-09-28 02:00:00 | 0 |
| 6 | 2018-09-29 02:00:00 | 0 |
| 7 | 2018-09-30 02:00:00 | 0 |
| 8 | 2018-10-01 02:00:01 | 1 |
| 9 | 2018-10-02 02:00:00 | 0 |
+----+---------------------+------+
9 rows in set (0.00 sec)

3.3. 建立存儲過程


/*每月1號 清空上月表,清空本月表;如果不是每月1號,則把昨天的數據插入本月數據表中*/
delimiter //
CREATE PROCEDURE p_dr_stats()
BEGIN
    declare v_day int;
  declare v_yesterday date;

set v_day = day(curdate());
set v_yesterday = date_sub(current_date(),interval 1 day);
select v_day;
/*select v_yesterday;*/

/*每月第一天 清空本月表和上月表;如果不是本月第一天,則把昨天的數據插入本月數據表中*/
if v_day = 1 then   
   truncate table biao_lastmonth;
   truncate table biao_month;
   insert into biao_lastmonth select * from zong_biao where day >= date_add(curdate()-day(curdate())+1,interval -1 month) and day < date_add(curdate(), interval - day(curdate()) + 1 day);
   insert into dr_call_time(c_datetime,type) values(now(),1);  /* 用於記錄該過程是否已執行*/
else
    insert into biao_month select * from zong_biao where day >= v_yesterday and day < curdate();
    insert into dr_call_time(c_datetime) values(now());
end if;
end //
delimiter ;

3.4 建立定時任務event

/*從2018-09-24 02:00:00開始,每天淩晨2:00執行上面的3.3的存儲過程
delimiter //
CREATE EVENT e_test
ON SCHEDULE EVERY 1 day STARTS TIMESTAMP ‘2018-09-24 02:00:00‘
ON COMPLETION PRESERVE
DO
BEGIN
CALL p_dr_stats();
END//
delimiter ; 

查看記錄是否是1號的表,了解event定時器和儲存過程的執行狀態

mysql> select * from dr_call_time;
+----+---------------------+------+
| id | c_datetime          | type |
+----+---------------------+------+
|  1 | 2018-09-24 02:00:00 |    0 |
|  2 | 2018-09-25 02:00:00 |    0 |
|  3 | 2018-09-26 02:00:00 |    0 |
|  4 | 2018-09-27 02:00:00 |    0 |
|  5 | 2018-09-28 02:00:00 |    0 |
|  6 | 2018-09-29 02:00:00 |    0 |
|  7 | 2018-09-30 02:00:00 |    0 |
|  8 | 2018-10-01 02:00:01 |    1 |
|  9 | 2018-10-02 02:00:00 |    0 |
+----+---------------------+------+
9 rows in set (0.00 sec)

到此處演示完畢,測試都是沒問題的

3.5查看創建存儲過程的語句


mysql> show create procedure p_dr_stats\G
*************************** 1. row ***************************
           Procedure: p_dr_stats
            sql_mode: STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `p_dr_stats`()
BEGIN
declare v_day int;
  declare v_yesterday date;

set v_day = day(curdate());
set v_yesterday = date_sub(current_date(),interval 1 day);
select v_day;

if v_day = 1 then   
   truncate table biao_lastmonth;
   truncate table biao_month;
   insert into biao_lastmonth select * from zong_biao where day >= date_add(curdate()-day(curdate())+1,interval -1 month) and day < date_add(curdate(), interval - day(curdate()) + 1 day);
   insert into dr_call_time(c_datetime,type) values(now(),1);  

else
    insert into biao_month select * from zong_biao where day >= v_yesterday and day < curdate();
    insert into dr_call_time(c_datetime) values(now());
end if;
end
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)

mysql> 

到此處MySQL之按月拆分主表並按月分表寫入數據已經演示完畢了,歡迎夥伴們留言一起交流學習。

MySQL之按月拆分主表並按月分表寫入數據提高數據查詢速度