| 類型 | 所占空間 | 不允許為NULL額外占用 |
|---|---|---|
| char | 一個(gè)字符三個(gè)字節(jié) | 一個(gè)字節(jié) |
| varchar | 一個(gè)字符三個(gè)字節(jié) | 一個(gè)字節(jié) |
| int | 四個(gè)字節(jié) | 一個(gè)字節(jié) |
| tinyint | 一個(gè)字節(jié) | 一個(gè)字節(jié) |
測(cè)試數(shù)據(jù)表如下:
CREATE TABLE `test_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `a` int(11) DEFAULT NOT NULL, `b` int(11) DEFAULT NOT NULL, `c` int(11) DEFAULT NOT NULL, PRIMARY KEY (`id`), KEY `test_table_a_b_c_index` (`a`,`b`,`c`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
命中索引:
mysql> explain select * from test_table where a = 1 and b = 2 and c = 3; +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+ | 1 | SIMPLE | test_table | NULL | ref | test_table_a_b_c_index | test_table_a_b_c_index | 12 | const,const,const | 1 | 100.00 | Using index | +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+
可以看到 key_len = 12,這是如何計(jì)算的呢?
因?yàn)樽址?UTF8,一個(gè)字段占用四個(gè)字節(jié),三個(gè)字段就是 4 * 3 = 12 字節(jié)。
是否允許為 NULL,如果允許為 NULL,則需要用額外的字節(jié)來(lái)標(biāo)記該字段,不同的數(shù)據(jù)類型所需的字節(jié)大小不同。
mysql> ALTER TABLE `test_table` CHANGE `a` `a` INT(11) NULL; mysql> ALTER TABLE `test_table` CHANGE `c` `c` INT(11) NULL; mysql> ALTER TABLE `test_table` CHANGE `b` `b` INT(11) NULL; mysql> explain select * from test_table where a = 1 and b = 2 and c = 3; +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+ | 1 | SIMPLE | test_table | NULL | ref | test_table_a_b_c_index | test_table_a_b_c_index | 15 | const,const,const | 1 | 100.00 | Using index | +----+-------------+------------+------------+------+------------------------+------------------------+---------+-------------------+------+----------+-------------+
可以看到,當(dāng)字段允許為空時(shí),這時(shí)的key_len 變成了15 = 4 3 + 1 3(INT 類型為空時(shí),額外占用一個(gè)字節(jié))。
有了這些基礎(chǔ)知識(shí)之后,再來(lái)根據(jù)實(shí)際的SQL 判斷索性性能好壞。
還是以上面那張數(shù)據(jù)表為例,為 a、b、c 三個(gè)字段創(chuàng)建聯(lián)合索引。
| SQL 語(yǔ)句 | 是否索引 |
|---|---|
| explain select * from test_table where a = 1 and b = 2 and c = 3; | Extra:Using index key_len: 15 |
| explain select * from test_table where a = 1 and b = 2 and c = 3 order by c; | Extra:Using index key_len: 15 |
| explain select * from test_table where b = 2 and c = 3; | Extra:Using where; Using index key_len: 15 |
| explain select * from test_table where a = 1 order by c; | Extra:Using where; Using index; Using filesort key_len: 5 |
| explain select * from test_table order by a, b, c; | Extra:Using index key_len: 15 |
| explain select * from test_table order by a, b, c desc; | Extra:Using index; Using filesort key_len:15 |
| explain select * from test_table where a in (1,2) and b in (1,2,3) and c = 1; | Extra:Using where; Using index key_len: 15 |
通常在查看執(zhí)行計(jì)劃時(shí), Extra 列為 Using index 則表示優(yōu)化器使用了覆蓋索引。
以上就是Mysql 索引該如何設(shè)計(jì)與優(yōu)化的詳細(xì)內(nèi)容,更多關(guān)于MySQL 索引設(shè)計(jì)與優(yōu)化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:資陽(yáng) 荊州 錦州 吉林 隨州 滄州 甘南 黑河
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Mysql 索引該如何設(shè)計(jì)與優(yōu)化》,本文關(guān)鍵詞 Mysql,索引,該,如何,設(shè)計(jì),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。