标签:判断 文件 ble sql mysql buffer group by 条目 where
(1)、id 相同执行顺序由上到下
mysql> explain
-> SELECT*FROM tb_order tb1
-> LEFT JOIN tb_product tb2 ON tb1.tb_product_id = tb2.id
-> LEFT JOIN tb_user tb3 ON tb1.tb_user_id = tb3.id;
+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+
| 1 | SIMPLE | tb1 | ALL | NULL | NULL | NULL | NULL | 1 | NULL |
| 1 | SIMPLE | tb2 | eq_ref | PRIMARY | PRIMARY | 4 | product.tb1.tb_product_id | 1 | NULL |
| 1 | SIMPLE | tb3 | eq_ref | PRIMARY | PRIMARY | 4 | product.tb1.tb_user_id | 1 | NULL |
+----+-------------+-------+--------+---------------+---------+---------+---------------------------+------+-------+
(2)、如果是子查询,id序号会自增,id值越大优先级就越高,越先被执行。
mysql> EXPLAIN
-> select * from tb_product tb1 where tb1.id = (select tb_product_id from tb_order tb2 where id = tb2.id =1);
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | tb1 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
| 2 | SUBQUERY | tb2 | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
(3)、id 相同与不同,同时存在
mysql> EXPLAIN
-> select * from(select * from tb_order tb1 where tb1.id =1) s1,tb_user tb2 where s1.tb_user_id = tb2.id;
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL |
| 1 | PRIMARY | tb2 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
| 2 | DERIVED | tb1 | const | PRIMARY | PRIMARY | 4 | const | 1 | NULL |
+----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
derived2:衍生表 2表示衍生的是id=2的表 tb1
详解:这种类型表示mysql会根据特定的算法快速查找到某个符合条件的索引,而不是会对索引中每一个数据都进行一 一的扫描判断,也就是所谓你平常理解的使用索引查询会更快的取出数据。而要想实现这种查找,索引却是有要求的,要实现这种能快速查找的算法,索引就要满足特定的数据结构。简单说,也就是索引字段的数据必须是有序的,才能实现这种类型的查找,才能利用到索引。
1、 Using filesort(文件排序):mysql无法按照表内既定的索引顺序进行读取。
mysql> explain select order_number from tb_order order by order_money;
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | tb_order | ALL | NULL | NULL | NULL | NULL | 1 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
row in set (0.00 sec)
说明:order_number是表内的一个唯一索引列,但是order by 没有使用该索引列排序,所以mysql使用不得不另起一列进行排序。
2、Using temporary:Mysql使用了临时表保存中间结果,常见于排序order by 和分组查询 group by。
mysql> explain select order_number from tb_order group by order_money;
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | SIMPLE | tb_order | ALL | NULL | NULL | NULL | NULL | 1 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
row in set (0.00 sec)
3、Using index 表示相应的select 操作使用了覆盖索引,避免访问了表的数据行,效率不错。
如果同时出现Using where ,表明索引被用来执行索引键值的查找。
如果没有同时出现using where 表明索引用来读取数据而非执行查找动作。
mysql> explain select order_number from tb_order group by order_number;
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
| 1 | SIMPLE | tb_order | index | index_order_number | index_order_number | 99 | NULL | 1 | Using index |
+----+-------------+----------+-------+--------------------+--------------------+---------+------+------+-------------+
row in set (0.00 sec)
4、Using where: WHERE子句用于限制匹配哪些行针对下一个表或发送到客户端
5、Using join buffer (Block Nested Loop), Using join buffer (Batched Key Access) :表示当前sql使用了连接缓存。来自较早联接的表被部分读取到联接缓冲区中,然后使用它们的行从缓冲区中执行与当前表的联接。
6、impossible where :where 字句 总是false ,mysql 无法获取数据行。
7、select tables optimized away:
8、distinct:MySQL正在寻找不同的值,因此在找到第一个匹配的行后,它将停止为当前行组合搜索更多行。
9、Using where with pushed condition: NDB Cluster正在使用条件下推优化来提高在非索引列和常量之间进行直接比较的效率
10、Using sort_union(...),Using union(...),Using intersect(...): 这些指示了特定算法,该算法显示了如何针对index_merge联接类型合并索引扫描 。
11、Using MRR: 使用多范围读取优化策略读取表
12、Using index for group-by: 与Using index表访问方法类似,Using index for group-by 表示MySQL找到了一个索引,该索引可用于检索a GROUP BY或 DISTINCT查询的所有列,而无需对实际表进行任何额外的磁盘访问。此外,以最有效的方式使用索引,因此对于每个组,仅读取少数索引条目。
13、Using index condition: 通过访问索引元组并首先对其进行测试以确定是否读取完整的表行来读取表。这样,除非有必要,否则索引信息将用于延迟(“ 下推 ”)整个表行的读取。
更多extra参数参见:https://dev.mysql.com/doc/refman/5.6/en/explain-output.html#explain-extra-information
标签:判断 文件 ble sql mysql buffer group by 条目 where
原文地址:https://www.cnblogs.com/eternityz/p/12243335.html