【问题标题】:MySQL Partitions: data not balanced in partitionsMySQL 分区:分区中的数据不平衡
【发布时间】:2012-01-14 02:27:46
【问题描述】:

我使用分区插件 ACTIVE 运行 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 在同一个分区?

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

【问题讨论】:

  • 我刚刚发现如果我使用 A、B、C、D 而不是 AAA、BBB、CCC、DDD 分区工作正常。您能否建议如何根据字符串而不是单个字符对表进行分区?谢谢!!!

标签: mysql partitioning


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2016-12-12
    • 2021-09-02
    • 2013-07-02
    • 1970-01-01
    相关资源
    最近更新 更多