key_len的含義
在MySQL中,可以通過explain查看SQL語句所走的路徑,如下所示:
mysql> create table t(a int primary key, b int not null, c int not null, index(b)); Query OK, 0 rows affected (0.01 sec) mysql> explain select b from t ; +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | t | index | NULL | b | 4 | NULL | 1 | Using index | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec)
其中,key_len表示使用的索引長度,是以字節(jié)為單位。在上面的例子中,由于int型占用4個字節(jié),而索引中只包含了1列,所以,key_len是4。
下面是聯(lián)合索引的情況:
mysql> alter table t add index ix(b, c); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> explain select b, c from t ; +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | t | index | NULL | ix | 8 | NULL | 1 | Using index | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec)
聯(lián)合索引ix包含了2列,并且都使用到了,所以,這里ken_len是8。
到這里,我們已經(jīng)可以理解key_len的含義了,似乎已經(jīng)沒有什么可講的了,但是,MySQL中key_len的計算還有很多需要注意的地方。
例如,我們將b這一列的NOT NULL約束去掉,然后ken_len就和我們預(yù)期不一樣了,如下所示:
mysql> alter table t modify b int; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> explain select b from t; +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | t | index | NULL | b | 5 | NULL | 1 | Using index | +----+-------------+-------+-------+---------------+------+---------+------+------+-------------+ 1 row in set (0.00 sec)
MySQL中key_len計算規(guī)則
MySQL中,key_len的計算規(guī)則如下:
根據(jù)官方文檔可以知道,decimal定義為decimal(M,D),其中,M是總的位數(shù),D是小數(shù)點后保留的位數(shù)。小數(shù)點前與小數(shù)點后的數(shù)字分開存儲,且以9位數(shù)為1組,用4個字節(jié)保存,如果低于9位數(shù),需要的字節(jié)數(shù)如下:
Leftover Digits Number of Bytes
-----------------------------
|0 |0 |
|1-2 |1 |
|3-4 |2 |
|5-6 |3 |
|7-9 |4 |
-----------------------------
例如:
decimal(20,6)=> 小數(shù)點左邊14位,小數(shù)點右邊6位 => 小數(shù)點左邊分組為5 + 9,需要3個字節(jié)+4個字節(jié)存儲,小數(shù)點一個分組,需要3個字節(jié)存儲 => 總共需要10個字節(jié)
decimal(18,9)=> 小數(shù)點左邊9位數(shù),小數(shù)點右邊9位數(shù) => 分別使用4個字節(jié)存儲 => 共需要 8個字節(jié)
decimal(18,2)=> 小數(shù)點左邊16位數(shù),小數(shù)點右邊2位數(shù) => 分組為7 + 9,需要8個字節(jié)存儲,小數(shù)點右邊1個字節(jié)存儲 => 共需要9個字節(jié)
通過key_len分析聯(lián)合索引
如下所示,我們定義了一個表t,表t包含a、b、c、d共4列:
mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `a` int(11) NOT NULL, `b` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL, `d` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `ix_x` (`b`,`d`,`c`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)
現(xiàn)在要執(zhí)行SQL語句如下:
select a from t where b = 5 and d = 10 order by c;
假設(shè)我們有一個索引ix_x(b,d,c),通過explain得到如下輸出:
mysql> explain select a from t where b = 5 and d = 10 order by c; +----+-------------+-------+------+---------------+------+---------+-------------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------------+------+--------------------------+ | 1 | SIMPLE | t | ref | ix_x | ix_x | 10 | const,const | 1 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+-------------+------+--------------------------+ 1 row in set (0.00 sec)
可以看到,查詢語句使用了聯(lián)合索引中的b和d兩列來過濾數(shù)據(jù)。
如果我們定義的聯(lián)合索引不是`ix_x(b,d,c)`,而是`ix_x(b, c, d)`,通過explain得到的輸入如下:
mysql> alter table t drop index ix_x; mysql> alter table t add index ix_x(b, c, d); mysql> explain select a from t where b = 5 and d = 10 order by c; +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+ | 1 | SIMPLE | t | ref | ix_x | ix_x | 5 | const | 2 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+-------+------+--------------------------+ 1 row in set (0.00 sec)
key_len為5,也就是說,只用到了聯(lián)合索引中的第一列,可以看到,雖然聯(lián)合索引包含了我們要查詢的所有列,但是,由于定義的順序問題,SQL語句并不能夠充分利用索引。
標(biāo)簽:內(nèi)蒙古 成都 湖州 重慶 常德 貴州 臨沂 天津
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL的查詢計劃中ken_len的值計算方法》,本文關(guān)鍵詞 MySQL,的,查詢,計劃,中,ken,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。