关于mysql explain中key_len的计算方法讲解


本文摘自PHP中文网,作者jacklove,侵删。

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

1.创建测试表及数据

1

2

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。

1

2

3

4

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再测试

1

2

3

4

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再测试

1

2

3

4

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的计算方法,更多相关内容请关注php中文网。

相关推荐:
如何通过php 使用curl模拟ip和来源进行访问

通过mysql 转换NULL数据方法

关于php 函数使用可变数量的参数的相关内容

以上就是关于mysql explain中key_len的计算方法讲解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

删除数据库的命令是什么?

mysql如何查询时间段

mysql有几种隔离级别

mysql如何查询子节点

navicat如何给mysql还原数据库

[深度liunx系统]安装数据库mysqlmysql workbench

理解mysql存储过程和函数

mysql如何远程连接端口并修改

mysql怎么查询今天的数据

mysql中explain用法和结果分析(详解)

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...