免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 7610 | 回复: 11
打印 上一主题 下一主题

[原创]MySQL的LIST分区体验与总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-11 11:34 |只看该作者 |倒序浏览
我博客上的链接:
http://blog.chinaunix.net/u/29134/showart_493359.html

终于有点空闲时间了,测试一下LIST分区,因为LIST在我们的开发中用到。他分区以后再补上。
版本:
Server version:         5.1.23a-maria-alpha-log MySQL Community Server [Maria] (GPL)
一、讲在前面
注意:
1、ALTER TABLE也可以用于对带分区的表进行重新分区,所以不能在建表之后再用ALTER TABLE语法。
2、如果你表中有KEY。用来分区的字段必须是KEY的一部份。
3、现在的分区属于水平分区。(垂直分区我们可以自己模拟,这个以后再写)


mysql> use t_girl
Database changed
先建立一个普通表
mysql> create table category( cid int unsigned not null auto_increment primary key, cname varchar(64) not null, parent_id int not null);
Query OK, 0 rows affected (0.00 sec)
mysql> create table parent(parent_id int not null auto_increment primary key,pname varchar(64) not null);
Query OK, 0 rows affected (0.00 sec)

分区表
mysql> create table category_part( cid int unsigned not null auto_increment,cname varchar(64) not null,parent_id int not null,primary key (cid,parent_id))
partition by list(parent_id)(
partition p1 values in (1,2,3,6,9),
partition p2 values in (4,5,10,22,23),
partition p3 values in (7,8,11,12,13),
partition p4 values in (14,15,16,17,20),
partition p5 values in (18,19,21,24,25)
);
Query OK, 0 rows affected (0.01 sec)

插入数据部分省略。。。
建立索引。
mysql> create index f_parent_id on category(parent_id);
Query OK, 2048000 rows affected (17.61 sec)
Records: 2048000 Duplicates: 0 Warnings: 0
mysql> show index from category;
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table    | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| category |          0 | PRIMARY     |            1 | cid         | A         |     2048000 |     NULL | NULL   |      | BTREE      |         |
| category |          1 | f_parent_id |            1 | parent_id   | A         |          25 |     NULL | NULL   |      | BTREE      |         |
+----------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.00 sec)


mysql> create index f_parent_id on category_part(parent_id);
Query OK, 2048000 rows affected (18.57 sec)
Records: 2048000 Duplicates: 0 Warnings: 0

mysql> show index from category_part;
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table         | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| category_part |          0 | PRIMARY     |            1 | cid         | A         |     2048000 |     NULL | NULL   |      | BTREE      |         |
| category_part |          0 | PRIMARY     |            2 | parent_id   | A         |     2048000 |     NULL | NULL   |      | BTREE      |         |
| category_part |          1 | f_parent_id |            1 | parent_id   | A         |         318 |     NULL | NULL   |      | BTREE      |         |
+---------------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.01 sec)

mysql> select count(*) from category;
+----------+

| count(*) |
+----------+

| 2048000 |
+----------+

1 row in set (0.00 sec)

mysql> select count(*) from category_part;
+----------+

| count(*) |
+----------+

| 2048000 |
+----------+

1 row in set (0.00 sec)

mysql> select count(*) from parent;
+----------+

| count(*) |
+----------+

| 25 |
+----------+

1 row in set (0.00 sec)

二、具体测试
1、我们来看一下查询性能比较:
1)、单表查询
mysql> select count(*) from category where parent_id in (22,20);
+----------+

| count(*) |
+----------+

| 17002 |
+----------+

1 row in set (0.03 sec)

mysql> select count(*) from category_part where parent_id in (22,20);
+----------+

| count(*) |
+----------+

| 17002 |
+----------+

1 row in set (0.02 sec)
分区表普通的做了索引的速度上快了一点,不过差别不是很大。

mysql> explain select count(*) from category where parent_id in (22,20);
+----+-------------+----------+-------+---------------+-------------+---------+------+-------+--------------------------+
| id | select_type | table    | type  | possible_keys | key         | key_len | ref  | rows  | Extra                    |
+----+-------------+----------+-------+---------------+-------------+---------+------+-------+--------------------------+
|  1 | SIMPLE      | category | range | f_parent_id   | f_parent_id | 4       | NULL | 14335 | Using where; Using index |
+----+-------------+----------+-------+---------------+-------------+---------+------+-------+--------------------------+
1 row in set (0.00 sec)

