Home >Database >Mysql Tutorial >查询优化之EXPLAIN的使用分析_MySQL

查询优化之EXPLAIN的使用分析_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:24:26940browse

bitsCN.com

不同的SQL语句写法,往往会带来很大的性能差异,我们怎么才能知道执行SQL查询开销呢?MySQL为我们提供了EXPLAIN关键词,在你的select语句前加上EXPLAIN关键词,MySQL将解释它是如何处理的SELECT查询,提供有关表如何联接和联接的次序,所扫描的记录数等相关信息,你可以凭借这些信息,来优化你的SQL查询。
EXPLAIN select id, username from userinfo where username like '%peng%';
我们在查询语句前加上了EXPLAIN关键词,那么我们可以得到如下的报告。

id: 1
select_type: SIMPLE
table: userinfo
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
rel: NULL
rows: 6
Extra: Using where

大概解释下每一个数据项的含义:
id:
SELECT识别符,SELECT的查询序列号;
select_type: SELECT类型,可以是SIMPLE(简单查询)、PRIMARY(最外面的select)等;
table:用到的表
type: 联接类型
possible_keys: 可用索引列
key: 实际用到的索引列
key_len: 键长度
rel: 使用哪个列或常数与key一起从表中选择行
rows: 检查的行数
Extra: 该列包含MySQL解决查询的详细信息

更具体的解释请查阅MySQL手册,在这里,把手册的内容再抄一遍并没有什么意义,当你有了这些参考数据后,就能更准确的完成MySQL查询优化了。

bitsCN.com
Statement:
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