mysql explain 竖着显示_虽然INDEX存在,但MySQL EXPLAIN显示所有类型

bd96500e110b49cbb3cd949968f18be7.png

I ran the below query using EXPLAIN

EXPLAIN SELECT form.id AS `Reference No.`,

department.name AS `Department`,

section.name AS `Section`

FROM form

JOIN department

ON form.deptid = department.id

JOIN section

ON form.sectid = section.id

Does type ALL in the first row indicate that there is going to be performance issues?

+----+-------------+------------+--------+---------------+---------+---------+----------------------+------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows |

+----+-------------+------------+--------+---------------+---------+---------+----------------------+------+

| 1 | SIMPLE | form | ALL | deptid,sectid | | | | 779 |

| 1 | SIMPLE | department | eq_ref | PRIMARY | PRIMARY | 4 | wfs-test.form.deptid | 1 |

| 1 | SIMPLE | section | eq_ref | PRIMARY | PRIMARY | 4 | wfs-test.form.sectid | 1 |

+----+-------------+------------+--------+---------------+---------+---------+----------------------+------+

解决方案

There is no reason for MySQL to use the index when it gets data from table form. Because the query doesn't have any WHERE clause, potentially all the rows from table from will be included in the final result set. More, because there is no ORDER BY clause, any order of the rows is good enough. This is why MySQL gets the rows directly from the table, without consulting any index.

Adding a WHERE condition could trigger the use of an index if an index that contains (some of) the fields involved in the WHERE conditions exist (and the fields, put in the correct order, are the leftmost columns included in the index).

Adding an ORDER BY clause (without WHERE) on fields from table form could also trigger the use of an index when all the fields selected from table form are contained in the index. It will change the type from ALL to index which means it will do a full scan of the index instead of the data rows to get the data it needs. While this is still a full scan, a full index scan usually runs faster than a full table scan because less data is loaded from the storage and parsed (the index is usually smaller than the table data).

More information about this can be found in the MySQL documentation.

The entire section "8.2.1 Optimizing SELECT Statements" is worth reading to better understand how to write faster queries.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值