EXPLAIN执行计划中的key_len大小计算

key_len

背景-优化sql时发现一条sql未使用到索引

mysql> explain SELECT `id`,`vip`,`level`,`is_dormant` FROM channel WHERE `staff_code`=15011603318612105450;
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys  | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | channel | NULL       | ALL  | idx_staff_code | NULL | NULL    | NULL |   11 |    10.00 | Using where |
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
1 row in set, 3 warnings (0.01 sec)

查了下索引,发现该字段已经上了索引,进而查了下字段类型发现是隐式转换导致的未用到索引

mysql> show index from channel;
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| channel |          0 | PRIMARY        |            1 | id          | A         |          11 |     NULL | NULL   |      | BTREE      |         |               |
| channel |          1 | idx_staff_code |            1 | staff_code  | A         |           5 |     NULL | NULL   |      | BTREE      |         |               |
| channel |          1 | idx_code       |            1 | code        | A         |          11 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.02 sec)

//查询条件类型不对,导致全表扫描,每次计算都要把字段转为int型来比较,所以全表扫描

mysql> explain SELECT `id`,`vip`,`level`,`is_dormant` FROM channel WHERE `staff_code`=15011603318612105450;
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys  | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | channel | NULL       | ALL  | idx_staff_code | NULL | NULL    | NULL |   11 |    10.00 | Using where |
+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
1 row in set, 3 warnings (0.01 sec)

//改变了where条件这下用上了索引,但索引的长度为82,字段长度为20,进而想到索引的长度是怎么计算的

mysql> explain SELECT `id`,`vip`,`level`,`is_dormant` FROM channel WHERE `staff_code`='15011603318612105450';
+----+-------------+---------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys  | key            | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | channel | NULL       | ref  | idx_staff_code | idx_staff_code | 82      | const |    4 |   100.00 | NULL  |
+----+-------------+---------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)

字符集utf8mb4_general_ci(一个字符占四个字节)

如下:
staff_code varchar(20 ) utf8mb4_general_ci not null 

key_len = 4*20(字符长度)+ 2 (变长字符要多留2个字节)

staff_code varchar(20 ) utf8mb4_general_ci null

key_len = 4*20(字符长度)+ 2 (变长字符要多留2个字节)

其他类型都可以推算,keylen和字符集,是否为null,以及字段类型相关
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文主要总结了工作中一些常用的操作及不合理的操作,在对慢查询进行优化时收集的一些有用的资料和信息,本文适合有MyS...
    Chting阅读 3,772评论 0 1
  • 这篇文章主要涉及到MySQL的知识点: 索引(包括分类及优化方式,失效条件,底层结构) sql语法(join,un...
    一根薯条阅读 7,789评论 0 8
  • MySQL技术内幕:SQL编程 姜承尧 第1章 SQL编程 >> B是由MySQL创始人之一Monty分支的一个版...
    沉默剑士阅读 7,286评论 0 3
  • 我在想 何时才能得到所求 狭窄的空间压迫了思想 看着世人的嘴脸 抚摸着疼痛的心 有点卑微有点懦弱却又是深知 天空飘...
    窗外蓝天白云阅读 1,211评论 1 6
  • 亲爱的宝贝: 曾经被一篇《贫穷限制了我的想象力》的文章啪啪打脸,一只Prada回形针1200元,一只Raf Sim...
    鑫雨亲子讲师阅读 3,003评论 0 0

友情链接更多精彩内容