mysql重建表分割槽並保留資料的方法

2021-08-17 23:05:13 字數 4191 閱讀 1532

本文介紹mysql重建表分割槽並保留資料的方法,mysql的表分割槽(partition)可以把乙個表的記錄分開多個區去儲存,查詢時可根據查詢的條件在對應的分割槽搜尋,而不需要整表查詢,提高查詢效率。

有分割槽的表與沒有分割槽的表使用上沒有太大的區別,但如果要對錶進行重新分割槽,刪除分割槽重建會刪除資料,因此不可直接進行操作,需要使用一些特別的處理實現。

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 |

+----------------+------------+

可以看到log表的資料已經按新分割槽儲存。

mysql表分割槽全文搜尋 Mysql表分割槽

什麼時候使用分割槽 海量資料 資料表索引大於伺服器有效記憶體 分割槽的限制 大部分只能對資料表的整型列進行分割槽,或者資料列可以通過分割槽函式轉化成整型列 其中columns支援 integer string date datetime型別 最大分割槽數目不能超過1024 如果含有唯一索引或者主鍵,...

mysql表分割槽

表分割槽的優點 查詢優化 缺點 除了資料庫管理方面複雜了點,其它的還沒有發現 只有5.1及之後的版本才支付分割槽,同時5.1中分割槽的一些維護的工具還不完善 mysql目前四種分割槽 1range 根據某個列的某種運算進行分割槽,分割槽的標誌都是該列的某種運算後的連續區間 create table ...

mysql 表分割槽

修改表的主鍵 alter table tb channel pv drop primary key,add primary key id channel 測試新增分割槽和刪除分割槽 新增刪除range分割槽 1 建立乙個分割槽 create table titles emp no int not n...