SQL优化

2019-04-11  本文已影响0人  蜗牛ICU
EXPLAIN

说明:

EXPLAIN SELECT * FROM 表名;
id select_type table type possible_keys key ken_len ref rows Extra
1 SIMPLE 表名 ALL 38

結果说明:

一、 id:

二、 select_type:

id select_type table type possible_keys key ken_len ref rows Extra
1 PEIMARY ALL 38
1 DERIVED 表名 ALL 38 Useing where
id select_type table type possible_keys key ken_len ref rows Extra
1 PEIMARY tb_order ALL 2
2 UNION tb_order ALL 2
UNION RESULT union1,2 ALL 2 Useing where
    explain select * from tb_order where user_id = (select id from tb_user where name like '%lidong%')
    
id select_type table type possible_keys key ken_len ref rows Extra
1 PEIMARY tb_order ref FK_orders_1 FK_order 8 const 1 using where
2 SUBQUERY tb_user ALL 7 using where

三 、table: 查询的表

四、type:

id select_type table type possible_keys key ken_len ref rows Extra
1 SIMPLE tb_order const PRIMRY PRIMAR 4 const 1
id select_type table type possible_keys key ken_len ref rows Extra
1 SIMPLE 表1 ALL fk_orders_1 1
2 SIMPLE 表2 eq_ref PRIMARY PRIMARY 8 MYBAT 1
id select_type table type possible_keys key ken_len ref rows Extra
1 SIMPLE tb_order ref order_number 62 const 1 using index

分析:

五、possible_keys

指出MySQL能使用哪个索引在该表中找到行。
如果该列为NULL,说明没有使用索引,可以对该列创建索引来提高性能。

六、key

显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。

可以强制使用索引或者忽略索引:

强制忽略索引:
    explain select * from tb_user igore index(age) where age < 10
 
强制使用索引:
    explain select * from tb_user use index(age) where age < 10

七、 key_len

显示MySQL决定使用的键长度。如果键是NULL,则长度为NULL。

注意:key_len是确定了MySQL将实际使用的索引长度。

八、 ref

显示使用哪个列或常数与key一起从表中选择行。

九、rows

显示MySQL认为它执行查询时必须检查的行数

十、Extra

该列包含MySQL解决查询的详细信息

下面几种情况下索引不会生效:

优化 MYSQL 参数:

上一篇下一篇

猜你喜欢

热点阅读