MySQL索引性能测试
MySQL索引性能测试
blog文档结构图:
很长一段时间没学习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.1 在数据库中创建测试表test1
[root@rhel6_lhr ~]# mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
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(
-> id int,
-> num int,
-> pass varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| num | int(11) | YES | | NULL | |
| pass | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> create table test2(
-> id int,
-> num int,
-> pass varchar(50),
-> index idIdx (id)
-> );
Query OK, 0 rows affected (0.24 sec)
mysql> create table test3(
-> id int,
-> 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<=1000000;i++));do `mysql -plhr lhr_test -e "insert into test1 values($i,floor($i+rand()*$i),md5($i));"`; done > /tmp/mysql.txt 2>&1
经过漫长的等待后查看数据,受不了了,直接20W做测试吧:
2 简单测试索引性能
2.1 在有索引和没有索引的情况下执行查询
mysql> select * from test1 limit 10;
+------+------+----------------------------------+
| id | num | pass |
+------+------+----------------------------------+
| 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 id>=50000 and id<50050;
+-------+----------------------------------+
| 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 id>=50000 and id<50050;
+----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
| 1 | SIMPLE | test2 | range | idIdx | idIdx | 5 | NULL | 49 | Using index condition |
+----+-------------+-------+-------+---------------+-------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
mysql> explain select num,pass from test1 where id>=50000 and id<50050;
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+
| 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 id>=50000 and id<50050;
+-------+----------------------------------+
| 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>
3 总结
表名 |
表属性 |
查询(单位:秒) |
插入(单位:秒) |
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的文章标题
.............................................................................................................................
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-1466094/,如需转载,请注明出处,否则将追究法律责任。
MySQL索引性能测试相关推荐
- mysql索引详解_MySQL索引详解
导读:大家都知道,一个MySQL数据库能够储存大量的数据,如果要查找那一个数据,就得费好大劲从一大堆的数据中找到,即费时间又费力气,这时,索引的出现就大大减轻了数据库管理员的工作.本文介绍了数据库索引 ...
- MySQL 索引详解
本文介绍了数据库索引,及其优.缺点.针对MySQL索引的特点.应用进行了详细的描述.分析了如何避免MySQL无法使用,如何使用EXPLAIN分析查询语句,如何优化MySQL索引的应用. 索引是一种特殊 ...
- mysql索引有几种使用索引的好处_mysql索引的类型和优缺点
mysql索引的类型和优缺点 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针. 注:[1]索引不是万能的!索引可以加快数据检索操作,但 ...
- mysql索引空间太大_MySQL优化索引
1. MySQL如何使用索引 索引用于快速查找具有特定列值的行.如果没有索引,MySQL必须从第一行开始,然后遍历整个表以找到相关的行.表越大,花费越多.如果表中有相关列的索引,MySQL可以快速确 ...
- mysql索引教程_MySQL教程96-MySQL索引类型
索引的类型和存储引擎有关,每种存储引擎所支持的索引类型不一定完全相同.MySQL 索引可以从存储方式.逻辑角度和实际使用的角度来进行分类. 存储方式区分 根据存储方式的不同,MySQL 中常用的索引在 ...
- mysql 树形结构_再读MySQL索引-《高性能MySQL》索引手记
最近工作中经常和MySQL打交道,当数据量小的时候,不同查询方式以及是否使用索引并无大碍,当数据量随着业务的成长急剧加速时,索引的重要性不言而喻. 本篇文章以<高性能MySQL>中的索引章 ...
- MySQL索引背后的数据结构及算法原理【转】
http://blog.codinglabs.org/articles/theory-of-mysql-index.html MySQL索引背后的数据结构及算法原理[转] 摘要 本文以MySQL数据库 ...
- mysql 索引合并
索引合并是mysql底层为我们提供的智能算法.本文就介绍了mysql 索引合并的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 索引合并是mysql底层为我们提 ...
- mysql中groupby会用到索引吗_开发人员不得不知的MySQL索引和查询优化
本文主要总结了工作中一些常用的操作及不合理的操作,在对慢查询进行优化时收集的一些有用的资料和信息,本文适合有 MySQL 基础的开发人员. 索引相关 索引基数 基数是数据列所包含的不同值的数量,例如, ...
- mysql索引底层实现原理_mysql的索引底层之实现原理
MySQL索引背后的数据结构及算法原理 一.定义 索引定义:索引(Index)是帮助MySQL高效获取数据的数据结构. 本质:索引是数据结构. 二.B-Tree m阶B-Tree满足以下条件: 1.每 ...
最新文章
- Struts权威著作
- chvg改变vg中LV的数量
- MCU中printf重定向实现
- 替换html标签内容正则表达式,正则表达式,替换所有HTML标签的简单实例
- 《走遍中国》珍藏版(十三)
- c语言下标法与指针法,《C和指针》中关于指针与下标的问题
- markdown、LaTeX输入自定义算符Res
- linux内核版本 2.6.39,编译RedHat Linux-2.6.39内核
- js对本地文件进行加密_怎么对电脑文件进行加密
- mysql 中文转拼音(简拼/全拼) 支持多音
- 微信小程序--Todos案例
- wps2016热点永久关闭
- 问佛----如果浮躁了,静下来看看
- 移动硬盘文件系统错误16389
- 几时几分几秒怎么写_几分几秒怎么写
- 长沙小吃比较好吃and著名的地方
- 介绍一下我大二开发的游戏:地下城冒险
- linux查看内存命令(查看进程虚拟内存)free命令、vmstat命令、pmap命令(free指令、vmstat指令、pmap指令)
- 干海星怎么吃做法 干海星的功效与作用有哪些
- Kettle邮件发送