mysql的hash分区_MySQL中hash和key分区值的计算方法

MySQL中hash和key分区值的计算方法

mysql中有一种叫作key作为partition key的类型.来看看记录是怎么分布的

对于hash 分区,使用%操作符,每个partition key只能是int类型,通过

partition key%3(比如定义了三个分区)来把记录分布三个不同的artition里面

mysql> create table t13 ( a int,b int) partition by hash(a) partitions 3

mysql>insert into t14 values(10,1);

mysql>insert into t14 values(11,1);

mysql>insert into t14 values(12,1);

10%3=1 所以第一条记录是在p1里面,11%3=2在第二个分区p2里面,以此类推.

mysql> explain partitions select * from t13 where a=10;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t13   | p1         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t13 where a=11;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t13   | p2         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t13 where a=12;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t13   | p0         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

对于使用key partition 的方法,官方文档说是使用了一种password的方法.

mysql>create table t14 (a int,b int) partition by key(a) partitions 3

insert into t14 values(10,1);

insert into t14 values(11,1);

insert into t14 values(12,1);

insert into t14 values(13,1);

insert into t14 values(14,1);

insert into t14 values(15,1);

insert into t14 values(16,1);

mysql> explain partitions select * from t14 where a=10;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p0         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t14 where a=11;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p0         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t14 where a=12;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p1         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t14 where a=13;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p1         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t14 where a=14;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p2         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain partitions select * from t14 where a=15;

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | t14   | p2         | ALL  | NULL          | NULL | NULL    | NULL |    2 | Using where |

+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+

我发现对于key partition的规律是每两个值落在同一步分区里面,其他没有什么规律字,只是为什么样这样分配到不同的

分区里面,没有很好的解释。如果使用password函数,这些值的结果根本就不一样.有知道的朋友可以和我交流一下.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值