Sobes.tech
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:

  1. Add or improve indexes — to avoid full table scans, create indexes on columns involved in WHERE, JOIN, and ORDER BY conditions.
  2. Rewrite the query — possibly change the query logic to reduce the amount of data processed.
  3. Avoid complex operations that cause temporary tables — for example, split the query into several parts or use aggregate functions with indexes.
  4. 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.

Task 5: EXPLAIN shows type: ALL, rows: 200000, Using… - sobes.tech