mysql explain中key_len值的说明
2015-12-09 14:10
706 查看
在mysql 的explain的输出中,有个key_len的列,其数据是如何计算的呢?
在看到了淘宝的dba以前发布的博客后,我在mysql 5.6上操作一番,了解了一点。
我的环境如下:
可以看到sql定义table时,datetime类型数据支持default设置为now(),结果也正常。
这个查询使用的index为2个char(20), key_len 是20+20+1,其中的1是字符串尾部的’\0’;
这个索引使用的是int类型的主键,key-len 为4,输出的type为const。
这里的索引类型为timestamp,key-len为4 。而且可以看到输出的type为range。
从结果中看到key-len为6。
在看到了淘宝的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) ERROR: No query specified
这个查询使用的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) ERROR: No query specified
这个索引使用的是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) ERROR: No query specified
这里的索引类型为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) ERROR: No query specified
从结果中看到key-len为6。
相关文章推荐
- csv 导入导出mysql
- mysql基础(五) Mysql删除表时,报"Connot delete or update a parent row:a foreign key constraint fails"解决方案
- Mysql中普通的索引、主键、唯一、全文索引的区别
- 【MySQL】1093 You can't specify target table 'table name' for update in FROM clause
- 转载 mysql 视图
- mysql字符编码问题latin1到utf8
- MySQL中临时表的基本创建与使用教程
- Mac上安装mysql遇到的坑
- mysql myisam引擎压缩表
- MySQL中describe命令的使用方法小结
- Mysql登录失败错误(10061)解决办法
- MySQL触发器运用于迁移和同步数据的实例教程
- Mysql查询优化——连接的技巧(一)
- mysql的sql经典写法收录
- [MySQL FAQ]系列 — 如何查看当前最新事务ID
- mysql 构建大表及myisam 基础知识
- MySQL中触发器的基础学习教程
- MySQL锁定状态查看命令
- MySQL中exists和join的区别
- MySQL性能优化之swap占用高