2011-11-26 140 views
0

我使用partition plugin ACTIVE運行MySQL 5.1.47。MySQL分區:分區中的數據不平衡

我創建下表:

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進入同一個分區?

非常感謝您的幫助! Evan)

+0

我剛剛發現,如果我使用A,B,C,D而不是AAA,BBB,CCC,DDD分區工作正常。你能否建議如何根據字符串分割表而不是單個字符?謝謝!!! – EBAH

回答

0

根據MySQL手冊http://dev.mysql.com/doc/refman/5.1/en/partitioning-key.html通過KEY分區與HASH分區類似,只不過散列函數現在由MySQL內部選擇(而不是由用戶在HASH分區中選擇)。

該手冊在此不清楚,但函數應該是MD5(),PASSWORD()或基於後者的函數。所以它不會根據NE的值選擇分區,而是根據它計算的散列值。使用的散列函數具有均勻分佈,所以當你有很多行時,它們將在分區之間均勻分佈。

所以如果你想選擇分區功能,你應該使用HASH分區。但是你期待什麼?或者NE列有哪些可能的值?