在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. Android的fuzz测试技术之符号执行浅谈-android学习之旅(82)

    简单的漏洞越来越少,需要改进目前的方法 : 通过符号执行,得出执行路径,然后在进行fuzzy是较为有效的方法之一 1)为待测单元自动地生成可到达的测试数据,即提高测试目标的覆盖率 2)根据特定的漏洞模 ...

  2. 基于RTMP的实时流媒体的QoE分析

    Holly French等人在论文<Real Time Video QoE Analysis of RTMP Streams>中,研究了基于RTMP的实时视频的QoE.在此记录一下. 他们 ...

  3. 《java入门第一季》之面向对象(this和super详细分析)

    此文章来自于书籍,里面介绍了this和super详细的区别.当然在后边的文章中还有涉及super的时候还会分析. Java关键字this.super使用总结 一.this Java关键字this只能用 ...

  4. Xcode出现may cause a leak的解决

    比如如下代码: -(void)performSelector:(SEL)selector onNode:(CCNode *)node withObject:(id)object recursive:( ...

  5. linux 网络不通问题排查

    基本的排错步骤(从上往下)ping 127.0.0.1ping的通说明tcp协议栈没有问题ping 主机地址 ping的通说明网卡没有问题ping 路由器默认网关 ping的通说明包可以到达路由器最后 ...

  6. 写一个python的服务监控程序

    写一个python的服务监控程序 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上, ...

  7. 色彩转换——RGB & HSV

    RGB to HSV The R,G,B values are divided by 255 to change the range from 0..255 to 0..1: R' = R/255 G ...

  8. linux下gtk+一个将字符串大写化的小示例

    首先用glade画图形界面: 并且设置gtk元素名称(ID)以及设置事件回调函数. 下面写代码: #include <gtk/gtk.h> #include <string.h> ...

  9. Herriot

    Herriot测试框架是Hadoop-0.21.0及以后版本中新加入的测试框架,它的出现主要是为了尽可能地模拟真实的大规模分布式系统,并且对该系统实现自动化测试.和Hadoop以前的测试框架MiniD ...

  10. Mina源码阅读笔记(四)—Mina的连接IoConnector2

    接着Mina源码阅读笔记(四)-Mina的连接IoConnector1,,我们继续: AbstractIoAcceptor: 001 package org.apache.mina.core.rewr ...