淺談mysql explain中key_len的計算方法
mysql的explain命令可以分析sql的性能,其中有一項是key_len(索引的長度)的統(tǒng)計。本文將分析mysql explain中key_len的計算方法。
1、創(chuàng)建測試表及數(shù)據(jù)
CREATE TABLE `member` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` tinyint(3) unsigned DEFAULT NULL, PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `member` (`id`, `name`, `age`) VALUES (NULL, 'fdipzone', '18'), (NULL, 'jim', '19'), (NULL, 'tom', '19');
2、查看explain
name的字段類型是varchar(20),字符編碼是utf8,一個字符占用3個字節(jié),那么key_len應(yīng)該是 20*3=60。
mysql> explain select * from `member` where name='fdipzone'; +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | 1 | SIMPLE | member | ref | name | name | 63 | const | 1 | Using index condition | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
explain的key_len為63,多出了3。
name字段是允許NULL,把name改為NOT NULL再測試
ALTER TABLE `member` CHANGE `name` `name` VARCHAR(20) NOT NULL; mysql> explain select * from `member` where name='fdipzone'; +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | 1 | SIMPLE | member | ref | name | name | 62 | const | 1 | Using index condition | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
現(xiàn)在key_len為62,比剛才少了1,但還是多了2??梢源_定,字段為NULL會多占用一個字節(jié)。
name字段類型為varchar,屬于變長字段,把varchar改為char再測試
ALTER TABLE `member` CHANGE `name` `name` CHAR(20) NOT NULL; mysql> explain select * from `member` where name='fdipzone'; +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+ | 1 | SIMPLE | member | ref | name | name | 60 | const | 1 | Using index condition | +----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
改為定長字段后,key_len為60,與預(yù)測的一致。
總結(jié):使用變長字段需要額外增加2個字節(jié),使用NULL需要額外增加1個字節(jié),因此對于是索引的字段,最好使用定長和NOT NULL定義,提高性能。
以上這篇淺談mysql explain中key_len的計算方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。