MySQL分区:分区中的数据不平衡

发布于 2024-12-18 12:10:02 字数 3048 浏览 2 评论 0原文

我运行 MySQL 5.1.47,分区插件处于活动状态。

我创建下表:

CREATE TABLE IF NOT EXISTS `prova` ( 
`NE` varchar(8) NOT NULL, 
`ASSERT` longtext NOT NULL 
) ENGINE=MyISAM 
PARTITION BY KEY(NE) 
PARTITIONS 4; 

然后插入 4 行:

INSERT INTO prova values ('AAA','this assert is from AAA'); 
INSERT INTO prova values ('BBB','this assert is from BBB'); 
INSERT INTO prova values ('CCC','this assert is from CCC'); 
INSERT INTO prova values ('DDD','this assert is from DDD'); 

我希望在每个分区中找到 1 条记录,但是:

mysql> explain partitions select * from prova where NE='AAA'; 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| 1 | SIMPLE | prova | p2 | system | NULL | NULL | NULL | NULL | 1 | | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='BBB'; 
+---+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE | prova | p1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='CCC'; 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| 1 | SIMPLE | prova | p0 | system | NULL | NULL | NULL | NULL | 1 | | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='DDD'; 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE | prova | p1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.00 sec)

所以,我的问题是: 我做错了什么?为什么 4 个插入没有分成 4 个分区? 为什么BBB和DDD要在同一个分区?

非常感谢您的帮助! 埃文)

I run MySQL 5.1.47 with partition plugin ACTIVE.

I create the following table:

CREATE TABLE IF NOT EXISTS `prova` ( 
`NE` varchar(8) NOT NULL, 
`ASSERT` longtext NOT NULL 
) ENGINE=MyISAM 
PARTITION BY KEY(NE) 
PARTITIONS 4; 

Then I insert 4 rows:

INSERT INTO prova values ('AAA','this assert is from AAA'); 
INSERT INTO prova values ('BBB','this assert is from BBB'); 
INSERT INTO prova values ('CCC','this assert is from CCC'); 
INSERT INTO prova values ('DDD','this assert is from DDD'); 

I expect to find 1 record in each partition but:

mysql> explain partitions select * from prova where NE='AAA'; 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| 1 | SIMPLE | prova | p2 | system | NULL | NULL | NULL | NULL | 1 | | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='BBB'; 
+---+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE | prova | p1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='CCC'; 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
| 1 | SIMPLE | prova | p0 | system | NULL | NULL | NULL | NULL | 1 | | 
+----+-------------+-------+------------+--------+---------------+------+---------+------+------+-------+ 
1 row in set (0.00 sec) 

mysql> explain partitions select * from prova where NE='DDD'; 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
| 1 | SIMPLE | prova | p1 | ALL | NULL | NULL | NULL | NULL | 4 | Using where | 
+----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+ 
1 row in set (0.00 sec)

So, my question is:
what am I doing wrong? Why the 4 insert aren't splitted into the 4 partitions?
Why BBB and DDD went in the same partition?

Thank you very much for your help!
Evan)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静 2024-12-25 12:10:02

根据MySQL手册 http://dev.mysql.com/doc /refman/5.1/en/partitioning-key.html 按 KEY 分区与按 HASH 分区类似,只是散列函数现在由 MySQL 内部选择(而不是像 HASH 分区那样由用户选择)。

手册对此并没有明确说明,但该函数应该是 MD5()PASSWORD() 或基于后者的函数。因此它不会根据 NE 的值来选择分区,而是根据它计算出的哈希值来选择分区。使用的哈希函数具有均匀分布,因此当您有很多行时,它们将在分区之间均匀分布。

所以如果你想选择分区的函数,你应该使用HASH分区。但你在期待什么?或者 NE 列的可能值是什么?

According to MySQL manual http://dev.mysql.com/doc/refman/5.1/en/partitioning-key.html partitioning by KEY is similar to partition by HASH except that the hashing function is now choosen internally by MySQL (and not by user as in HASH partition).

The manual is not clear in this, but the function should be either MD5(), PASSWORD() or a function based on the latter one. So it does not select partition based on the value of NE, but on a hash computed from it. Used hash functions have even distributions, so when you have many rows, they will be distributed evenly between the partitions.

So if you want to choose the function by which to partition, you should use partition by HASH. But what you were expecting? Or what are the possible values of NE column?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文