在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. Microsoft Dynamics CRM 2011 JS操作集锦

    1.Xrm.Page.context 用户ID:getUserId() 用户角色:getUserRoles() 用户语言:getUserLcid() 组织名称:getOrgUniqueName() 组 ...

  2. Cocos2D的OALSimpleAudio预加载音频

    在OALSimpleAudio接口中,没有委托机制和通知机制告诉你什么时候预加载完成.也不需要这样的机制. 如下代码: //Listing 11-1. Preloading a background ...

  3. LIRe 源代码分析 7:算法类[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  4. 测试access函数

    测试程序: 测试结果: chown root access.out 将用户ID改为root chmod u+s access.out 打开 set-user-ID位

  5. AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框

    AngularJS+BootStrap实现弹出对话框 参考资料: http://angular-ui.github.io/bootstrap/#/modal https://www.zybuluo.c ...

  6. ZooKeeper leader election

    Paxos是分布式应用中解决同步问题的核心.作为应用研发工程师,我们总是倾向于使用一种相对简洁的方式实现复杂的算法.ZooKeeper leader election实现就是一个非常好的参考. 其实现 ...

  7. 安卓系统底层C语言算法之测试参数是几个long型的算法

    #include <stdio.h> #define BITS_PER_LONG (sizeof(unsigned long) * 8) //求一个数x是几个long的长度 #define ...

  8. vs2010修改状态栏的CStatusBar指针的的SetPaneText()方法时死活不对问题

    vs2010的mfc在有些地方不太一样不容易注意到,今天在修改状态栏的时候,就碰见了问题,死活修改不了. 参照下面的帖子: 点击打开链接 : 使用VS2010更改MFC程序的状态栏 2011-04-1 ...

  9. Digogo ugdx文件的制作

    The openplatform source code is in old IT FTP server at "vte/KCD/20150814/openplatform_wallace. ...

  10. leetCode之旅(12)-反转二叉树

    背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...