MySQL执行计划中key_len详解
(1).索引字段的附加信息:可以分为变长和定长数据类型讨论,当索引字段为定长数据类型,比如char,int,datetime,需要有是否为空的标记,这个标记需要占用1个字节;对于变长数据类型,比如:varchar,除了是否为空的标记外,还需要有长度信息,需要占用2个字节;
(备注:当字段定义为非空的时候,是否为空的标记将不占用字节)
(2).同时还需要考虑表所使用的字符集,不同的字符集,gbk编码的为一个字符2个字节,utf8编码的一个字符3个字节;
先看定长数据类型的一个例子(编码为gbk):
root@test 07:32:39>create table test_char(id int not null ,name_1 char(20),name_2 char(20), -> primary key(id),key ind_name(name_1,name_2))engine=innodb charset=gbk; root@test 07:33:55>insert into test_char values(1,’xuancan’,'taobaodba’); root@test 07:34:55>explain select * from test_char where name_1=’xuancan’\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_char type: ref possible_keys: ind_name key: ind_name key_len: 41 ref: const rows: 1 Extra: Using where; Using index key_len=41=20*2+1(备注:由于name_1为空,isnull的标记被打上,需要计算1个字节) root@test 07:35:31>explain select * from test_char where name_1=’xuancan’ and name_2=’taobaodba’\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_char type: ref possible_keys: ind_name key: ind_name key_len: 82 ref: const,const rows: 1 Extra: Using where; Using index key_len=82=20*2+20*2+1+1(备注:由于name_1,name_2两列被使用到,但两列都为为空,需要计算2个字节)
变长数据类型(gbk编码):
root@test 08:30:51>create table test_varchar(id int not null ,name_1 varchar(20),name_2 varchar(20), -> primary key(id),key ind_name(name_1,name_2))engine=innodb charset=gbk; root@test 08:37:51>insert into test_varchar values(1,’xuancan’,'taobaodba’); root@test 08:38:14>explain select * from test_varchar where name_1=’xuancan’\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_varchar type: ref possible_keys: ind_name key: ind_name key_len: 43 ref: const rows: 1 Extra: Using where; Using index key_len=43=20*2+1+2(备注:由于为name_1字段定义为空,所以需要计算1,;同时由于是变长字段varchar,所以需要加上2) root@test 08:38:46>alter table test_varchar modify column name_1 varchar(20) not null; Query OK, 1 row affected (0.52 sec) Records: 1 Duplicates: 0 Warnings: 0 root@test 08:42:11>explain select * from test_varchar where name_1=’xuancan’\G; *************************** 1. row *************************** id: 1 select_type: SIMPLE table: test_varchar type: ref possible_keys: ind_name key: ind_name key_len: 42 ref: const rows: 1 Extra: Using where; Using index key_len=42=20*2+2(备注由于name_1字段修改为not null之后,isnull的标记锁占用的字节释放掉,但是变长字段长度所占用的2个字节没有释放); 上面是测试gbk编码的测试,同时也可以测试一下其他编码的key_len计算。
MySQL执行计划中key_len详解的更多相关文章
- MySQL 执行计划中Extra(Using where,Using index,Using index condition,Using index,Using where)的浅析
关于如何理解MySQL执行计划中Extra列的Using where.Using Index.Using index condition,Using index,Using where这四者的区别 ...
- oracle获取执行计划及优缺点 详解
一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了):1. explain plan for获取: 2. set autotrace on : 3. statistics_leve ...
- MySQL 执行计划中Extra的浅薄理解
1.using where: Extra中出现“Using where”,通常来说,意味着全表扫描或者在查找使用索引的情况下,但是还有查询条件不在索引字段当中. 如果需要回表也是用这个. 2.usin ...
- MySQL执行计划extra中的using index 和 using where using index 的区别
本文出处:http://www.cnblogs.com/wy123/p/7366486.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...
- 读懂MySQL执行计划
原文:https://mp.weixin.qq.com/s/-BlLvBKcF-yalELY7XkqaQ 前言 在之前的面试过程中,问到执行计划,有很多童鞋不知道是什么?甚至将执行计划与执行时间认为是 ...
- MySQL 执行计划explain详解
MySQL 执行计划explain详解 2015-08-10 13:56:27 分类: MySQL explain命令是查看查询优化器如何决定执行查询的主要方法.这个功能有局限性,并不总会说出真相,但 ...
- MySQL 执行计划详解
我们经常使用 MySQL 的执行计划来查看 SQL 语句的执行效率,接下来分析执行计划的各个显示内容. EXPLAIN SELECT * FROM users WHERE id IN (SELECT ...
- 解读EXPLAIN执行计划中的key_len(转)
原文:http://imysql.com/2015/10/20/mysql-faq-key-len-in-explain.shtml 导读 EXPLAIN中的key_len一列表示什么意思,该如何解读 ...
- MySQL执行计划 EXPLAIN参数
MySQL执行计划参数详解 转http://www.jianshu.com/p/7134286b3a09 MySQL数据库中,在SELECT查询语句前边加上“EXPLAIN”或者“DESC”关键字,即 ...
随机推荐
- SQL基础分类
我们可以把学习过的sql语言,进行分类: 1. DDL : 数据定义语言 a) 操作库表结构的语言. Create drop alter 2. DML : 数据操作语言 a) 操作数据的语言: upd ...
- 图片大小的模式UIViewContentMode
- document.write()
以前一直以为document.write()就一定会清空文档里面的所有内容,一直没有去尝试,最近才知道原来是要在特定的情况下document.write才会清空文档里面所有内容的,在这里,觉得应该告诉 ...
- 『TCP/IP详解——卷一:协议』读书笔记——05
2013-08-19 22:35:57 2.6 PPP:点对点协议 PPP点对点协议修改了SLIP协议中的所有缺陷: 1. 在串联链路上封装IP数据报的方法.PPP即支持数据为8位&无奇偶检验 ...
- GET和POST有什么区别?
前几天有人问我这个问题.我说GET是用于获取数据的,POST,一般用于将数据发给服务器之用. 这个答案好像并不是他想要的.于是他继续追问有没有别的区别?我说这就是个名字而已,如果服务器支持,他完全可以 ...
- [转]15年双11手淘前端技术巡演 - H5性能最佳实践
[原文地址]:https://github.com/amfe/article/issues/21 前言 2015年是全面『无线化』的一年,在BAT(财报)几家公司都已经超过50%的流量来自移动端,这次 ...
- HttpLib - 一个对 Http 协议进行封装的库
今日,在 Codeplex 上看到一个开源项目,对 Http 协议进行了封装,这下可以方便这些在 .NET 平台下访问 Web 服务器的同学们了,比如,从 Web 服务器抓取一个页面,使用 .NET ...
- 通过Anuglar Material串串学客户端开发 - NodeJS模块机制之Module.Exports
module.exports 前文讲到在Angular Material的第二个编译文件docs/gulpfile.js中却看到了一个奇怪的东西module.exports那么module.expor ...
- [MFC] MFC 获取指定窗口截图(大小可调)
void screenShot(CRect rect,int left,int top,char *name){//截取窗口的大小,位置,名字(保存在默认路径下) CBitmap* m_pBitmap ...
- [C++] 几行代码生成漂亮图片,数学家就是牛!
信息获得处:http://news.cnblogs.com/n/501488/ 分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=alad ...