设为首页 收藏本站
查看: 647|回复: 0

[经验分享] MySQL索引性能测试

[复制链接]

尚未签到

发表于 2015-12-22 11:59:13 | 显示全部楼层 |阅读模式
MySQL索引性能测试
 
blog文档结构图:
DSC0000.jpg
 

很长一段时间没学习MySQL了,一直致力于oracle的研究,最近得空了就再拾起MySQL看看吧,记得去年发布过的2篇MySQL文章:

      MySQL 5.6.21下载安装之下载篇(一)  : http://blog.itpub.net/26736162/viewspace-1349705/
    MySQL 5.6.21下载安装之安装篇(二): http://blog.itpub.net/26736162/viewspace-1349787/

今天我们就来看看mysql中索引的性能测试:
准备环境
1.1  在数据库中创建测试表test1
  [iyunv@rhel6_lhr ~]# mysql -p
  Enter password:
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection>
  Server version: 5.6.21-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)
   
  Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
   
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
   
  mysql> show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | db1                |
  | db4                |
  | lhr_test           |
  | mysql              |
  | opensource         |
  | opesource          |
  | performance_schema |
  | test               |
  | wyzc               |
  +--------------------+
  10 rows in set (0.00 sec)
   
  mysql> use lhr_test;
  Reading table information for completion of table and column names
  You can turn off this feature to get a quicker startup with -A
   
  Database changed
  mysql> create table test1(
      ->>
      -> num int,
      -> pass varchar(50)
      -> );
  Query OK, 0 rows affected (0.01 sec)
   
  mysql> desc test1;
  +-------+-------------+------+-----+---------+-------+
  | Field | Type        | Null | Key | Default | Extra |
  +-------+-------------+------+-----+---------+-------+
  |>
  | num   | int(11)     | YES  |     | NULL    |       |
  | pass  | varchar(50) | YES  |     | NULL    |       |
  +-------+-------------+------+-----+---------+-------+
  3 rows in set (0.00 sec)
   
  mysql> create table test2(
      -> >
      ->  num int,
      ->  pass varchar(50),
      ->  index>
      ->  );
  Query OK, 0 rows affected (0.24 sec)
   
  mysql> create table test3(
      -> >
      ->  num int,
      ->  pass varchar(50)
      ->  );
   reset query cache;Query OK, 0 rows affected (0.09 sec)
   
   
   
 

