计算mysqlexplain中key_len流程讲析

本文主要给大家介绍计算MySQL explain中key_len流程讲析,文章内容都是笔者用心摘选和编辑的,计算mysql explain中key_len流程讲析具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下主题内容吧。

10年积累的成都网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有紫金免费网站建设让你可以放心的选择与我们合作。

 

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流程讲析,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。

 

网站名称:计算mysqlexplain中key_len流程讲析
当前URL:https://www.cdcxhl.com/article30/jhdjso.html

成都网站建设公司_创新互联,为您提供网站建设建站公司网站内链软件开发网站营销搜索引擎优化

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司