Senior
Task 5: EXPLAIN shows type: ALL, rows: 200000, Using temporary; Using filesort. What does it mean and how to optimize?
sobes.tech AI
Answer from AI
In the EXPLAIN output:
- type: ALL means a full table scan, which is usually slow with large data volumes.
- rows: 200000 — the number of rows MySQL plans to read.
- Using temporary — a temporary table is used for query processing, for example, during sorting or grouping.
- Using filesort — sorting is performed not by index, but with an additional sorting operation.
How to optimize:
- Add or improve indexes — to avoid full table scans, create indexes on columns involved in WHERE, JOIN, and ORDER BY conditions.
- Rewrite the query — possibly change the query logic to reduce the amount of data processed.
- Avoid complex operations that cause temporary tables — for example, split the query into several parts or use aggregate functions with indexes.
- Check statistics and update indexes — so the optimizer has current information.
Example: if the query sorts by a column without an index, add an index:
ALTER TABLE table_name ADD INDEX idx_column_name(column_name);
This can significantly speed up execution and remove Using filesort.