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函數,這些值的結果根本就不一樣.有知道的朋友可以和我交流一下.
總結
以上是生活随笔為你收集整理的mysql的hash分区_MySQL中hash和key分区值的计算方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: neo4j 机器学习_neo4j(图数据
- 下一篇: nodejs mysql 模型_node