日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql explain中key_len值的说明

發(fā)布時(shí)間:2023/12/15 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql explain中key_len值的说明 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在mysql 的explain的輸出中,有個(gè)key_len的列,其數(shù)據(jù)是如何計(jì)算的呢?

在看到了淘寶的dba以前發(fā)布的博客后,我在mysql 5.6上操作一番,了解了一點(diǎn)。

環(huán)境準(zhǔn)備 – 創(chuàng)建表。

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');

我的環(huán)境如下:

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時(shí),datetime類型數(shù)據(jù)支持default設(shè)置為now(),結(jié)果也正常。

查看key_len值

char型的索引

mysql> explain select * from test_keylen where name1='name11'\G; *************************** 1. row *************************** id: 1select_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

這個(gè)查詢使用的index為2個(gè)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: 1select_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

這個(gè)索引使用的是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: 1select_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: 1select_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

從結(jié)果中看到key-len為6。

總結(jié)

以上是生活随笔為你收集整理的mysql explain中key_len值的说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。