一、语法

  1. {EXPLAIN | DESCRIBE | DESC}
  2. tbl_name [col_name | wild]
  3.  
  4. {EXPLAIN | DESCRIBE | DESC}
  5. [explain_type] SELECT select_options
  6.  
  7. explain_type: {EXTENDED | PARTITIONS}

二、数据库准备

表一:

  1. DROP TABLE IF EXISTS `products`;
  2. SET @saved_cs_client = @@character_set_client;
  3. SET character_set_client = utf8;
  4. CREATE TABLE `products` (
  5. `products_id` int(11) unsigned NOT NULL auto_increment,
  6. `products_type` int(11) unsigned NOT NULL default '1',
  7. `products_quantity` float NOT NULL default '0',
  8. `products_model` varchar(32) default NULL,
  9. `products_upc` varchar(32) default NULL,
  10. `products_isbn` varchar(32) default NULL,
  11. `products_image` varchar(128) default NULL,
  12. `products_image_thumbnail` varchar(200) NOT NULL
  13. `products_price` decimal(15,4) NOT NULL default '0.0000',
  14. `products_virtual` tinyint(1) NOT NULL default '0',
  15. `products_date_added` datetime NOT NULL default '0001-01-01 00:00:00',
  16. `products_last_modified` datetime default NULL,
  17. `products_date_available` datetime default NULL,
  18. `products_weight` float NOT NULL default '0',
  19. `products_status` tinyint(1) NOT NULL default '0',
  20. `products_tax_class_id` int(11) NOT NULL default '0',
  21. `manufacturers_id` int(11) default NULL,
  22. `products_web_id` int(11) default NULL,
  23. `products_ordered` float NOT NULL default '0',
  24. `products_quantity_order_min` float NOT NULL default '1',
  25. `products_quantity_order_units` float NOT NULL default '1',
  26. `products_priced_by_attribute` tinyint(1) NOT NULL default '0',
  27. `product_is_free` tinyint(1) NOT NULL default '0',
  28. `product_is_call` tinyint(1) NOT NULL default '0',
  29. `products_quantity_mixed` tinyint(1) NOT NULL default '0',
  30. `product_is_always_free_shipping` tinyint(1) NOT NULL default '0',
  31. `products_qty_box_status` tinyint(1) NOT NULL default '1',
  32. `products_quantity_order_max` float NOT NULL default '0',
  33. `products_sort_order` int(11) NOT NULL default '0',
  34. `products_discount_type` tinyint(1) NOT NULL default '0',
  35. `products_discount_type_from` tinyint(1) NOT NULL default '0',
  36. `products_price_sorter` decimal(15,4) NOT NULL default '0.0000',
  37. `master_categories_id` int(11) NOT NULL default '0',
  38. `products_mixed_discount_quantity` tinyint(1) NOT NULL default '1',
  39. `metatags_title_status` tinyint(1) NOT NULL default '0',
  40. `metatags_products_name_status` tinyint(1) NOT NULL default '0',
  41. `metatags_model_status` tinyint(1) NOT NULL default '0',
  42. `metatags_price_status` tinyint(1) NOT NULL default '0',
  43. `metatags_title_tagline_status` tinyint(1) NOT NULL default '0',
  44. `itemno` varchar(32) default NULL,
  45. `products_images_no` varchar(10) default '0',
  46. `products_url` varchar(512) default NULL,
  47. PRIMARY KEY (`products_id`),
  48. UNIQUE KEY `itemno` (`itemno`)
  49. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  50. SET character_set_client = @saved_cs_client;

表二:

  1. DROP TABLE IF EXISTS `products_image`;
  2. SET @saved_cs_client = @@character_set_client;
  3. SET character_set_client = utf8;
  4. CREATE TABLE `products_image` (
  5. `id` int(10) unsigned NOT NULL auto_increment,
  6. `products_id` int(10) unsigned NOT NULL,
  7. `products_images_no` varchar(10) default '0'
  8. `image_dir` varchar(200) default NULL,
  9. `products_image_thumbnail` varchar(200) default NULL
  10. `flag` int(2) default NULL,
  11. `up_time` datetime default NULL,
  12. `web_from` varchar(20) default NULL,
  13. PRIMARY KEY (`id`),
  14. KEY `idx_porducts_id` (`products_id`)
  15. ) ENGINE=MyISAM AUTO_INCREMENT=1DEFAULT CHARSET=utf8;
  16. SET character_set_client = @saved_cs_client;

三、关于explain选项

下面是一个实例:

  1. mysql> explain select products_id from products limit 1;
  2. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
  5. | 1 | SIMPLE | products | index | NULL | PRIMARY | 4 | NULL | 3113 | Using index |
  6. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+

id

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

select_type

  1. 1SIMPLE:简单的select查询,不使用union及子查询
  2. 2PRIMARY:最外层的select查询
  3. 3UNIONUNION中的第二个或随后的select查询,不依赖于外部查询的结果集
  4. 4DEPENDENT UNIONUNION中的第二个或随后的select查询,依赖于外部查询的结果集
  5. 5UNION RESULT UNION查询的结果集SUBQUERY子查询中的第一个select查询,不依赖于外部查询的结果集
  6. 6DEPENDENT SUBQUERY:子查询中的第一个select查询,依赖于外部查询的结果集DERIVED用于from子句里有子查询的情况。
  7. MySQL会递归执行这些子查询,把结果放在临时表里。
  8. 7UNCACHEABLE SUBQUERY:结果集不能被缓存的子查询,必须重新为外层查询的每一行进行评估
  9. 8UNCACHEABLE UNION:UNION中的第二个或随后的select查询,属于不可缓存的子查询

table

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

others

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

extra

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

 四、具体的实例

1、mysql版本

  1. mysql> select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.1.73-log |
  6. +------------+
  7. 1 row in set (0.00 sec)

2、sql语句分析1

  1. mysql> explain select products_id from products;
  2. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------+
  5. | 1 | SIMPLE | products | index | NULL | PRIMARY | 4 | NULL | 3113 | Using index |
  6. +----+-------------+----------+-------+---------------+---------+---------+------+------+-------------

3、sql语句分析2

  1. mysql> explain select products_id from (select * from products limit 10) b ;
  2. +----+-------------+------------+------+---------------+------+---------+------+------+-------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+------------+------+---------------+------+---------+------+------+-------+
  5. | 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | |
  6. | 2 | DERIVED | products | ALL | NULL | NULL | NULL | NULL | 3113 | |
  7. +----+-------------+------------+------+---------------+------+---------+------+------+-------+

4、sql语句分析3

  1. mysql> explain select products_id from products where products_id=10 union select products_id \
  2. from products where products_id=20 ;
  3. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
  4. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  5. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+
  6. | 1 | PRIMARY | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  7. | 2 | UNION | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  8. | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | |
  9. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------------+

5、sql语句分析4

  1. mysql> explain select * from products where products_id in ( select products_id from products where \
  2. products_id=10 union select products_id from products where products_id=20 );
  3. +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
  4. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  5. +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+
  6. | 1 | PRIMARY | products | ALL | NULL | NULL | NULL | NULL | 3113 | Using where |
  7. | 2 | DEPENDENT SUBQUERY | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  8. | 3 | DEPENDENT UNION | products | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
  9. | NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | |
  10. +----+--------------------+------------+-------+---------------+---------+---------+-------+------+-------------+

mysql优化利器之explain的更多相关文章

  1. Mysql优化基础之Explain工具

    字段解释 id:代表sql中查询语句的序列号,序列号越大则执行的优先级越高,序号一样谁在前谁先执行.id为null则最后执行 select_type:查询类型,表示当前被分析的sql语句的查询的复杂度 ...

  2. 一次浴火重生的MySQL优化(EXPLAIN命令详解)

    一直对SQL优化的技能心存无限的向往,之前面试的时候有很多面试官都会来一句,你会优化吗?我说我不太会,这时可能很多人就会有点儿说法了,比如会说不要使用通配符*去检索表.给常常使用的列建立索引.还有创建 ...

  3. 小白学习mysql之优化基础(EXPLAIN的连接类型)

    ## 导语很多情况下,有很多人用各种select语句查询到了他们想要的数据后,往往便以为工作圆满结束了.这些事情往往发生在一些学生亦或刚入职场但之前又没有很好数据库基础的小白身上,但所谓闻道有先后,只 ...

  4. mysql优化(三)–explain分析sql语句执行效率

    mysql优化(三)–explain分析sql语句执行效率 mushu 发布于 11个月前 (06-04) 分类:Mysql 阅读(651) 评论(0) Explain命令在解决数据库性能上是第一推荐 ...

  5. MySQL优化—工欲善其事,必先利其器之EXPLAIN(转)

    最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...

  6. MySQL优化—工欲善其事,必先利其器之EXPLAIN

    最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...

  7. mysql优化:explain分析sql语句执行效率

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...

  8. MySQL — 优化之explain执行计划详解(转)

    EXPLAIN简介 EXPLAIN 命令是查看查询优化器如何决定执行查询的主要方法,使用EXPLAIN,只需要在查询中的SELECT关键字之前增加EXPLAIN这个词即可,MYSQL会在查询上设置一个 ...

  9. MYSQL优化浅谈,工具及优化点介绍,mysqldumpslow,pt-query-digest,explain等

    MYSQL优化浅谈 msyql是开发常用的关系型数据库,快速.稳定.开源等优点就不说了. 个人认为,项目上线,标志着一个项目真正的开始.从运维,到反馈,到再分析,再版本迭代,再优化… 这是一个漫长且考 ...

随机推荐

  1. A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28

    上面回答有问题,找到qq官方的文档了 http://service.exmail.qq.com/cgi-bin/help?id=28&no=1000585&subtype=1 如果您的 ...

  2. 精选 5 个漂亮的 CSS3 图片滑过特效

    这篇文章将为大家分享5款漂亮的CSS3图片滑过特效,比如滑过后显示图片的详细文字介绍,又比如滑过后对图片进行淡入淡出的效果等等.让我们一起来看看,喜欢的朋友赶紧收藏. 1.非常酷的CSS3图片说明效果 ...

  3. HostMonitor监控主机状态

    HostMonitor 可以对windows和linux下的主机进行很多信息的监控,还提供web方式查看

  4. Coursera课程《大家的python》(Python for everyone)课件

    You can access the Google Drive containing all of the current and in-progress lecture slides for thi ...

  5. OpenCV特征检测教程

    http://docs.opencv.org/2.4/doc/tutorials/features2d/table_of_content_features2d/table_of_content_fea ...

  6. android中一些特殊字符(如:←↑→↓等箭头符号)的Unicode码值

    在项目中,有时候在一些控件(如Button.TextView)中要添加一些符号,如下图所示:                          这个时候可以使用图片的方式来显示,不过这些可以直接使用U ...

  7. TreeListControl:设置行样式

    <Style x:Key="OddEvenRowStyle" TargetType="{x:Type dxg:GridRowContent}"> & ...

  8. 在CentOS/RHEL上设置SSH免密码登录

    本文会告诉你怎样在 CentOS/RHEL 上设置 SSH 免密码登录.自动登录配置好以后,你可以通过它使用 SSH (Secure Shell)和安全复制 (SCP)来移动文件. SSH 是开源的, ...

  9. Inner Classes with TypeScript

    原文:https://blog.oio.de/2014/03/21/inner-classes-typescript/ b.ts class Foo { sex:string; say(){ new ...

  10. nyoj-659-推断三角形(大坑)

    推断三角形 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 小明非常喜欢研究三角形.如今,小明已经知道三角形的三条边.假设三条边能组成三角形,小明就会非常高兴,他就会得 ...