您好,登錄后才能下訂單哦!
小編給大家分享一下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普通表如何轉換成分區表”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。