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)

值被随机分到各个分区,说明分区表创建成功。




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2137232/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2137232/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值