mysql中explain的使用
explain
使用explain来查看查询过程 可以从结果反映查询的过程从而优化查询
- 从查询结果来看,主要看key type 和rows:
- table:查询的表格.
- possible_keys:可能用到的索引
- key_len索引长度
- ref:显示索引哪一列被使用
- key:使用的索引
- rows:扫描查询的列数
- type:查询连接类型,是较为重要的一个指标,结果值从好到坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL 常见的有六种
All <index<range<ref<eq_ref<const
- ALL 表示全盘扫描,非常原生态的查询方法
- index 按照索引全盘扫描
- range 一定范围缩影扫描 between in and or > <
- ref 查询条件(where)是索引但不是主键和unique 不需要全盘扫描,找到符合条件的列会在附近小范围扫描
- eq_ref 使用了主键或者唯一性索引的情况下进行查找
- const where后面接主键查询