mysql> explain partitions select count(*) from category_part where parent_id in (22,20);
+----+-------------+---------------+------------+-------+---------------+-------------+---------+------+-------+--------------------------+
| id | select_type | table         | partitions | type  | possible_keys | key         | key_len | ref  | rows  | Extra                    |
+----+-------------+---------------+------------+-------+---------------+-------------+---------+------+-------+--------------------------+
|  1 | SIMPLE      | category_part | p2,p4      | range | f_parent_id   | f_parent_id | 4       | NULL | 16893 | Using where; Using index |
+----+-------------+---------------+------------+-------+---------------+-------------+---------+------+-------+--------------------------+
1 row in set (0.00 sec)

mysql> select count(*) from category where parent_id = 25;
+----------+

| count(*) |
+----------+

| 2001 |
+----------+

1 row in set (0.01 sec)

mysql> select count(*) from category_part where parent_id = 25;
+----------+

| count(*) |
+----------+

| 2001 |
+----------+

1 row in set (0.00 sec)

mysql> explain select count(*) from category where parent_id = 25;
+----+-------------+----------+------+---------------+-------------+---------+-------+-------+-------------+
| id | select_type | table    | type | possible_keys | key         | key_len | ref   | rows  | Extra       |
+----+-------------+----------+------+---------------+-------------+---------+-------+-------+-------------+
|  1 | SIMPLE      | category | ref  | f_parent_id   | f_parent_id | 4       | const | 38240 | Using index |
+----+-------------+----------+------+---------------+-------------+---------+-------+-------+-------------+
1 row in set (0.00 sec)

mysql> explain partitions select count(*) from category_part where parent_id = 25;
+----+-------------+---------------+------------+------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table         | partitions | type | possible_keys | key         | key_len | ref   | rows | Extra       |
+----+-------------+---------------+------------+------+---------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | category_part | p5         | ref  | f_parent_id   | f_parent_id | 4       | const | 4647 | Using index |
+----+-------------+---------------+------------+------+---------------+-------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
可以看出,扫描的行数大幅度减少
2)、多表内联性能

mysql> select count(*) from category as a inner join parent as b using(parent_id);
+----------+

| count(*) |
+----------+

| 2048000 |
+----------+

1 row in set (0.84 sec)

mysql> select count(*) from category_part as a inner join parent as b using(parent_id);
+----------+

| count(*) |
+----------+

| 2048000 |
+----------+

1 row in set (0.88 sec)
mysql> explain select count(*) from category as a inner join parent as b using(parent_id);
+----+-------------+-------+-------+---------------+-------------+---------+--------------------+-------+-------------+
| id | select_type | table | type  | possible_keys | key         | key_len | ref                | rows  | Extra       |
+----+-------------+-------+-------+---------------+-------------+---------+--------------------+-------+-------------+
|  1 | SIMPLE      | b     | index | PRIMARY       | PRIMARY     | 4       | NULL               |    25 | Using index |
|  1 | SIMPLE      | a     | ref   | f_parent_id   | f_parent_id | 4       | t_girl.b.parent_id | 81920 | Using index |
+----+-------------+-------+-------+---------------+-------------+---------+--------------------+-------+-------------+
2 rows in set (0.00 sec)

mysql> explain partitions select count(*) from category_part as a inner join parent as b using(parent_id);
+----+-------------+-------+----------------+-------+---------------+-------------+---------+--------------------+------+-------------+
| id | select_type | table | partitions     | type  | possible_keys | key         | key_len | ref                | rows | Extra       |
+----+-------------+-------+----------------+-------+---------------+-------------+---------+--------------------+------+-------------+
|  1 | SIMPLE      | b     | NULL           | index | PRIMARY       | PRIMARY     | 4       | NULL               |   25 | Using index |
|  1 | SIMPLE      | a     | p1,p2,p3,p4,p5 | ref   | f_parent_id   | f_parent_id | 4       | t_girl.b.parent_id | 6421 | Using index |
+----+-------------+-------+----------------+-------+---------------+-------------+---------+--------------------+------+-------------+
2 rows in set (0.00 sec)
可以看出,扫描的行数大幅度减少

