您好,登錄后才能下訂單哦!
下文主要給大家帶來mysql重建表如何實現分區并保留數據,希望這些文字能夠帶給大家實際用處,這也是我編輯mysql重建表如何實現分區并保留數據這篇文章的主要目的。好了,廢話不多說,大家直接看下文吧。
1.創建與原始表一樣結構的新表,新分區。
2.將原始表中數據復制到新表。
3.刪除原始表。
4.將新表名稱改為原始表名稱。
日志表原始結構如下,按id分區。
CREATE DATABASE `test`;use `test`;CREATE TABLE `log` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `content` text NOT NULL COMMENT '內容', `status` tinyint(3) unsigned NOT NULL COMMENT '記錄狀態', `addtime` int(11) unsigned NOT NULL COMMENT '添加時間', `lastmodify` int(11) unsigned NOT NULL COMMENT '最后修改時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (id) (PARTITION p10w VALUES LESS THAN (100000) ENGINE = InnoDB, PARTITION p20w VALUES LESS THAN (200000) ENGINE = InnoDB, PARTITION p50w VALUES LESS THAN (500000) ENGINE = InnoDB, PARTITION p100w VALUES LESS THAN (1000000) ENGINE = InnoDB, PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;insert into `log`(content,status,addtime,lastmodify) values('content1',1, unix_timestamp('2018-01-11 00:00:00'), unix_timestamp('2018-01-11 00:00:00')), ('content2',1, unix_timestamp('2018-02-22 00:00:00'), unix_timestamp('2018-02-22 00:00:00')), ('content3',1, unix_timestamp('2018-03-31 00:00:00'), unix_timestamp('2018-03-31 00:00:00'));
查看數據分區分布
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='test' AND TABLE_NAME = 'log';+----------------+------------+| PARTITION_NAME | TABLE_ROWS | +----------------+------------+| p10w | 3 | | p20w | 0 | | p50w | 0 | | p100w | 0 || pmax | 0 | +----------------+------------+
日志數據需要按時間進行搜尋,因此需要按日志時間重建分區。
1.創建log2,按時間分區(每月1個分區)
CREATE TABLE `log2` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `content` text NOT NULL COMMENT '內容', `status` tinyint(3) unsigned NOT NULL COMMENT '記錄狀態', `addtime` int(11) unsigned NOT NULL COMMENT '添加時間', `lastmodify` int(11) unsigned NOT NULL COMMENT '最后修改時間', PRIMARY KEY (`id`,`addtime`), KEY `id`(`id`), KEY `addtime`(`addtime`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (addtime) (PARTITION p201801 VALUES LESS THAN (unix_timestamp('2018-02-01 00:00:00')) ENGINE = InnoDB, PARTITION p201802 VALUES LESS THAN (unix_timestamp('2018-03-01 00:00:00')) ENGINE = InnoDB, PARTITION p201803 VALUES LESS THAN (unix_timestamp('2018-04-01 00:00:00')) ENGINE = InnoDB, PARTITION p201804 VALUES LESS THAN (unix_timestamp('2018-05-01 00:00:00')) ENGINE = InnoDB, PARTITION pmax VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;
2.將log的數據復制到log2
insert into `log2` select * from `log`;
3.刪除log表
drop table `log`;
4.將log2表改名為log
rename table `log2` to `log`;
執行后查看數據分區分布
SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA='test' AND TABLE_NAME = 'log';+----------------+------------+| PARTITION_NAME | TABLE_ROWS | +----------------+------------+| p201801 | 1 | | p201802 | 1 | | p201803 | 1 | | p201804 | 0 || pmax | 0 | +----------------+------------+
對于以上關于mysql重建表如何實現分區并保留數據,大家是不是覺得非常有幫助。如果需要了解更多內容,請繼續關注我們的行業資訊,相信你會喜歡上這些內容的。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。