MySQL普通錶轉換成分割槽表的兩種方法舉例
以jxfp_data_bak表為例:
mysql> desc jxfp_data_bak;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| SH | varchar(32) | YES | | NULL | |
| KPJH | varchar(32) | YES | | NULL | |
| ZFJH | varchar(10) | YES | | NULL | |
| TYPE | char(3) | YES | | NULL | |
| MONTH | char(10) | YES | | NULL | |
| STATUS | varchar(255) | YES | MUL | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| FP_DATA | mediumtext | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
方法1:用alter table table_name partition by命令重建分割槽表
mysql> alter table jxfp_data_bak PARTITION BY KEY(SH) PARTITIONS 8;
ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
直接增加主鍵會報錯:
mysql> alter table jxfp_data_bak add PRIMARY KEY(SH);
ERROR 1068 (42000): Multiple primary key defined
需要刪除原先的主鍵,並增加聯合主鍵:
mysql> ALTER TABLE `jxfp_data_bak` DROP PRIMARY KEY, ADD PRIMARY KEY(`id`,`SH`);
檢視新的表結構:
mysql> desc jxfp_data_bak;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| SH | varchar(32) | NO | PRI | | | --SH列為聯合主鍵
| KPJH | varchar(32) | YES | | NULL | |
| ZFJH | varchar(10) | YES | | NULL | |
| TYPE | char(3) | YES | | NULL | |
| MONTH | char(10) | YES | | NULL | |
| STATUS | varchar(255) | YES | | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| FP_DATA | mediumtext | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
用alter table table_name partition by命令重建分割槽表:
建hash分割槽會報錯,因為hash分割槽欄位只能是整型,不能是varchar型:
mysql> alter table jxfp_data_bak PARTITION BY HASH(SH) PARTITIONS 8;
ERROR 1659 (HY000): Field 'SH' is of a not allowed type for this type of partitioning
但建key分割槽則可以成功:
mysql> alter table jxfp_data_bak PARTITION BY KEY(SH) PARTITIONS 8;
Query OK, 78317 rows affected (3.42 sec)
Records: 78317 Duplicates: 0 Warnings: 0
測試分割槽功能:
mysql> explain partitions select * from jxfp_data_bak;
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
| 1 | SIMPLE | jxfp_data_bak | p0,p1,p2,p3,p4,p5,p6,p7 | ALL | NULL | NULL | NULL | NULL | 43588 | NULL |
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
1 row in set (0.00 sec)
以上我們可以看到全表掃描跨越了七個分割槽(p0--p7),說明分割槽表功能生效。
方法2:利用create table命令重建分割槽表
mysql> CREATE TABLE `jxfp_data` (
-> `id` bigint(20) NOT NULL AUTO_INCREMENT,
-> `SH` varchar(32) DEFAULT NULL COMMENT '稅號',
-> `KPJH` varchar(32) DEFAULT NULL COMMENT '開票機號',
-> `ZFJH` varchar(10) DEFAULT NULL COMMENT '主分機號',
-> `TYPE` char(3) DEFAULT NULL,
-> `MONTH` char(10) DEFAULT NULL,
-> `STATUS` varchar(255) DEFAULT NULL COMMENT '解析狀態標識',
-> `CREATE_TIME` datetime DEFAULT NULL COMMENT '插入時間',
-> `UPDATE_TIME` datetime DEFAULT NULL COMMENT '更新時間',
-> `FP_DATA` mediumtext COMMENT '發票資料',
-> PRIMARY KEY (`id`,`SH`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=81060 DEFAULT CHARSET=utf8 PARTITION BY LINEAR KEY (SH) PARTITIONS 8;
Query OK, 0 rows affected (0.08 sec) --建立分割槽表成功
注意:如果原表裡有主鍵欄位,則MYSQL的分割槽欄位必須包含在主鍵欄位內,否則會建立失敗,因此上面需要定義兩個聯合主鍵(`id`,`SH`)
插回資料:
mysql> insert into jxfp_data select * from ixinnuo_sjcj.jxfp_data;
Query OK, 745540 rows affected, 1 warning (28.21 sec)
Records: 745540 Duplicates: 0 Warnings: 1
測試:
mysql> explain partitions select * from jxfp_data;
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | jxfp_data | p0,p1,p2,p3,p4,p5,p6,p7 | ALL | NULL | NULL | NULL | NULL | 695986 | NULL |
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
mysql> desc jxfp_data_bak;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| SH | varchar(32) | YES | | NULL | |
| KPJH | varchar(32) | YES | | NULL | |
| ZFJH | varchar(10) | YES | | NULL | |
| TYPE | char(3) | YES | | NULL | |
| MONTH | char(10) | YES | | NULL | |
| STATUS | varchar(255) | YES | MUL | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| FP_DATA | mediumtext | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
方法1:用alter table table_name partition by命令重建分割槽表
mysql> alter table jxfp_data_bak PARTITION BY KEY(SH) PARTITIONS 8;
ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function
直接增加主鍵會報錯:
mysql> alter table jxfp_data_bak add PRIMARY KEY(SH);
ERROR 1068 (42000): Multiple primary key defined
需要刪除原先的主鍵,並增加聯合主鍵:
mysql> ALTER TABLE `jxfp_data_bak` DROP PRIMARY KEY, ADD PRIMARY KEY(`id`,`SH`);
檢視新的表結構:
mysql> desc jxfp_data_bak;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| SH | varchar(32) | NO | PRI | | | --SH列為聯合主鍵
| KPJH | varchar(32) | YES | | NULL | |
| ZFJH | varchar(10) | YES | | NULL | |
| TYPE | char(3) | YES | | NULL | |
| MONTH | char(10) | YES | | NULL | |
| STATUS | varchar(255) | YES | | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| FP_DATA | mediumtext | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
用alter table table_name partition by命令重建分割槽表:
建hash分割槽會報錯,因為hash分割槽欄位只能是整型,不能是varchar型:
mysql> alter table jxfp_data_bak PARTITION BY HASH(SH) PARTITIONS 8;
ERROR 1659 (HY000): Field 'SH' is of a not allowed type for this type of partitioning
但建key分割槽則可以成功:
mysql> alter table jxfp_data_bak PARTITION BY KEY(SH) PARTITIONS 8;
Query OK, 78317 rows affected (3.42 sec)
Records: 78317 Duplicates: 0 Warnings: 0
測試分割槽功能:
mysql> explain partitions select * from jxfp_data_bak;
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
| 1 | SIMPLE | jxfp_data_bak | p0,p1,p2,p3,p4,p5,p6,p7 | ALL | NULL | NULL | NULL | NULL | 43588 | NULL |
+----+-------------+---------------+-------------------------+------+---------------+------+---------+------+-------+-------+
1 row in set (0.00 sec)
以上我們可以看到全表掃描跨越了七個分割槽(p0--p7),說明分割槽表功能生效。
方法2:利用create table命令重建分割槽表
mysql> CREATE TABLE `jxfp_data` (
-> `id` bigint(20) NOT NULL AUTO_INCREMENT,
-> `SH` varchar(32) DEFAULT NULL COMMENT '稅號',
-> `KPJH` varchar(32) DEFAULT NULL COMMENT '開票機號',
-> `ZFJH` varchar(10) DEFAULT NULL COMMENT '主分機號',
-> `TYPE` char(3) DEFAULT NULL,
-> `MONTH` char(10) DEFAULT NULL,
-> `STATUS` varchar(255) DEFAULT NULL COMMENT '解析狀態標識',
-> `CREATE_TIME` datetime DEFAULT NULL COMMENT '插入時間',
-> `UPDATE_TIME` datetime DEFAULT NULL COMMENT '更新時間',
-> `FP_DATA` mediumtext COMMENT '發票資料',
-> PRIMARY KEY (`id`,`SH`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=81060 DEFAULT CHARSET=utf8 PARTITION BY LINEAR KEY (SH) PARTITIONS 8;
Query OK, 0 rows affected (0.08 sec) --建立分割槽表成功
注意:如果原表裡有主鍵欄位,則MYSQL的分割槽欄位必須包含在主鍵欄位內,否則會建立失敗,因此上面需要定義兩個聯合主鍵(`id`,`SH`)
插回資料:
mysql> insert into jxfp_data select * from ixinnuo_sjcj.jxfp_data;
Query OK, 745540 rows affected, 1 warning (28.21 sec)
Records: 745540 Duplicates: 0 Warnings: 1
測試:
mysql> explain partitions select * from jxfp_data;
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
| 1 | SIMPLE | jxfp_data | p0,p1,p2,p3,p4,p5,p6,p7 | ALL | NULL | NULL | NULL | NULL | 695986 | NULL |
+----+-------------+-----------+-------------------------+------+---------------+------+---------+------+--------+-------+
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2137603/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 普通錶轉換成分割槽表的四種方法
- 普通錶轉換為分割槽表
- 將普通錶轉換為分割槽表
- Oracle 將普通錶轉換為分割槽表Oracle
- ORACLE普通錶轉換成分割槽表的操作Oracle
- 【分割槽】如何將一個普通錶轉換為分割槽表
- 普通錶轉換分割槽表-線上重定義
- 將mysql非分割槽錶轉換為分割槽表MySql
- ORACLE將普通錶轉變為分割槽表方法Oracle
- 線上重定義 ?普通錶轉換成分割槽表
- 非分割槽錶轉換成分割槽表
- Oracle線上將普通錶轉分割槽表Oracle
- ORACLE 普通錶轉換成分割槽表(線上重定義)Oracle
- 將非分割槽錶轉換為分割槽表
- 批次轉換分割槽表為普通表
- 批量轉換分割槽表為普通表
- Oracle Database將普通錶轉換為分割槽表遇到的問題OracleDatabase
- ORACLE 普通錶轉換成分割槽表(線上重定義)(zt)Oracle
- 高手幫忙,超大普通錶轉分割槽表?
- 堆錶轉換成分割槽表
- 將一個非分割槽錶轉換為分割槽表
- 非分割槽錶轉換為分割槽表和partition indexIndex
- 非分割槽錶轉換成分割槽表以及注意事項
- 【ASK_ORACLE】使用insert語句將普通錶轉換成分割槽表Oracle
- 利用線上重定義功能將普通錶轉換為分割槽表問題
- ORACLE 11g 範圍分割槽錶轉換INTERVAL分割槽表Oracle
- 線上表格重定義研究,比如將普通錶轉換為分割槽表等
- 線上重定義的操作步驟__普通錶轉換為分割槽表的實驗案例
- Oracle普通表修改為分割槽表的方法Oracle
- 使用dbms_redefinition包線上把大資料量非分割槽錶轉換為分割槽表大資料
- 利用ORACLE DBMS_REDEFINITION包進行普通表到分割槽表的線上轉換Oracle
- MySQL表的四種分割槽型別MySql型別
- oracle 普通表-分割槽表改造流程Oracle
- LightDB 22.4 新特性之相容Oracle線上轉換普通表為分割槽表Oracle
- rebuild分割槽表分割槽索引的方法Rebuild索引
- (3) MySQL分割槽表使用方法MySql
- mysql幾種表分割槽建立案例MySql
- Oracle 12C新特性-線上把非分割槽錶轉為分割槽表Oracle