浅谈mysqlexplain中key_len的计算方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了浅谈mysqlexplain中key_len的计算方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3354字,纯文字阅读大概需要5分钟。
内容图文
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中EXPLAIN解释命令及用法讲解
- Mysql中explain作用详解
- mysql优化利器之explain使用介绍
- mysql explain的用法(使用explain优化查询语句)
- Mysql Explain 详细介绍
- 简述Mysql Explain 命令
- MySQL中EXPLAIN命令详解
- MySQL查询优化之explain的深入解析
- mysql中explain用法详解
- Mysql之EXPLAIN显示using filesort介绍
- mysql总结之explain
- MySQL性能分析及explain的使用说明
- MYSQL explain 执行计划
- mysql之explain使用详解(分析索引)
内容总结
以上是互联网集市为您收集整理的浅谈mysqlexplain中key_len的计算方法全部内容,希望文章能够帮你解决浅谈mysqlexplain中key_len的计算方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。