三、MySQL优化之explain执行计划key_len、ref、rows的属性

explain之key_len:使用索引的字节数

表示索引使用的字节数,key通过该列计算查询中使用的索引长度,
在不损失精确性的情况下,长度越小越好

key_len显示的值为索引的最大可能长度,并非实际使用长度
即key_len是根据表定义计算而得,不是通过表内检索出来的

mysql> explain select * from t1 where id_card='622421' and tel='156'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t1
         type: ref
possible_keys: inx_idcard_tel
          key: inx_idcard_tel
      key_len: 93
          ref: const,const
         rows: 1
        Extra: Using index condition
----------------------------
mysql> explain select * from t1 where id_card='622421'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t1
         type: ref
possible_keys: inx_idcard_tel
          key: inx_idcard_tel
      key_len: 57
          ref: const
         rows: 1
        Extra: Using index condition

通过上面两个例子:数据越精确,key_len的值越大 同样查询条件下,key_len的值越小越好

explain之ref:type访问类型的具体值

ref是type访问类型具体的一个值

显示索引的哪些列被使用,如果可能的话是常量const,哪些列或常量被用于查找索引列上的值

mysql> explain select * from t1,t2 where t1.t2_id=t2.id and t1.id_card='622421';
+----+-------------+-------+--------+----------------+----------------+---------+---------------+------+-----------------------+
| id | select_type | table | type   | possible_keys  | key            | key_len | ref           | rows | Extra                 |
+----+-------------+-------+--------+----------------+----------------+---------+---------------+------+-----------------------+
|  1 | SIMPLE      | t1    | ref    | inx_idcard_tel | inx_idcard_tel | 57      | const         |    1 | Using index condition |
|  1 | SIMPLE      | t2    | eq_ref | PRIMARY        | PRIMARY        | 4       | demo.t1.t2_id |    1 | NULL                  |
+----+-------------+-------+--------+----------------+----------------+---------+---------------+------+-----------------------+

从上面例子可以看出:id_card是使用了常量,t1.t2_id具体的关联字段

explain之rows:查询时影响的行数

根据表统计信息及索引选用情况大致估算出找到所需数据所需要的读取的行数

简单而言,就是查询多少行数据才能得到自己所需要的的数据

每张表有多少行记录被优化器查询,越少越好

-- 查询总数
mysql> select * from t1;
+----+-------+-------+---------+------+------+
| id | t2_id | t3_id | id_card | tel  | name |
+----+-------+-------+---------+------+------+
|  3 |     4 |     3 | 622421  | 156  | 张三 |
|  4 |     5 |     2 | 622321  | 131  | 李四 |
|  5 |     6 |     1 | 621421  | 132  | 王五 |
|  6 |     4 |     3 | 620422  | 177  | 赵六 |
+----+-------+-------+---------+------+------+
-- t2_id=4 查询条数
mysql> select * from t1 where t2_id = 4;
+----+-------+-------+---------+------+------+
| id | t2_id | t3_id | id_card | tel  | name |
+----+-------+-------+---------+------+------+
|  3 |     4 |     3 | 622421  | 156  | 张三 |
|  6 |     4 |     3 | 620422  | 177  | 赵六 |
+----+-------+-------+---------+------+------+
-- 执行计划
mysql> explain select * from t1 where t2_id=4\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: t1
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra: Using where

t1表一共有4条数据,t2_id=4的有两条
从执行计划可以看出type=all,全表扫描,所以rows=4
所以查询t2_id=4的两条数据时,在查询了4条数据(全表扫描)中找到了所需要的数据

如果想要优化上面例子,不让全表扫描,在t2_id字段上建立索引
alter table t1 add index idx_t2_id (t2_id)
create index idx_t2_id on t1(t2_id)
以上两种建索引方式任选一种,再次执行上述SQL的explain,得到的rows=2

写在最后

人这一生也没有多少时间可以挥霍,
踏实一点,务实一些,
做自己想做的事,
如此简单!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值