使用explain来优化查询SQL

使用explain可以得知查询SQL的执行计划,这里主要介绍从explain的每列,我们可以看出什么

explain出的type列的值罗列如下,查询性能从好到坏

system

const

eq_ref

ref

fulltext

ref_or_null

index_merge

unique_subquery

index_subquery

range

index

all

这里主要说说经常碰到的const,eq_ref,ref,index,all

const可以推出:const is used when you compare all parts of a PRIMARY KEY or UNIQUE index to constant values;

eq_ref可以推出:It is used when all parts of an index are used by the join and the index is a PRIMARY KEY or UNIQUE NOT NULL index;

ref可以推出:ref is used if the join uses only a leftmost prefix of the key or if the key is not a PRIMARY KEY or UNIQUE index (in other words, if the join cannot select a single row based on the key value).

index,不一定表示使用了索引,要看extra列是否出现Using  index。

all,是全表扫描,没有走索引。

const,eq_ref,ref,index,all这几个里面一定走索引的是const,eq_ref,ref;可能走索引的是index,一定不走索引的是all。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值