主页 > 知识库 > 浅谈mysql explain中key_len的计算方法

浅谈mysql explain中key_len的计算方法

热门标签:地方门户网站 呼叫中心市场需求 铁路电话系统 Linux服务器 服务外包 网站排名优化 AI电销 百度竞价排名

mysql的explain命令可以分析sql的性能,其中有一项是key_len(索引的长度)的统计。本文将分析mysql explain中key_len的计算方法。

1、创建测试表及数据

CREATE TABLE `member` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(20) DEFAULT NULL,
 `age` tinyint(3) unsigned DEFAULT NULL,
 PRIMARY KEY (`id`),
 KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `member` (`id`, `name`, `age`) VALUES (NULL, 'fdipzone', '18'), (NULL, 'jim', '19'), (NULL, 'tom', '19');

2、查看explain

name的字段类型是varchar(20),字符编码是utf8,一个字符占用3个字节,那么key_len应该是 20*3=60

mysql> explain select * from `member` where name='fdipzone';
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref  | rows | Extra         |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| 1 | SIMPLE   | member | ref | name     | name | 63   | const |  1 | Using index condition |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+

explain的key_len为63,多出了3

name字段是允许NULL,把name改为NOT NULL再测试

ALTER TABLE `member` CHANGE `name` `name` VARCHAR(20) NOT NULL;

mysql> explain select * from `member` where name='fdipzone';
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref  | rows | Extra         |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| 1 | SIMPLE   | member | ref | name     | name | 62   | const |  1 | Using index condition |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+

现在key_len为62,比刚才少了1,但还是多了2。可以确定,字段为NULL会多占用一个字节。

name字段类型为varchar,属于变长字段,把varchar改为char再测试

ALTER TABLE `member` CHANGE `name` `name` CHAR(20) NOT NULL;

mysql> explain select * from `member` where name='fdipzone';
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref  | rows | Extra         |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
| 1 | SIMPLE   | member | ref | name     | name | 60   | const |  1 | Using index condition |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+

改为定长字段后,key_len为60,与预测的一致。

总结:使用变长字段需要额外增加2个字节,使用NULL需要额外增加1个字节,因此对于是索引的字段,最好使用定长和NOT NULL定义,提高性能。

以上这篇浅谈mysql explain中key_len的计算方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • mysql计算时间差函数
  • Mysql中通过生日计算年龄的多种方法
  • 在php和MySql中计算时间差的方法
  • mysql 字符串长度计算实现代码(gb2312+utf8)
  • Mysql数据库的QPS和TPS的意义和计算方法
  • MySQL几点重要的性能指标计算和优化方法总结
  • MySQL的查询计划中ken_len的值计算方法
  • mysql日期和时间的间隔计算实例分析
  • MySQL日期加减函数详解
  • mysql 触发器创建与使用方法示例
  • MySQL触发器基本用法详解【创建、查看、删除等】
  • mysql累加计算实现方法详解

标签:衡水 湘潭 湖南 兰州 铜川 崇左 黄山 仙桃

巨人网络通讯声明:本文标题《浅谈mysql explain中key_len的计算方法》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266