7_索引面試題分析

注:App端的代碼塊區可能會因爲內容過多而顯示不全,請雙擊代碼塊區查看內容!

create table test03(
	id int primary key not null auto_increment,
	c1 char(10),
	c2 char(10),
	c3 char(10),
	c4 char(10),
	c5 char(10)
);
insert into test03(c1,c2,c3,c4,c5) values('a1','a2','a3','a4','a5');
insert into test03(c1,c2,c3,c4,c5) values('b1','b2','b3','b4','b5');
insert into test03(c1,c2,c3,c4,c5) values('c1','c2','c3','c4','c5');
insert into test03(c1,c2,c3,c4,c5) values('d1','d2','d3','d4','d5');
insert into test03(c1,c2,c3,c4,c5) values('e1','e2','e3','e4','e5');
select * from test03;

-- 創建索引
create index ind_test03_c1234 on test03(c1,c2,c3,c4);
show index from test03;
mysql> explain select * from test03 where c1='a1' and c5='a5'order by c3,c2;
+----+-------------+--------+------------+------+------------------+------------------+---------+-------+------+----------+----------------------------------------------------+
| id | select_type | table  | partitions | type | possible_keys    | key              | key_len | ref   | rows | filtered | Extra                                              |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------+------+----------+----------------------------------------------------+
|  1 | SIMPLE      | test03 | NULL       | ref  | ind_test03_c1234 | ind_test03_c1234 | 11      | const |    1 |    20.00 | Using index condition; Using where; Using filesort |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------+------+----------+----------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test03 where c1='a1' and c2='a2' and c5='a5'order by c3,c2;
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
| id | select_type | table  | partitions | type | possible_keys    | key              | key_len | ref         | rows | filtered | Extra                              |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
|  1 | SIMPLE      | test03 | NULL       | ref  | ind_test03_c1234 | ind_test03_c1234 | 22      | const,const |    1 |    20.00 | Using index condition; Using where |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)
-- 本例有常量c2,故order by之後的c2不排序


mysql> explain select * from test03 where c1='a1' and c2='a2' and c5='a5'group by c3,c2;
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
| id | select_type | table  | partitions | type | possible_keys    | key              | key_len | ref         | rows | filtered | Extra                              |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
|  1 | SIMPLE      | test03 | NULL       | ref  | ind_test03_c1234 | ind_test03_c1234 | 22      | const,const |    1 |    20.00 | Using index condition; Using where |
+----+-------------+--------+------------+------+------------------+------------------+---------+-------------+------+----------+------------------------------------+
1 row in set, 1 warning (0.00 sec)
-- 分組之前比排序
-- group by和order by的索引優化法則幾乎是一致的
-- group by基本上都需要進行排序,會有臨時表產生

-- 一般性建議
-- 1.對於單鍵索引,儘量選擇針對當前query過濾性更好的索引
-- 2.在選擇組合索引時,當前query中過濾性最好的字段在索引字順序中,位置越靠左越好
-- 3.在選擇組合索引時,儘量選擇可以能夠包含當前query中的where子句中更多字段的索引
-- 4.儘可能通過分析統計信息和調整query的寫法達到選擇合適索引的目的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章