MySQL索引效能測試

hxw2ljj發表於2015-10-30

MySQL索引效能測試

 

blog文件結構圖:

wpsEBD3.tmp

 

很長一段時間沒學習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

[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做測試吧:

wpsEBE5.tmp 

wpsEBE6.tmp 

 

 

簡單測試索引效能

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>

 

 

總結

表名

表屬性

查詢(單位:秒)

插入(單位:秒)

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章