MySQL分表优化(2)

来源:未知 责任编辑:责任编辑 发表时间:2014-05-10 12:32 点击:

+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
|t_group |     0 | PRIMARY     |      1 | id     |A    |  10388608 |  NULL | NULL |   | BTREE  |    |
| t_group |     1 | idx_user_name  |     1 | user_name | A    |     8 |  NULL | NULL |   |BTREE   |    |
| t_group |     1 | idx_combination1|      1 | user_name | A    |     8 |  NULL |NULL |   | BTREE   |    |
| t_group |     1 |idx_combination1 |      2 | money   | A    |    3776|  NULL | NULL |   | BTREE   |    |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
4 rows in set (0.00 sec)
 
  PS:
  idx_combination1 这个索引是必须的,因为要对user_name来GROUP BY。此时属于松散索引扫描!当然完了后你可以干掉她。
  idx_user_name 这个索引是为了加快单独执行constant这种类型的查询。
  我们要根据用户名来分表
 mysql> select user_name from t_group where 1 group by user_name;
+-----------+
| user_name |
+-----------+
| david  |
| leo   |
| livia  |
| lucy   |
| sarah  |
| simon  |
| sony   |
| sunny  |
+-----------+
8 rows in set (0.00 sec)
 
  所以结果表应该是这样的。
 
mysql> show tables like 't_group_%';
+------------------------------+
| Tables_in_t_girl (t_group_%) |
+------------------------------+
| t_group_david        |
| t_group_leo         |
| t_group_livia        |
| t_group_lucy        |
| t_group_sarah        |
| t_group_simon        |
| t_group_sony        |
| t_group_sunny        |
+------------------------------+
8 rows in set (0.00 sec)
 
  3、对比结果。
 
mysql> select count(*) from t_group where user_name = 'david';
+----------+
| count(*) |
+----------+
| 1298576 |
+----------+
1 row in set (1.71 sec)
 
  执行了将近2秒。
 
mysql> select count(*) from t_group_david;
+----------+
| count(*) |
+----------+
| 1298576 |
+----------+
1 row in set (0.00 sec)
 
  几乎是瞬间的。
 
mysql> select count(*) from t_group where user_name <> 'david';
+----------+
| count(*) |
+----------+
| 9090032 |
+----------+
1 row in set (9.26 sec)
执行了将近10秒,可以想象,这个是实际的项目中是不能忍受的。
mysql> select (select count(*) from t_group) - (select count(*) from t_group_david) as total;
+---------+
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • mysql-mmm
  • mysqldump命令——MySQL数据库备份还原
  • Oracle数据导入MySQL的快捷工具:MySQL Migration Toolkit
  • 简简单单储存过程——循环一个select结果集
  • MySQL数据库十大优化技巧
  • Mysql主主复制架构配置
  • Mysql安装笔记
  • MySQL Stmt预处理提高效率问题的小研究
  • Mysql的Procedure 参数为NULL问题分析
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1