在mysql 的explain的输出中,有个key_len的列,其数据是如何计算的呢?

在看到了淘宝的dba以前发布的博客后,我在mysql 5.6上操作一番,了解了一点。

环境准备 – 创建表。

use test;

drop table if exists test_keylen;

create table test_keylen (
id int not null,
name1 char(20),
name2 char(20),
create_time timestamp default current_timestamp,
update_time datetime default now(),
primary key (id),
key index_name (name1 , name2),
key index_createtime (create_time),
key index_updatetime (update_time)
) engine=innodb charset=gbk; insert into test_keylen(id,name1,name2) values(1,'name11','name12');
insert into test_keylen(id,name1,name2) values(2,'name21','name22');

我的环境如下:

mysql> show variables like "ver%";
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| version | 5.6.22 |
| version_comment | Homebrew |
| version_compile_machine | x86_64 |
| version_compile_os | osx10.9 |
+-------------------------+----------+
4 rows in set (0.00 sec)

可以看到sql定义table时,datetime类型数据支持default设置为now(),结果也正常。

查看key_len值

char型的索引

mysql> explain select * from test_keylen where name1='name11'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_keylen
type: ref
possible_keys: index_name
key: index_name
key_len: 41
ref: const
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)

这个查询使用的index为2个char(20), key_len 是20+20+1,其中的1是字符串尾部的’\0’;

int型的索引

mysql> explain select * from test_keylen where id=1 \G;  # key_len: 4 -- int
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_keylen
type: const
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: const
rows: 1
Extra: NULL
1 row in set (0.00 sec)

这个索引使用的是int类型的主键,key-len 为4,输出的type为const。

tiemstamp类型索引

mysql> explain select * from test_keylen where create_time>"2015-10-01" \G;  # key_len: 4 -- timestamp
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_keylen
type: range
possible_keys: index_createtime
key: index_createtime
key_len: 4
ref: NULL
rows: 2
Extra: Using index condition
1 row in set (0.00 sec)

这里的索引类型为timestamp,key-len为4 。而且可以看到输出的type为range。

索引类型为datetime

mysql> explain select * from test_keylen where update_time>"2015-10-01" \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test_keylen
type: range
possible_keys: index_updatetime
key: index_updatetime
key_len: 6
ref: NULL
rows: 2
Extra: Using index condition
1 row in set (0.00 sec)

从结果中看到key-len为6。

mysql explain中key_len值的说明的更多相关文章

  1. mysql explain中key_len的作用

    mysql explain中key_len的作用key_len越小 索引效果越好 name的字段类型是varchar(20),字符编码是utf8,一个字符占用3个字节,那么key_len应该是 20* ...

  2. mysql explain 中key_len的计算

    今天丁原问我mysql执行计划中的key_len是怎么计算得到的,当时还没有注意,在高性能的那本书讲到过这个值的计算,但是自己看执行计划的时候一直都没有太在意这个值,更不用说深讨这个值的计算了: ke ...

  3. mysql explain中key_len的计算

    ken_len表示索引使用的字节数,根据这个值,就可以判断索引使用情况,特别是在组合索引的时候,判断是否所有的索引字段都被查询用到. key_len显示了条件检索子句需要的索引长度,但 ORDER B ...

  4. Mysql explain中key_len的作用及计算规则

    key_len表示索引使用的字节数,根据这个值可以判断索引的使用情况,特别是在组合索引的时候,判断该索引有多少部分被使用到非常重要. 在计算key_len时,下面是一些需要考虑的点: 索引字段的附加信 ...

  5. mysql explain中的 “Select tables optimized away”

    mysql explain中的 “Select tables optimized away” http://blog.chinaunix.net/uid-10449864-id-2956845.htm ...

  6. 为什么rows这么大,在mysql explain中---写在去acumg听讲座的前一夜

    这周五下班前,发现了一个奇怪问题,大概是这个背景 一张表,结构为 Create Table: CREATE TABLE `out_table` ( `id` ) NOT NULL AUTO_INCRE ...

  7. mysql explain中的列

    参考:<高性能mysql>附录D EXPLAIN MySql将Select查询分为简单和复杂类型,复杂类型分为3大类:简单子查询,所谓的派生表(在派生表的子查询),以及UNION查询. 列 ...

  8. mysql explain中的type列含义和extra列的含义

    很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是“访问类型”,更通俗一点就是: ...

  9. [MySQL] explain中的using where和using index

    1. 查看表中的所有索引 show index from modify_passwd_log;  有两个 一个是id的主键索引 , 一个是email_id的普通索引 2. using index表示 ...

随机推荐

  1. VisualSVN Server安装过程

     运行VisualSVN-Server-2.7.3.msi程序, 如下图 点击Next, 下一步 选中 I accept选项, 点击Next, 下一步 选择默认配置, 服务和控制台组件方式, 点击 ...

  2. kettle文件自动化部署(shell脚本执行):命令行参数传入

    shell脚本中调用kitchen 和 pan去执行,job和transformation文件.分 windows和 dos系统两种. 举个简单的小例子 shell脚本: export JAVA_HO ...

  3. C#中任意类型数据转成JSON格式

    /// <summary>    /// List转成json     /// </summary>    /// <typeparam name="T&quo ...

  4. 将 MVVM 演化为 MVVMM

    众所周知,MVVM模式解决了Controller的臃肿并方便单元测试,为了方便后续代码维护,在上版本新功能开发中,项目开始使用MVVM模式进行开发. 但从上图可以看出,MVVM模式中,Controll ...

  5. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

  6. obj-c中SEL签名和Invocation示例

    参考小示例,代码如下: #import <Foundation/Foundation.h> @interface PlayList:NSObject @property NSMutable ...

  7. 一个div在另一个div中水平垂直的方法

    html <div id="main"> <div id="box"> 一个div在另一个div中垂直居中实现方法 </div&g ...

  8. centos 7下安装python 3.6笔记

    每次在centos上安装python 3都需要重新查资料,这次索性自己记下笔记. 首先安装gcc yum -y install gccyum install zlib-devel./configure ...

  9. Centos 7 卸载自带的openjdk

    [root@localhost ~]# rpm -qa|grep jdk java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5.el6_3.x86_64 java-1.7.0- ...

  10. java核心卷轴之集合

    1. Iterator 1.1 注意事项 接口的remove方法将删除上次调用next方式时返回的对象,即:remove之前,必须有next(先获取,再删除). 1.2 例一:删除字符串集合中的第一个 ...