• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

浅谈mysql explain中key_len的计算方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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的计算方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持极客世界。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
mysql 转换NULL数据方法(必看)发布时间:2022-02-08
下一篇:
MySQL通过触发器解决数据库中表的行数限制详解及实例发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap