欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql优化利器之explain使用介绍

程序员文章站 2023-12-03 09:33:10
一、语法 {explain | describe | desc} tbl_name [col_name | wild] {explain | descr...

一、语法

{explain | describe | desc}
  tbl_name [col_name | wild] 
{explain | describe | desc}
  [explain_type] select select_options 
explain_type: {extended | partitions}

二、数据库准备

表一:

drop table if exists `products`;
set @saved_cs_client   = @@character_set_client;
set character_set_client = utf8;
create table `products` (
 `products_id` int(11) unsigned not null auto_increment,
 `products_type` int(11) unsigned not null default '1',
 `products_quantity` float not null default '0',
 `products_model` varchar(32) default null,
 `products_upc` varchar(32) default null,
 `products_isbn` varchar(32) default null,
 `products_image` varchar(128) default null,
 `products_image_thumbnail` varchar(200) not null,
 `products_price` decimal(15,4) not null default '0.0000',
 `products_virtual` tinyint(1) not null default '0',
 `products_date_added` datetime not null default '0001-01-01 00:00:00',
 `products_last_modified` datetime default null,
 `products_date_available` datetime default null,
 `products_weight` float not null default '0',
 `products_status` tinyint(1) not null default '0',
 `products_tax_class_id` int(11) not null default '0',
 `manufacturers_id` int(11) default null,
 `products_web_id` int(11) default null,
 `products_ordered` float not null default '0',
 `products_quantity_order_min` float not null default '1',
 `products_quantity_order_units` float not null default '1',
 `products_priced_by_attribute` tinyint(1) not null default '0',
 `product_is_free` tinyint(1) not null default '0',
 `product_is_call` tinyint(1) not null default '0',
 `products_quantity_mixed` tinyint(1) not null default '0',
 `product_is_always_free_shipping` tinyint(1) not null default '0',
 `products_qty_box_status` tinyint(1) not null default '1',
 `products_quantity_order_max` float not null default '0',
 `products_sort_order` int(11) not null default '0',
 `products_discount_type` tinyint(1) not null default '0',
 `products_discount_type_from` tinyint(1) not null default '0',
 `products_price_sorter` decimal(15,4) not null default '0.0000',
 `master_categories_id` int(11) not null default '0',
 `products_mixed_discount_quantity` tinyint(1) not null default '1',
 `metatags_title_status` tinyint(1) not null default '0',
 `metatags_products_name_status` tinyint(1) not null default '0',
 `metatags_model_status` tinyint(1) not null default '0',
 `metatags_price_status` tinyint(1) not null default '0',
 `metatags_title_tagline_status` tinyint(1) not null default '0',
 `itemno` varchar(32) default null,
 `products_images_no` varchar(10) default '0',
 `products_url` varchar(512) default null,
 primary key (`products_id`),
 unique key `itemno` (`itemno`)
) engine=myisam auto_increment=1 default charset=utf8;
set character_set_client = @saved_cs_client;

表二:

drop table if exists `products_image`;
set @saved_cs_client   = @@character_set_client;
set character_set_client = utf8;
create table `products_image` (
 `id` int(10) unsigned not null auto_increment,
 `products_id` int(10) unsigned not null,
 `products_images_no` varchar(10) default '0',
 `image_dir` varchar(200) default null,
 `products_image_thumbnail` varchar(200) default null,
 `flag` int(2) default null,
 `up_time` datetime default null,
 `web_from` varchar(20) default null,
 primary key (`id`),
 key `idx_porducts_id` (`products_id`)
) engine=myisam auto_increment=1default charset=utf8;
set character_set_client = @saved_cs_client;

三、关于explain选项

下面是一个实例:

mysql> explain select products_id from products limit 1;
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key   | key_len | ref | rows | extra    |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | simple   | products | index | null     | primary | 4    | null | 3113 | using index |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+

id

mysql  query  optimizer选定的执行计划中查询的序列号。
表示查询中执行select子句或操作表的顺序,id值越大优先级越高,越先被执行。id相同,执行顺序由上至下

select_type

1、simple:简单的select查询,不使用union及子查询
2、primary:最外层的select查询
3、union:union中的第二个或随后的select查询,不依赖于外部查询的结果集
4、dependent union:union中的第二个或随后的select查询,依赖于外部查询的结果集
5、union result: union查询的结果集subquery子查询中的第一个select查询,不依赖于外部查询的结果集
6、dependent subquery:子查询中的第一个select查询,依赖于外部查询的结果集derived用于from子句里有子查询的情况。
    mysql会递归执行这些子查询,把结果放在临时表里。
7、uncacheable subquery:结果集不能被缓存的子查询,必须重新为外层查询的每一行进行评估
8、uncacheable union:union中的第二个或随后的select查询,属于不可缓存的子查询

table

1、system:表仅有一行(系统表)。这是const连接类型的一个特例。
2、const:const用于用常数值比较primary key时。当查询的表仅有一行时,使用system。
3、eq_ref:除const类型外最好的可能实现的连接类型。它用在一个索引的所有部分被连接使用并且索引是unique或primary key,
    对于每个索引键,表中只有一条记录与之匹配。
4、ref:连接不能基于关键字选择单个行,可能查找到多个符合条件的行。叫做ref是因为索引要跟某个参考值相比较。
    这个参考值或者是一个常数,或者是来自一个表里的多表查询的结果值。
5、ref_or_null:如同ref,但是mysql必须在初次查找的结果里找出null条目,然后进行二次查找。
6、index_merge:说明索引合并优化被使用了。
7、unique_subquery:在某些in查询中使用此种类型,而不是常规的ref:
    value in (select primary_key from single_table where some_expr)
    index_subquery在某些in查询中使用此种类型,与unique_subquery类似,但是查询的是非唯一性索引:
    value in (select key_column from single_table where some_expr)
8、range:只检索给定范围的行,使用一个索引来选择行。key列显示使用了哪个索引。
    当使用=、<>、>、>=、<、<=、is null、<=>、between或者in操作符,用常量比较关键字列时,可以使用range。
9、index:全表扫描,只是扫描表的时候按照索引次序进行而不是行。主要优点就是避免了排序,但是开销仍然非常大。
10、all:最坏的情况,从头到尾全表扫描

 others

possible_keys:指出mysql能在该表中使用哪些索引有助于查询。如果为空,说明没有可用的索引
key:mysql实际从possible_key选择使用的索引。如果为null,则没有使用索引。
    很少的情况下,mysql会选择优化不足的索引。这种情况下,
    可以在select语句中使用use  index(indexname)来强制使用一个索引
    或者用ignore  index(indexname)来强制mysql忽略索引
key_len:使用的索引的长度。在不损失精确性的情况下,长度越短越好
ref:显示索引的哪一列被使用了
rows:mysql认为必须检查的用来返回请求数据的行数

extra

1、distinct: 一旦mysql找到了与行相联合匹配的行,就不再搜索了。
2、not exists: mysql 优化了left join,一旦它找到了匹配left join标准的行,就不再搜索了。
3、range checked for each: record(index map:#)没有找到理想的索引,
    因此对于从前面表中来的每一个行组合,mysql检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一。
4、using filesort: 表示mysql会对结果使用一个外部索引排序,而不是从表里按索引次序读到相关内容。
    可能在内存或者磁盘上进行排序。mysql中无法利用索引完成的排序操作称为“文件排序”。
5、using index: 列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,
    这发生在对表的全部的请求列都是同一个索引的部分的时候。
6、using temporary: mysql需要创建一个临时表来存储结果,这通常发生在对不同的列集进行order by上,而不是group by上。
7、using where: 使用了where从句来限制哪些行将与下一张表匹配或者是返回给用户。
    如果不想返回表中的全部行,并且连接类型all或index,这就会发生,或者是查询有问题。

四、具体的实例

1、mysql版本

mysql> select version();
+------------+
| version() |
+------------+
| 5.1.73-log |
+------------+
1 row in set (0.00 sec)

2、sql语句分析1

mysql> explain select products_id from products; 
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key   | key_len | ref | rows | extra    |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
| 1 | simple   | products | index | null     | primary | 4    | null | 3113 | using index |
+----+-------------+----------+-------+---------------+---------+---------+------+------+-------------

3、sql语句分析2

mysql> explain select products_id from (select * from products limit 10) b ;    
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table   | type | possible_keys | key | key_len | ref | rows | extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| 1 | primary   | <derived2> | all | null     | null | null  | null |  10 |    |
| 2 | derived   | products  | all | null     | null | null  | null | 3113 |    |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+

4、sql语句分析3

mysql> explain select products_id from products where products_id=10 union select products_id \
from products where products_id=20 ;
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table   | type | possible_keys | key   | key_len | ref  | rows | extra    |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | primary   | products  | const | primary    | primary | 4    | const |  1 | using index |
| 2 | union    | products  | const | primary    | primary | 4    | const |  1 | using index |
| null | union result | <union1,2> | all  | null     | null  | null  | null | null |       |
+----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+

5、sql语句分析4

mysql> explain select * from products where products_id in ( select products_id from products where \
products_id=10 union select products_id from products where products_id=20 );
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type    | table   | type | possible_keys | key   | key_len | ref  | rows | extra    |
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | primary      | products  | all  | null     | null  | null  | null | 3113 | using where |
| 2 | dependent subquery | products  | const | primary    | primary | 4    | const |  1 | using index |
| 3 | dependent union  | products  | const | primary    | primary | 4    | const |  1 | using index |
| null | union result    | <union2,3> | all  | null     | null  | null  | null | null |       |
+----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+

完成