国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

淺談mysql explain中key_len的計算方法

2024-07-24 12:52:03
字體:
來源:轉載
供稿:網友

mysql的explain命令可以分析sql的性能,其中有一項是key_len(索引的長度)的統計。本文將分析mysql explain中key_len的計算方法。

1、創建測試表及數據

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個字節,那么key_len應該是 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 |+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+

現在key_len為62,比剛才少了1,但還是多了2。可以確定,字段為NULL會多占用一個字節。

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 |+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙山县| 杭州市| 伊春市| 鄯善县| 商丘市| 邯郸县| 潼南县| 南开区| 汕尾市| 麻栗坡县| 敖汉旗| 佛教| 南安市| 且末县| 历史| 彩票| 宜兰县| 荔波县| 新巴尔虎左旗| 天柱县| 广饶县| 怀来县| 海城市| 武宣县| 贺兰县| 荣成市| 崇阳县| 运城市| 阜平县| 嫩江县| 左权县| 曲松县| 琼海市| 霍城县| 牟定县| 惠东县| 麟游县| 汉沽区| 玛多县| 于田县| 惠东县|