mysql explain中key_len值的说明
在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值的说明的更多相关文章
- mysql explain中key_len的作用
mysql explain中key_len的作用key_len越小 索引效果越好 name的字段类型是varchar(20),字符编码是utf8,一个字符占用3个字节,那么key_len应该是 20* ...
- mysql explain 中key_len的计算
今天丁原问我mysql执行计划中的key_len是怎么计算得到的,当时还没有注意,在高性能的那本书讲到过这个值的计算,但是自己看执行计划的时候一直都没有太在意这个值,更不用说深讨这个值的计算了: ke ...
- mysql explain中key_len的计算
ken_len表示索引使用的字节数,根据这个值,就可以判断索引使用情况,特别是在组合索引的时候,判断是否所有的索引字段都被查询用到. key_len显示了条件检索子句需要的索引长度,但 ORDER B ...
- Mysql explain中key_len的作用及计算规则
key_len表示索引使用的字节数,根据这个值可以判断索引的使用情况,特别是在组合索引的时候,判断该索引有多少部分被使用到非常重要. 在计算key_len时,下面是一些需要考虑的点: 索引字段的附加信 ...
- mysql explain中的 “Select tables optimized away”
mysql explain中的 “Select tables optimized away” http://blog.chinaunix.net/uid-10449864-id-2956845.htm ...
- 为什么rows这么大,在mysql explain中---写在去acumg听讲座的前一夜
这周五下班前,发现了一个奇怪问题,大概是这个背景 一张表,结构为 Create Table: CREATE TABLE `out_table` ( `id` ) NOT NULL AUTO_INCRE ...
- mysql explain中的列
参考:<高性能mysql>附录D EXPLAIN MySql将Select查询分为简单和复杂类型,复杂类型分为3大类:简单子查询,所谓的派生表(在派生表的子查询),以及UNION查询. 列 ...
- mysql explain中的type列含义和extra列的含义
很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是“访问类型”,更通俗一点就是: ...
- [MySQL] explain中的using where和using index
1. 查看表中的所有索引 show index from modify_passwd_log; 有两个 一个是id的主键索引 , 一个是email_id的普通索引 2. using index表示 ...
随机推荐
- 菜鸟玩云计算之廿二: saltstack 配置
菜鸟玩云计算之廿二: saltstack 配置 要求环境: RHEL6.4+ >=Python2.6.6, < Python 3.0 关闭salt-master/minion服务: # ...
- Java 继承Thread类和实现Runnable接口的区别
ava中线程的创建有两种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 在实际应用中,我 ...
- cocoa编程第4版 8.5 挑战1 解答
看似简单,其实也很简单,但开始思路想错了:还上网查了一下,有网友说是将Array Controller的Keys中的personName改为personName.length,好像完全不起作用. 后来 ...
- MIDle生命周期详解,以及工作原理
当MIDlet被应用程序管理器成功地初始化之后,就开始展开了它的生命周期.MIDlet的生命周期完全由应用程序管理器控制,也就是说,当MIDlet要从一个状态变成另外一个状态时,应用程序管理器会调用对 ...
- HashMap是无序的
一. 说明 HashMap是基于哈希表Map的实现.设计初衷主要是为了解决键值(key-value)对应关联的,HashMap的优势是可以很快的根据键(key)找到该键对应的值(value),但是我们 ...
- using MR to compute PageRank
In this post I explain how to compute PageRank using the MapReduce approach to parallelization. This ...
- C标准中关于空指针的那些事
1 C标准不保证用所有二进制位都为0的变量来表示空指针,但它保证空指针与任何对象或函数的指针都不相等,取地址操作符&永远也不会返回空指针: 2 C标准称在指针上下文中的"值为0的整形 ...
- mybatis中分页插件PageHelper的使用
转载博客:http://blog.csdn.net/u012728960/article/details/50791343
- 查找链表中是否有环linked-list-cycle
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 使用javascript中读取Xml文件做成的一个二级联动菜单
[html] view plaincopy <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...