T086学习网 | 站长学院 | 技术文档 | 成语 | 歇后语 | 帝国时代 | 代码收藏 | IP地址查询 | 生活百科 | 生日密码 | CSS压缩 | 用户评论 | 欣欣百宝箱

MySQL分表优化试验

【 网络 更新时间:2010-04-02 | 字体:
[导读]我们的项目中有好多不等于的情况。今天写这篇文章简单的分析一下怎么个优化法。 这里的分表逻辑是根据t_group表的user_name组的个数来分的。 因为这种情况单独user_name字段上的索引就属于烂索引。起不了啥名明显的效...

索引情况。

mysql> show index from t_group;
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table   | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| 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;
+---------+
| total   |
+---------+
| 9090032 |
+---------+
1 row in set (0.00 sec)
几乎是瞬间的。

原文:http://yueliangdao0608.blog.51cto.com/397025/107356

  • 转载请注明来源:IT学习网 网址:http://www.t086.com/ 向您的朋友推荐此文章
  • 特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系我们,我们会尽快予以更正。
更多
留言建议ASP探针PHP探针站长Enjoy的Blog
© 2017 T086学习网 - T086.com(原itlearner.com)
RunTime:8.41ms QueryTime:7