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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM