MySQL中如何创建Key分区表
本篇文章为大家展示了MySQL中如何创建Key分区表,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
按照KEY进行分区类似于按照HASH分区,除了HASH分区使用的用户定义的表达式,而KEY分区的 哈希函数是由MySQL 服务器提供。MySQL 簇(Cluster)使用函数MD5()来实现KEY分区;
对于使用其他存储引擎的表,服务器使用其自己内部的 哈希函数,这些函数是基于与PASSWORD()一样的运算法则。
Key分区与Hash分区很相似,只是Hash函数不同,定义时把Hash关键字替换成Key即可,同样Key分区也有对应与线性Hash的线性Key分区方法。
语法为PARTITION BY LINEAR KEY(列名)
创建key分区表举例如下:
mysql> CREATE TABLE `dsf_data` (
-> `id` bigint(20) NOT NULL AUTO_INCREMENT,
-> `SH` varchar(32) DEFAULT NULL COMMENT '税号',
-> `KPJH` varchar(32) DEFAULT NULL COMMENT '开票机号',
-> `ZFJH` varchar(32) DEFAULT NULL COMMENT '主分机号',
-> `MONTH` varchar(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`),
-> KEY `index_sh` (`SH`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=1173560 DEFAULT CHARSET=utf8 PARTITION BY LINEAR KEY (SH) PARTITIONS 8;
Query OK, 0 rows affected (0.11 sec)
备注:如果分区字段中有主键或者唯一索引的列,那么所有主键列和唯一索引列都必须包含进来,因此上一步必须有两个主键PRIMARY KEY (`id`,`SH`)存在。
插入数据:
mysql> insert into dsf_data select * from test.fp_data;
Query OK, 202632 rows affected, 1 warning (18.96 sec)
Records: 202632 Duplicates: 0 Warnings: 1
mysql> explain partitions select sh from dsf_data; --全表扫描共访问了8个分区(p0--p7)
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
| 1 | SIMPLE | dsf_data | p0,p1,p2,p3,p4,p5,p6,p7 | index | NULL | index_sh | 98 | NULL | 8 | Using index |
+----+-------------+----------+-------------------------+-------+---------------+----------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='130202568907641'; --值被随机分到了p0分区
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p0 | ref | index_sh | index_sh | 98 | const | 1 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='440300683797687'; --值被随机分到了p4分区
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p4 | ref | index_sh | index_sh | 98 | const | 1 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> explain partitions select sh from dsf_data where sh='91500107784224861G'; --sh的值被随机分到了p6分区
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| 1 | SIMPLE | dsf_data | p6 | ref | index_sh | index_sh | 98 | const | 452 | Using where; Using index |
+----+-------------+----------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
值被随机分到各个分区,说明分区表创建成功。
上述内容就是MySQL中如何创建Key分区表,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。