MySQL的四种不同查询的分析

发表于:2012-3-01 09:38

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:inter12    来源:51Testing软件测试网采编

  1、前置条件:

  本次是基于小数据量,且数据块在一个页中的最理想情况进行分析,可能无具体的实际意义,但是可以借鉴到各种复杂条件下,因为原理是相同的,知小见大,见微知著!

  打开语句分析并确认是否已经打开

  1. mysql> set profiling=1;     
  2. Query OK, 0 rows affected (0.00 sec)  
  3. mysql> select @@profiling;  
  4. +-------------+  
  5. | @@profiling |  
  6. +-------------+  
  7. |           1 |  
  8. +-------------+  
  9. 1 row in set (0.01 sec)

  2、数据准备:

  2.1 全表扫描数据

  1. create table person4all(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id));  
  2. insert into person4all(name,gender) values("zhaoming","male");  
  3. insert into person4all(name,gender) values("wenwen","female");

  2.2 根据主键查看数据

  1. create table person4pri(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id));  
  2. insert into person4pri(name,gender) values("zhaoming","male");  
  3. insert into person4pri(name,gender) values("wenwen","female");

  2.3 根据非聚集索引查数据

  1. create table person4index(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id) , index(gender));  
  2. insert into person4index(name,gender) values("zhaoming","male");  
  3. insert into person4index(name,gender) values("wenwen","female");

  2.4 根据覆盖索引查数据

  1. create table person4cindex(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id) , index(name,gender));    
  2. insert into person4cindex(name,gender) values("zhaoming","male");    
  3. insert into person4cindex(name,gender) values("wenwen","female");

  主要从以下几个方面分析:查询消耗的时间,走的执行计划等方面。

  3、开工测试

  第一步:全表扫描

  1. mysql> select * from person4all ;  
  2. +----+----------+--------+  
  3. | id | name     | gender |  
  4. +----+----------+--------+  
  5. |  1 | zhaoming | male   |  
  6. |  2 | wenwen   | female |  
  7. +----+----------+--------+  
  8. 2 rows in set (0.00 sec)

  查看其执行计划:

  1. mysql> explain select * from person4all;  
  2. +----+-------------+------------+------+---------------+------+---------+------+------+-------+  
  3. | id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |  
  4. +----+-------------+------------+------+---------------+------+---------+------+------+-------+  
  5. |  1 | SIMPLE      | person4all | ALL  | NULL          | NULL | NULL    | NULL |    2 |       |  
  6. +----+-------------+------------+------+---------------+------+---------+------+------+-------+  
  7. 1 row in set (0.01 sec)

  我们可以很清晰的看到走的是全表扫描,而没有走索引!

  查询消耗的时间:

  1. mysql> show profiles;  
  2. +----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+  
  3. | Query_ID | Duration   | Query                                                                                                                             |  
  4. |       54 | 0.00177300 | select * from person4all                                                                                                          |  
  5. |       55 | 0.00069200 | explain select * from person4all                                                                                                  |  
  6. +----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+

41/41234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号