mysql> explain select count(*) from category as a inner join parent as b using(parent_id) where a.parent_id =19;
+----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type  | possible_keys | key         | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | b     | const | PRIMARY       | PRIMARY     | 4       | const |    1 | Using index |
|  1 | SIMPLE      | a     | ref   | f_parent_id   | f_parent_id | 4       | const | 6746 | Using index |
+----+-------------+-------+-------+---------------+-------------+---------+-------+------+-------------+

2 rows in set (0.00 sec)

mysql> explain partitions select count(*) from category_part as a inner join parent as b using(parent_id) where a.parent_id =19;
+----+-------------+-------+------------+-------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key         | key_len | ref   | rows | Extra       |
+----+-------------+-------+------------+-------+---------------+-------------+---------+-------+------+-------------+
|  1 | SIMPLE      | b     | NULL       | const | PRIMARY       | PRIMARY     | 4       | const |    1 | Using index |
|  1 | SIMPLE      | a     | p5         | ref   | f_parent_id   | f_parent_id | 4       | const | 5203 | Using index |
+----+-------------+-------+------------+-------+---------------+-------------+---------+-------+------+-------------+
2 rows in set (0.00 sec)

由以上数据可以看出,数据越大,查询性能提升的越明显!

2、下来看看写性能
mysql> insert into category(cname,parent_id) values ('Test',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into category_part(cname,parent_id) values ('Test',1);
Query OK, 1 row affected (0.00 sec)


mysql> select * from category into outfile '/tmp/a.txt';
ERROR 1086 (HY000): File '/tmp/a.txt' already exists
mysql> select * from category into outfile '/tmp/test.dat';
Query OK, 2048005 rows affected (2.82 sec)

mysql> truncate table category;
Query OK, 0 rows affected (0.06 sec)

mysql> truncate table category_part;
Query OK, 2048005 rows affected (0.10 sec)


mysql> load data infile '/tmp/test.dat' into table category;
Query OK, 2048005 rows affected (17.67 sec)
Records: 2048005 Deleted: 0 Skipped: 0 Warnings: 0

mysql> load data infile '/tmp/test.dat' into table category_part;
Query OK, 2048005 rows affected (21.62 sec)
Records: 2048005 Deleted: 0 Skipped: 0 Warnings: 0


可以看出,写性能损失不了多少。
牺牲了少许写的性能却大幅度提高了查询的性能,这个是值得的。
如果我有什么说的不对的地方,欢迎各位提意见!

论坛徽章:
0
2 [报告]
发表于 2008-03-22 13:19 |只看该作者
对于分区大家讨论一下吧,好像没有人发表意见

论坛徽章:
0
3 [报告]
发表于 2008-03-23 00:16 |只看该作者
不会,向你学习......

论坛徽章:
0
4 [报告]
发表于 2008-03-23 09:43 |只看该作者

回复 #3 hahazhu0634 的帖子

大家一块学习!

论坛徽章:
0
5 [报告]
发表于 2008-03-23 20:52 |只看该作者
分区一般人,用不到呀

论坛徽章:
0
6 [报告]
发表于 2008-04-04 19:05 |只看该作者
顶一下 支持原创 写些东西出来不容易

论坛徽章:
0
7 [报告]
发表于 2008-04-09 17:17 |只看该作者
不错不错,支持原创

论坛徽章:
0
8 [报告]
发表于 2008-05-05 15:50 |只看该作者
顶一下,学习中。。。。。。。。。。。。。。。

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
9 [报告]
发表于 2008-05-20 16:28 |只看该作者
我想在我的数据库中用分区的功能
我们要做一个企业的考勤系统,想以每个月份分区,最多在数据库中保留两年的纪录
还要写存储过程,自动创建分区,删除分区的都要考虑呀

现在还没有怎么学习MySQL的分区功能
不知道有没有Oracle类似的分区移进移出的功能

MySQL的分区功能在5.1的版本中还是pre-alpha的状态
还是老实点用CentOS5.1自带的MySQL5.0.22的吧

[ 本帖最后由 ruochen 于 2008-5-20 16:56 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-05-20 17:04 |只看该作者
原帖由 ruochen 于 2008-5-20 16:28 发表
我想在我的数据库中用分区的功能
我们要做一个企业的考勤系统,想以每个月份分区,最多在数据库中保留两年的纪录
还要写存储过程,自动创建分区,删除分区的都要考虑呀

现在还没有怎么学习MySQL的分区功能 ...



不靠5.1的分区,你自己也可以手动分区。自己的逻辑,更加灵活。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP