Home > Database > Mysql Tutorial > body text

mysql explain_MySQL

WBOY
Release: 2016-06-01 13:52:15
Original
808 people have browsed it

MySQLexplain

explain的语法如下:

explain [extended] select … from … where …

如果使用了extended,那么在执行完explain语句后,可以使用show warnings语句查询相应的优化信息。

比如我们执行  select uid from user where uname=’scofield’ order by uid  执行结果会有


 | id | select_type | table | type  | possible_keys     | key     | key_len | ref   | rows | Extra |

这些东西。

其中 table 表示是哪个表的数据。

type比较重要。表示链接的类型。链接类型由好到坏的,依次是    system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

一般情况,至少要达到 range 级别,最好是 ref 级别。否则可能会有性能问题。

possible_keys 是指可以应用到该表的索引,如果为NULL则没有。

key 是指用到的索引。

key_len 是索引的长度,在不影响查询精度的情况下,值越小越好。

ref 是指索引的那一列被使用了。一般会是个常数。

rows 是指有多少行。

extra 是指额外的信息。也是比较重要的。如果值为 distinct ,说明mysql 找到了域行联合匹配的行,就不再查找了。

如果值为 not exits  :  mysql优化了 left join ,一旦找到了 left join 匹配的行,便不再进行搜索了。

如果值为 rang checked for each :  没有找到理想的索引。

如果为 using filesort ,则需要改进sql了。这说明 mysql执行 需要 文件排序。这是比较影响效率的。

如果为  using  temporary , 这是使用了 临时表。 这种情况也比较影响效率,sql需要改进。或者从应用层进行改进。

如果为 where used 说明使用了where语句。如果 type为 all 或者 index ,一般会出现这样的结果。这样的问题,一般是查询需要改进。

在一般稍大的系统中,基本尽可能的减少 join ,子查询 等等。mysql就使用最简单的查询,这样效率最高。至于 join 等,可以放在应用层去解决。

转自:http://www.gosoa.com.cn/mysql-explain-%e7%ac%94%e8%ae%b0%e6%95%b4%e7%90%86/

参考:http://www.cnitblog.com/aliyiyi08/archive/2008/09/09/48878.html

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!