Mysql之sql语句优化:explain
来源:互联网 发布:淘宝页头显示在哪里 编辑:程序博客网 时间:2024/04/19 07:39
- explain的列分析
- select_type 查询类型
- table 查询针对的表
- possible_key 可能用到的索引
- key 最终用的索引
- key_len 使用的索引的最大长度
- type列 是指查询的方式 非常重要是分析查数据过程的重要依据
- ref列 指连接查询时 表之间的字段引用关系
- rows 是指估计要扫描多少行
- extra
explain的列分析
id: 代表select 语句的编号, 如果是连接查询,表之间是平等关系, select 编号都是1,从1开始. 如果某select中有子查询,则编号递增.
mysql> explain select goods_id,goods_name from goods where goods_id in (select goods_id from goods where cat_id=4) \G*************************** 1. row *************************** id: 1 select_type: PRIMARY table: goods type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 31 Extra: Using where*************************** 2. row *************************** id: 2 select_type: DEPENDENT SUBQUERY table: goods type: unique_subquerypossible_keys: PRIMARY,cat_id key: PRIMARY key_len: 3 ref: func rows: 1 Extra: Using where2 rows in set (0.00 sec)
select_type: 查询类型
- simple (不含子查询)
- primary (含子查询或派生查询)
- subquery (非from子查询)
- derived (from型子查询)
- union
- union result
table: 查询针对的表
有可能是
实际的表名 如select * from t1;
表的别名 如 select * from t2 as tmp;
derived 如from型子查询时
null 直接计算得结果,不用走表
possible_key: 可能用到的索引
注意: 系统估计可能用的几个索引,但最终,只能用1个.
key : 最终用的索引.
key_len: 使用的索引的最大长度
type列: 是指查询的方式, 非常重要,是分析”查数据过程”的重要依据
可能的值
- all: 意味着从表的第1行,往后,逐行做全表扫描.,运气不好扫描到最后一行.
例: 把goods_name列上的索引去掉, 并根据goods_name来查询
mysql> explain select goods_name from goods where goods_name='诺基亚N85' \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: ALLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 31 Extra: Using where1 row in set (0.00 sec)
- index: 比all性能稍好一点,
通俗的说: all 扫描所有的数据行,相当于data_all index 扫描所有的索引节点,相当于index_all
2种情况可能出现:
1:索引覆盖的查询情况下, 能利用上索引,但是又必须全索引扫描.
mysql> explain select goods_id from goods where goods_id=1 or goods_id+1>20\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: indexpossible_keys: PRIMARY key: PRIMARY key_len: 3 ref: NULL rows: 31 Extra: Using where; Using index1 row in set (0.00 sec)mysql> explain select goods_id,click_count from goods where goods_id=1 or goods_id+1>20 \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: ALLpossible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 31 Extra: Using where1 row in set (0.00 sec)
2: 是利用索引来进行排序,但取出所有的节点select goods_id from goods order by goods_id desc;分析: 没有加where条件, 就得取所有索引节点,同时,又没有回行,只取索引节点.再排序,经过所有索引节点.mysql> explain select goods_id from goods order by goods_id asc\G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: indexpossible_keys: NULL key: PRIMARY key_len: 3 ref: NULL rows: 31 Extra: Using index1 row in set (0.00 sec)
- range: 意思是查询时,能根据索引做范围的扫描
mysql> explain select goods_id,goods_name,shop_price from goods where goodsid >25 \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: rangepossible_keys: PRIMARY key: PRIMARY key_len: 3 ref: NULL rows: 8 Extra: Using where1 row in set (0.00 sec)
- ref 意思是指 通过索引列,可以直接引用到某些数据行
mysql> explain select goods_id,goods_name from goods where cat_id=4 \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: refpossible_keys: cat_id key: cat_id key_len: 2 ref: const rows: 3 Extra:1 row in set (0.00 sec)
在这个例子中,通过cat_id索引 指向N行goods数据,来查得结果.
- eq_ref 是指,通过索引列,直接引用某1行数据
常见于连接查询中
mysql> explain select goods_id,shop_price from goods innert join ecs_categoy using(cat_id) where goods_id> 25 \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: innert type: rangepossible_keys: PRIMARY,cat_id key: PRIMARY key_len: 3 ref: NULL rows: 8 Extra: Using where*************************** 2. row *************************** id: 1 select_type: SIMPLE table: ecs_category type: eq_refpossible_keys: PRIMARY key: PRIMARY key_len: 2 ref: shop.innert.cat_id rows: 1 Extra: Using index2 rows in set (0.00 sec)
- const, system, null 这3个分别指查询优化到常量级别, 甚至不需要查找时间.
一般按照主键来查询时,易出现const,system
或者直接查询某个表达式,不经过表时, 出现NULL
mysql> explain select goods_id,goods_name,click_count from goods wher_id=4 \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: constpossible_keys: PRIMARY key: PRIMARY key_len: 3 ref: const rows: 1 Extra:1 row in set (0.00 sec)mysql> explain select max(goods_id) from goods \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: NULL type: NULL myisam表的max,min,count在表中优化过,不需要\真正查找,为NULLpossible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: Select tables optimized away1 row in set (0.00 sec)
ref列 指连接查询时, 表之间的字段引用关系.
mysql> explain select goods_id,cat_name,goods_name from goods inner join ec_category using(cat_id) where ecs_category.cat_name='' \G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: goods type: ALLpossible_keys: cat_id key: NULL key_len: NULL ref: NULL rows: 31 Extra:*************************** 2. row *************************** id: 1 select_type: SIMPLE table: ecs_category type: eq_refpossible_keys: PRIMARY key: PRIMARY key_len: 2 ref: shop. goods.cat_id rows: 1 Extra: Using where2 rows in set (0.00 sec)
rows : 是指估计要扫描多少行.
extra:
- index: 是指用到了索引覆盖,效率非常高
- using where 是指光靠索引定位不了,还得where判断一下
- using temporary 是指用上了临时表, group by 与order by 不同列时,或group by ,order by 别的表的列.
- using filesort : 文件排序(文件可能在磁盘,也可能在内存), (?????
select sum(shop_price) from goods group by cat_id(???? 这句话,用到了临时表和文件排序)
0 0
- Mysql之sql语句优化:explain
- mysql优化之EXPLAIN语句
- MySQL优化 之 用explain分析低效率的sql语句
- mysql sql 优化之explain
- mysql使用explain优化sql语句
- MySQL SQL优化之 执行计划 explain
- mysql执行sql语句优化分析命令explain命令小记
- mysql执行sql语句优化分析命令explain命令小记
- mysql优化–explain分析sql语句执行效率
- mysql优化–explain分析sql语句执行效率
- MySQL查询优化语句--explain
- 性能优化之MySQL优化(二)- explain分析SQL
- mysql 利用explain优化sql
- sql语句前加入explain优化sql
- MYSQL之“EXPLAIN语句简介”
- mysql查询语句优化命令mysql explain
- sql 优化之EXPLAIN PLAN
- MySQL查询优化之explain
- JavaEE框架Bootstrap HTML5 jQuery SpringMVC maven
- 【arvik开源项目】impush简介
- Spring基于注解TestContext 测试框架使用详解
- 二叉树的深层性质
- Maven安装配置
- Mysql之sql语句优化:explain
- ReactNative中文网推出的代码热更新服务
- java 企业网站源码 后台 springmvc SSM 前台 静态化 代码生成器
- 注释 Android Framework 中的一堆 debug 信息
- Qt 渐变色Gradient
- 【整理】Adam Taylor玩转Microzed系列——以太网通信部分
- An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module
- 微信自适应页面
- rsync远程同步+inotify-tools自动检测笔记