我创建了一个mysql表,并将哈希分区如下。
mysql> CREATE TABLE employees (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT,
PRIMARY KEY(id)
)
PARTITION BY HASH(id)
PARTITIONS 10;
成功创建表后,我将值1(插入store_id)插入到下面的表中
mysql>INSERT INTO employees (store_id) values (1);
现在我不明白这个1的值会在哪里进入?进入哪个分区(p0,p1,p2 ...... p10)store_id值为1?我以为它会进入p0。但事实并非如此。见下文我这样检查了
mysql>SELECT TABLE_NAME, PARTITION_NAME, TABLE_ROWS, AVG_ROW_LENGTH,DATA_LENGTH FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME LIKE 'employees';
它已显示值已进入p1.see
mysql>
+------------+----------------+------------+----------------+-------------+
| TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |
+------------+----------------+------------+----------------+-------------+
| employees | p0 | 0 | 0 | 16384 |
| employees | p1 | 1 | 16384 | 16384 |
| employees | p2 | 0 | 0 | 16384 |
| employees | p3 | 0 | 0 | 16384 |
| employees | p4 | 0 | 0 | 16384 |
| employees | p5 | 0 | 0 | 16384 |
| employees | p6 | 0 | 0 | 16384 |
| employees | p7 | 0 | 0 | 16384 |
| employees | p8 | 0 | 0 | 16384 |
| employees | p9 | 0 | 0 | 16384 |
+------------+----------------+------------+----------------+-------------+
我不知道为什么它再次被插入p1.tested ..我这次插入值2 ...
mysql> INSERT INTO employees (store_id) values (2);
它已进入p2。
+------------+----------------+------------+----------------+-------------+
| TABLE_NAME | PARTITION_NAME | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH |
+------------+----------------+------------+----------------+-------------+
| employees | p0 | 0 | 0 | 16384 |
| employees | p1 | 1 | 16384 | 16384 |
| employees | p2 | 1 | 16384 | 16384 |
| employees | p3 | 0 | 0 | 16384 |
| employees | p4 | 0 | 0 | 16384 |
| employees | p5 | 0 | 0 | 16384 |
| employees | p6 | 0 | 0 | 16384 |
| employees | p7 | 0 | 0 | 16384 |
| employees | p8 | 0 | 0 | 16384 |
| employees | p9 | 0 | 0 | 16384 |
+------------+----------------+------------+----------------+-------------+
为什么将值插入到不同的分区中?是否存在散列分区遵循的规则?有趣的是它离开p0并开始插入p1?解释
答案 0 :(得分:1)
如果this解释适用于您的MySQL版本,则可以通过以下方式找到分区号:MOD([Your input],[Number of partitions])
。
在你的情况下,第一行可能有id
= 1,计算将是MOD(1,10)
= 1.行进入分区1(id
= 2进入分区2)