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的写法达到选择合适索引的目的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章