對(duì)于mysql explain中key_len的計(jì)算方法講解
發(fā)表時(shí)間:2023-07-11 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]mysql的explain命令可以分析sql的性能,其中有一項(xiàng)是key_len(索引的長(zhǎng)度)的統(tǒng)計(jì)。本文將分析mysql explain中key_len的計(jì)算方法。 1.創(chuàng)建測(cè)試表及數(shù)據(jù)CREATE...
mysql的explain命令可以分析sql的性能,其中有一項(xiàng)是key_len(索引的長(zhǎng)度)的統(tǒng)計(jì)。本文將分析mysql explain中key_len的計(jì)算方法。
1.創(chuàng)建測(cè)試表及數(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,一個(gè)字符占用3個(gè)字節(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再測(cè)試
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會(huì)多占用一個(gè)字節(jié)。
name字段類型為varchar,屬于變長(zhǎng)字段,把varchar改為char再測(cè)試
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
+----+-------------+--------+------+---------------+------+---------+-------+------+-----------------------+
改為定長(zhǎng)字段后,key_len為60,與預(yù)測(cè)的一致。
總結(jié):使用變長(zhǎng)字段需要額外增加2個(gè)字節(jié),使用NULL需要額外增加1個(gè)字節(jié),因此對(duì)于是索引的字段,最好使用定長(zhǎng)和NOT NULL定義,提高性能。
本篇文章講解了mysql explain中key_len的計(jì)算方法,更多相關(guān)內(nèi)容請(qǐng)關(guān)注php中文網(wǎng)。
相關(guān)推薦:
如何通過php 使用curl模擬ip和來源進(jìn)行訪問
通過mysql 轉(zhuǎn)換NULL數(shù)據(jù)方法
關(guān)于php 函數(shù)使用可變數(shù)量的參數(shù)的相關(guān)內(nèi)容
以上就是關(guān)于mysql explain中key_len的計(jì)算方法講解的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。