通过合理使用JOIN,可以减少”Using where; Using temporary; Using filesort”的出现。 例如,考虑以下查询: SELECTcustomers.name,orders.order_dateFROMcustomers,ordersWHEREcustomers.id=orders.customer_idANDcustomers.age>30; SQL Copy 可以使用JOIN进行优化: SELECTcustomers.name,orders.order_dat...
Using Filesort 和 Using Temporary:说明没有使用到索引。 impossible where:说明条件永远不成立。 use index:表示相应的select中使用了覆盖索引,避免访问了表的数据行, 效率很好 using where:表明使用了where过滤。 using join buffer:使用了连接缓存。 3. 分析常见问题和优化建议 在分析EXPLAIN输出时,可以根...
上面那个例子中其实就用到了:Using index,因为只返回一列code,它字段走了索引。 Using temporary表示是否使用了临时表,一般多见于order by 和 group by语句。执行sql如下: explain select name from test1 group by name; 结果: 图片 Using where表示使用了where条件过滤。 Using join buffer 表示是否使用连接缓冲。
SELECT* FROM(SELECTtarget, Count(*) FROMoperation GROUPBYtarget)t WHEREtarget='rm-xxxx'+---+---+---+---+---+---+---+---+---+---+ |id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra| +---+---+---+---+---+---+---+---...
Usingwhere;Usingtemporary||2|DEPENDENT SUBQUERY|||ImpossibleWHEREnoticed after reading const tables||3|DERIVED|o|ref|idx_2,idx_5|idx_5|8|const|1|Usingwhere;Usingfilesort|+---+---+---+---+---+---+---+---+---+---+ 重写为 JOIN 之后,子查询的选择模式从 DEPENDENT SUBQUERY 变成 ...
1.Using filesort 主要出现在 order by 排序、复合索引跨列; order by 排序 出现原因:查询a表,却根据b表排序,例如: 代码语言:javascript 代码运行次数:0 复制 Cloud Studio代码运行 select*From test01 where a='3'order by b; 如果避免此情况出现,就根据什么字段查,就根据什么字段进行排序。如: ...
Using index 使用覆盖索引 Using where 使用了用where子句来过滤结果集 Using filesort 使用文件排序,使用非索引列进行排序时出现,非常消耗性能,尽量优化。 Using temporary 使用了临时表。 一些SQL优化建议 1、SQL语句不要写的太复杂。 一个SQL语句要尽量简单,不要嵌套太多层。
| 1 | PRIMARY | o | index | | PRIMARY | 8 | | 24 | Using where; Using temporary | | 2 | DEPENDENT SUBQUERY | | | | | | | | Impossible WHERE noticed after reading const tables | | 3 | DERIVED | o | ref | idx_2,idx_5 | idx_5 | 8 | const | 1 | Using where; Usi...
|1|PRIMARY|o|index||PRIMARY|8||24|Using where; Using temporary| |2|DEPENDENT SUBQUERY|||Impossible WHERE noticed after reading const tables| |3|DERIVED|o|ref|idx_2,idx_5|idx_5|8|const|1|Using where; Using filesort| +---+---+---+---+---+---+---+---+---+---+ 重写为...
其实临时表在之前的博客就已经出现过了,在 MySQL 中的排序 一文中就说到如果 order by 的列上没有索引,或者说没有用到索引,那么就需要进行额外排序(using filesort),而额外排序优先在一块 sort_buffer 空间中进行,如果这块空间大小小于要加载的字段总长度,那么就会用到临时文件辅助排序,这个临时文件就是临时表。