Extra分析:包含不在其他属性显示,但是又非常重要的信息
1.Using FileSort:说明MySQL会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取,即MySQL无法使用索引完成的排序称为"文件排序"
2.Using temporary:使用了临时表来保存中间结果,MYSQL在对查询结果进行排序的时候使用了临时表,常见于排序OrderBy 和分组查询GroupBy
Using FileSort只是不能按照索引方法进行排序,但是Using temporary会创建一张临时表,将缓存数据存放在临时表中,然后再删除临时表,操作变得更凶险了
3.Using Index:
-
表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错
-
如果同时出现Using Where,表明索引被用来执行索引键值的查找
-
如果没有同时出现Using Where,表明索引用来读取数据而非执行查找工作
4.覆盖索引:
- select 查询的数据列只用ongoing索引中就能够取得,不必读取数据行,MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件,换句话说:索引列要被所建的索引覆盖
如果想使用覆盖索引,在select的时候不能使用select * from t,具体写出对应的字段
5.Using Where:使用where过滤条件
6.Using Join Buffer:使用了连接缓存(join太多个表,配置文件里面的JoinBuffer的值可以调大一点)
7.Impossible Where:where子句的值总是false,不能获取任何元组
8.Select tables optimized away:在没有GroupBy子句的情况下,基于索引优化Min/Max操作或者对于MyISAM存储引擎优化Count(*)操作,不必等到执行阶段再进行计算,查询执行计划生成的阶段即完成优化
9.distinct:优化distinct操作,在找到第一个匹配的元组后即停止找同样值的动作
1.主键自动创建唯一索引
2.频繁作为查询的条件的字段应该创建索引
3.查询中与其他表关联的字段,外键关系建立索引
4.频繁更新的字段不应该创建索引,因为每次更新不单单是更新了记录,还会更新索引,非常耗时
5.where条件中用不到的字段不适合创建索引
6.单键、组合索引的选择问题,(在高并发下倾向于创建组合索引)
7.查询中排序的字段,排序字段若通过索引去访问将大大的提高排序速度
8.查选中统计或分组的字段
索引和排序是有关系的:建完索引之后,排序也要按照索引建立的顺序进行排序,要不然会造成索引失效现象
1.1(A)没有创建关联索引
mysql> explain SELECT * from t4 ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| 1 | SIMPLE | t4 | index | NULL | PRIMARY | 4 | NULL | 8 | |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
1.1(B)关联索引
mysql> explain SELECT * from t4 ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
| 1 | SIMPLE | t4 | index | NULL | PRIMARY | 4 | NULL | 8 | |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| 1 | SIMPLE | t4 | index | NULL | index_id_name_age | 42 | NULL | 8 | Using index |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
| 1 | SIMPLE | t4 | index | NULL | index_id_name_age | 42 | NULL | 8 | Using index |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-------------+
1 row in set (0.00 sec)
♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟
1.2(A)没有创建关联索引
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 8 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
1.2(B)创建关联索引
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| 1 | SIMPLE | t4 | index | NULL | index_id_name_age | 42 | NULL | 8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| 1 | SIMPLE | t4 | index | NULL | index_id_name_age | 42 | NULL | 8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
| 1 | SIMPLE | t4 | index | NULL | index_id_name_age | 42 | NULL | 8 | Using index; Using filesort |
+----+-------------+-------+-------+---------------+-------------------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)
♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟
1.3(A)没有创建关联索引
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
1.3(B)创建关联索引
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id,t4.`name`;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.id,t4.`name`,t4.age;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟
1.4(A)没有创建关联索引
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
1.4(B)创建关联索引
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.id,t4.age;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
mysql> explain SELECT * from t4 where t4.id = '1' ORDER BY t4.`name`,t4.age,t4.id;
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | t4 | const | PRIMARY,index_id_name_age | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------------------+---------+---------+-------+------+-------+
1 row in set (0.00 sec)
2.创建索引代码
CREATE INDEX index_id_name_age ON t4 (id,`name`,age)
♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜ ♟ ♔ ♕ ♗ ♘ ♖ ♟