溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

計算mysql explain中key_len流程講析

發布時間:2020-04-29 16:56:34 來源:億速云 閱讀:272 作者:三月 欄目:MySQL數據庫

本文主要給大家介紹計算mysql explain中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 |
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+

改為定長字段后,key_len為60,與預測的一致。
總結:使用變長字段需要額外增加2個字節,使用NULL需要額外增加1個字節,因此對于是索引的字段,最好使用定長和NOT NULL定義,提高性能。

看完以上關于計算mysql explain中key_len流程講析,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業知識信息 ,可以持續關注我們的行業資訊欄目的。

 

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女