• ADADADADAD

    MySQL中如何创建Key分区表[ mysql数据库 ]

    mysql数据库 时间:2024-11-28 13:25:52

    作者:文/会员上传

    简介:

    按照KEY进行分区类似于按照HASH分区,除了HASH分区使用的用户定义的表达式,而KEY分区的 哈希函数是由MySQL 服务器提供。MySQL 簇(Cluster)使用函数MD5()来实现KEY分区;对于使用其

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    按照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分区表.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: keymysql