在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. 图像处理程序框架—MFC相关知识点

    CDC:Windows使用与设备无关的图形设备环境(DC :Device Context) 进行显示 . MFC基础类库定义了设备环境对象类----CDC类. CDC与CGdiObject的关系 说道 ...

  2. 关于STM32的延时问题

    最近一直在搞一辆智能小车,用STM32单片机驱动,往上面加了很多外设,外型如下: 今天下午打算在LCD显示一个温度,却发现怎么都显示不了,也找不出原因,还好我们公司的郑工帮我看出了问题,让我顺利改过来 ...

  3. multiset基础学习,可以有重复类型的多重集合容器

    #include <set> #include <iostream> using namespace std; struct Student { char *name; int ...

  4. 关于electron的sqlite3报错,需重新编译的问题

    你需要安装sqlite3的所有依赖项,例如vs.python等.或者简单的npm安装命令,它会安装windows下的所有依赖. npm install -g windows-build-tools 然 ...

  5. 《转》Xcode 6 正式版如何创建一个Empty Application

    Xcode 6 正式版里面没有Empty Application这个模板,这对于习惯了纯代码编写UI界面的程序员来说很不习惯. 有网友给出了一个解决方法是,把Xcode 6 beta版里面的模板复制过 ...

  6. 到底创建了几个String对象?

    到底创建了几个String对象? 标签: 堆栈使用 对象创建 分类: 开发技术 关键字: java 面试题 string 创建几个对象 作者:臧圩人(zangweiren) 网址:http://zan ...

  7. win8 JDK环境变量不生效

    执行where java  看一下路径对不对,如果对的话就把system32下面的3个java相关的exe删了即可,如果路径不对就修改环境变量.

  8. Linux的chkconfig命令详解

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法: chkconfig [--a ...

  9. selenium获取百度账户cookies

    [效果图] 效果图最后即为获取到的cookies,百度账户的cookies首次获取,需要手动登录,之后就可以注入cookies,实现免密登录. [代码] public class baiduCooki ...

  10. Spring温故而知新 - bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...