1.2  在系统提示符下执行如下语句创建100万行数据
在操作系统执行命令,生成100W条数据,其中-plhr中的lhr为mysql的密码,lhr_test为数据库名,该命令为一整条命令: for ((i=1;i /tmp/mysql.txt   2>&1  

 

经过漫长的等待后查看数据,受不了了,直接20W做测试吧:

DSC0001.png  

DSC0002.png  

 

 

简单测试索引性能
2.1  在有索引和没有索引的情况下执行查询
 

  mysql> select * from test1 limit 10;
  +------+------+----------------------------------+
  |>
  +------+------+----------------------------------+
  |    1 |    1 | c4ca4238a0b923820dcc509a6f75849b |
  |    2 |    3 | c81e728d9d4c2f636f067f89cc14862c |
  |    3 |    4 | eccbc87e4b5ce2fe28308fd9f2a7baf3 |
  |    4 |    5 | a87ff679a2f3e71d9181a67b7542122c |
  |    5 |    5 | e4da3b7fbbce2345d7772b0674a318d5 |
  |    6 |    6 | 1679091c5a880faf6fb5e6087eb1b2dc |
  |    7 |   10 | 8f14e45fceea167a5a36dedd4bea2543 |
  |    8 |   12 | c9f0f895fb98ab9159f51fd0297e236d |
  |    9 |   12 | 45c48cce2e2d7fbdea1afc51c7c6ad26 |
  |   10 |   12 | d3d9446802a44259755d38e6d163e820 |
  +------+------+----------------------------------+
  10 rows in set (0.00 sec)
   
  mysql> reset query cache; ---清空缓存
  Query OK, 0 rows affected (0.18 sec)
   
  mysql> select num,pass   from test1 where>=50000 and>
  +-------+----------------------------------+
  | num   | pass                             |
  +-------+----------------------------------+
  | 66755 | 1017bfd4673955ffee4641ad3d481b1c |
  | 70239 | 334146de1b9346272cb013adf1a35aea |
  | 93704 | f67fe69d3660b4d35a731817b538b21d |
  | 90594 | 0cce9d48eb96fdf93fbae8640d547b8e |
  | 94673 | 171ab172efb24344684eca5b04abffca |
  | 51476 | e7beb1dcf073b1d1e700fb02eccaf064 |
  | 78604 | 2432fc2efe99899b0ecff8ade0211e7d |
  | 62059 | 1cc41f4ab8528178818a29b9ef5fabbb |
  | 94777 | 79c3489e2392afd26733d285dee3abd0 |
  | 71807 | f0547ecd4e64a31e247c34b64547f812 |
  | 62106 | 3e53ae683f8e8c84221db763b30fe907 |
  | 74578 | fbbbadb6d1a15c0c924c73b0b0a4b7cb |
  | 91069 | 9ba86c2987b9321a45b4dbf1eff6bb4a |
  | 71692 | 8ec41a3e649625a55ceafc35f6fa45e8 |
  | 72179 | 87b0cba64000c51c883f57274c04519c |
  | 96570 | f7c3c4088dfe80933e84ca084fa3524a |
  | 70295 | 5a5a84625f44b7e7345b4ea6fde06627 |
  | 68758 | 85dbdb1cbb78b9be83ccedd468732e0a |
  | 77853 | 16d37a42180158171d57e1cc8122b415 |
  | 81193 | c3a8217c9d3a5d9c5e76a77d8f4a8fde |
  | 71512 | 4e2598d3fa41ae72d1927b81328dbd51 |
  | 87838 | 81b993dae9d5735b0714c325c526aee5 |
  | 51719 | d3d2a1a264feb84bd8ba9d0557aafca8 |
  | 88469 | d5e390212ea61535b492b740102df78a |
  | 92232 | cf50b28ef624912ff106c57ca9be41dc |
  | 67030 | a4eacdf08e8fda83c7784c8fd21f7811 |
  | 93650 | 9a84af5408986faab11f648a07867d84 |
  | 70810 | 31bd7cc9213175d709fcfa2eeb4b202a |
  | 77757 | 2d084a4acd512e6314d6e8ae111b8205 |
  | 66555 | 2a12b41adeedc754b55ec468d1a41d09 |
  | 77727 | 33702a9c691c0f5aaac103d7dd1952eb |
  | 92189 | d081111dbdee3c687d1439b444d64004 |
  | 90372 | dfdc9e0c03a33349408e99f28d07f899 |
  | 91206 | 0610027c7b4268080e7c1c5f04af05a7 |
  | 77903 | cb07accc409bbb4c0adc6afb26cf351b |
  | 55402 | a1f3a4e959c66a4dd4f330f13ff4d808 |
  | 75258 | 9f75e281cbe6072bd91a286e64fb6f0d |
  | 88422 | 9bf3f8e2f454487987a4888544f9e1be |
  | 76166 | 664a26f366b9ef4988631e95af9b366d |
  | 87260 | f19ea2ad04c46f33134d405510650a60 |
  | 73856 | 3b4421d0ab0e43c65932c51fb58f593f |
  | 89850 | ca355f31b8e517abc70bf477ca77f4ce |
  | 84650 | afa8024de2c03966e71d6f94a93b6b93 |
  | 63982 | ce9e053a63f6a8aed199bed09f1e498e |
  | 92551 | 3cf419e05d85881157b758a01c6ef399 |
  | 80764 | af7994b458c40e4a18ec60f5e622e522 |
  | 50054 | c3beb22d8bb8a4b874fd7bb8a8914643 |
  | 72336 | 5f9f76d679371d223deeda050bdc9d85 |
  | 85120 | 218171bd4087237acdcc6d3846b9cda5 |
  | 85468 | d38aad5d5676be87eaf6ade964caff4f |
  +-------+----------------------------------+
  50 rows in set (0.15 sec)
   
   
  mysql> insert into test2 select * from test1;
  Query OK, 225494 rows affected (2.53 sec)
  Records: 225494  Duplicates: 0  Warnings: 0
   
  mysql>  reset query cache;
  Query OK, 0 rows affected (0.00 sec)
   
  mysql> explain select num,pass   from test2 where>=50000 and>
  +----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
  |>
  +----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
  |  1 | SIMPLE      | test2 | range |>
  +----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
  1 row in set (0.00 sec)
   
  mysql>  explain select num,pass   from test1  where>=50000 and>
  +----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
  |>
  +----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
  |  1 | SIMPLE      | test1 | ALL  | NULL          | NULL | NULL    | NULL | 226851 | Using where |
  +----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
  1 row in set (0.00 sec)
   
  mysql> reset query cache;
  Query OK, 0 rows affected (0.00 sec)
   
  mysql>  select num,pass   from test2 where>=50000 and>
  +-------+----------------------------------+
  | num   | pass                             |
  +-------+----------------------------------+
  | 66755 | 1017bfd4673955ffee4641ad3d481b1c |
  | 70239 | 334146de1b9346272cb013adf1a35aea |
  | 93704 | f67fe69d3660b4d35a731817b538b21d |
  | 90594 | 0cce9d48eb96fdf93fbae8640d547b8e |
  | 94673 | 171ab172efb24344684eca5b04abffca |
  | 51476 | e7beb1dcf073b1d1e700fb02eccaf064 |
  | 78604 | 2432fc2efe99899b0ecff8ade0211e7d |
  | 62059 | 1cc41f4ab8528178818a29b9ef5fabbb |
  | 94777 | 79c3489e2392afd26733d285dee3abd0 |
  | 71807 | f0547ecd4e64a31e247c34b64547f812 |
  | 62106 | 3e53ae683f8e8c84221db763b30fe907 |
  | 74578 | fbbbadb6d1a15c0c924c73b0b0a4b7cb |
  | 91069 | 9ba86c2987b9321a45b4dbf1eff6bb4a |
  | 71692 | 8ec41a3e649625a55ceafc35f6fa45e8 |
  | 72179 | 87b0cba64000c51c883f57274c04519c |
  | 96570 | f7c3c4088dfe80933e84ca084fa3524a |
  | 70295 | 5a5a84625f44b7e7345b4ea6fde06627 |
  | 68758 | 85dbdb1cbb78b9be83ccedd468732e0a |
  | 77853 | 16d37a42180158171d57e1cc8122b415 |
  | 81193 | c3a8217c9d3a5d9c5e76a77d8f4a8fde |
  | 71512 | 4e2598d3fa41ae72d1927b81328dbd51 |
  | 87838 | 81b993dae9d5735b0714c325c526aee5 |
  | 51719 | d3d2a1a264feb84bd8ba9d0557aafca8 |
  | 88469 | d5e390212ea61535b492b740102df78a |
  | 92232 | cf50b28ef624912ff106c57ca9be41dc |
  | 67030 | a4eacdf08e8fda83c7784c8fd21f7811 |
  | 93650 | 9a84af5408986faab11f648a07867d84 |
  | 70810 | 31bd7cc9213175d709fcfa2eeb4b202a |
  | 77757 | 2d084a4acd512e6314d6e8ae111b8205 |
  | 66555 | 2a12b41adeedc754b55ec468d1a41d09 |
  | 77727 | 33702a9c691c0f5aaac103d7dd1952eb |
  | 92189 | d081111dbdee3c687d1439b444d64004 |
  | 90372 | dfdc9e0c03a33349408e99f28d07f899 |
  | 91206 | 0610027c7b4268080e7c1c5f04af05a7 |
  | 77903 | cb07accc409bbb4c0adc6afb26cf351b |
  | 55402 | a1f3a4e959c66a4dd4f330f13ff4d808 |
  | 75258 | 9f75e281cbe6072bd91a286e64fb6f0d |
  | 88422 | 9bf3f8e2f454487987a4888544f9e1be |
  | 76166 | 664a26f366b9ef4988631e95af9b366d |
  | 87260 | f19ea2ad04c46f33134d405510650a60 |
  | 73856 | 3b4421d0ab0e43c65932c51fb58f593f |
  | 89850 | ca355f31b8e517abc70bf477ca77f4ce |
  | 84650 | afa8024de2c03966e71d6f94a93b6b93 |
  | 63982 | ce9e053a63f6a8aed199bed09f1e498e |
  | 92551 | 3cf419e05d85881157b758a01c6ef399 |
  | 80764 | af7994b458c40e4a18ec60f5e622e522 |
  | 50054 | c3beb22d8bb8a4b874fd7bb8a8914643 |
  | 72336 | 5f9f76d679371d223deeda050bdc9d85 |
  | 85120 | 218171bd4087237acdcc6d3846b9cda5 |
  | 85468 | d38aad5d5676be87eaf6ade964caff4f |
  +-------+----------------------------------+
  50 rows in set (0.00 sec)
   
 

 

2.2  在有索引和没有索引的情况下新增数据
 

 

   
  mysql>  reset query cache;
  Query OK, 0 rows affected (0.00 sec)
   
  mysql>  insert into test3 select * from test1;
  Query OK, 225494 rows affected (1.67 sec)
  Records: 225494  Duplicates: 0  Warnings: 0
   
  mysql>
 

 

总结
表名

表属性

查询(单位:秒)

插入(单位:秒)

test1

无索引

0.15

 

test2

有索引

0.00

2.53

test3

无索引

 

1.67

 

 

结论:通常情况下,有索引的情况下查询比较快,插入比较慢,所以在大批量的数据导入操作中应该首先删除索引,待数据导入完成后再建立索引,由于深入学习过oracle,感觉在这里说这些似乎是废话,但认真做实验是一种态度,本blog中总有一些你不知道的,o(∩_∩)o 哈哈。

 

 

 

 

 

.............................................................................................................................

本文作者:小麦苗,只专注于数据库的技术,更注重技术的运用

ITPUB BLOG:http://blog.itpub.net/26736162

本文地址: http://blog.itpub.net/26736162/viewspace-1466094/

QQ:642808185 注明:ITPUB的文章标题


.............................................................................................................................

 

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-154741-1-1.html 上篇帖子: Ubuntu 14.10 安装 Nginx + MySQL + HHVM + phpMyAdmin [LNMH] 下篇帖子: MySQL 5.6.21下载安装之安装篇(二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表