MySQL中如何创建Key分区表

发布时间:2021-07-28 17:01:53 作者:Leah
来源:亿速云 阅读:237

本篇文章为大家展示了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分区表,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 创建、查看分区表的Metadata
  2. hive分区表如何创建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

key mysql

上一篇:R语言的函数参数分享

下一篇:Oracle中如何创建复合索引

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》