登录
首页 >  数据库 >  MySQL

Mysql测试_索引对查询性能影响

来源:SegmentFault

时间:2023-01-11 13:52:46 141浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《Mysql测试_索引对查询性能影响》,聊聊MySQL,我们一起来看看吧!

1.环境信息
mysql-5.6.17-winx64

2.新建两张表,tb_big_data和tb_big_data2。tb_big_data数据1100000条,tb_big_data2数据2000条

mysql> select count(*) from tb_big_data;
+----------+
| count(*) |
+----------+
|  1100000 |
+----------+
1 row in set

mysql> select count(*) from tb_big_data2;
+----------+
| count(*) |
+----------+
|     2000 |
+----------+
1 row in set

3.执行join查询,查看执行join查询且结果为49000的查询时间

mysql> select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2;

4.对字段增加普通索引和不加索引,查询时间相差1000倍

mysql>  show profiles;
+----------+--------------+--------------------------------------------------------------------------------------------------------+
| Query_ID | Duration     | Query                                                                                                  |
+----------+--------------+--------------------------------------------------------------------------------------------------------+                                                                                 |
|        9 | 135.77425125 | select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2 |                                              
|       11 |   4.96485125 | ALTER TABLE tb_big_data ADD INDEX index_name (random)                                                  |
|       12 |   0.01095375 | describe tb_big_data                                                                                   |
|       13 |   0.14014425 | select * from tb_big_data2 b left join tb_big_data a on b.random_more = a.random where b.random_more=2 |
+----------+--------------+--------------------------------------------------------------------------------------------------------+

5.单列索引和多列索引
未创建索引,对两个字段过滤查询需要,结果为2条数据耗时6.4s,创建多列索引(count,random),耗时0.03s,创建两个单列索引,耗时0.08s。在对多个字段进行过滤查询时,多列索引和单列索引的性能还是不一样的。

mysql> show profiles;
+----------+-------------+-------------------------------------------------------------+
| Query_ID | Duration    | Query                                                       |
+----------+-------------+-------------------------------------------------------------+
|        8 |    6.439019 | select count(*) from tb_big_data where count=6 and random=2 |
|        9 |   44.570048 | create index index_name on tb_big_data(count,random)        |
|       10 |   0.0311755 | select count(*) from tb_big_data where count=6 and random=2 |                                       |
|       13 | 37.07460275 | create index index_name on tb_big_data(count)                                                     |
|       15 | 39.00397825 | create index index_name2 on tb_big_data(random)                                                     |
|       17 |  0.08649375 | select count(*) from tb_big_data where count=6 and random=2 |
+----------+-------------+-------------------------------------------------------------+

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表