What are the usage scenarios of using filesort and using temporary?

The

question originates from this article: MySQL answering questions using filesort VS using temporary

in the article:

mysql> explain select * from t1 force index(id), t2 where t1.id=1 and t1.col1 = t2.col2 order by t2.col1 ;
+----+-------------+-------+------+---------------+------+---------+-------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref   | rows | Extra                                        |
+----+-------------+-------+------+---------------+------+---------+-------+------+----------------------------------------------+
|  1 | SIMPLE      | t1    | ref  | id            | id   | 5       | const |    1 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | t2    | ALL  | NULL          | NULL | NULL    | NULL  |    1 | Using where; Using join buffer               |
+----+-------------+-------+------+---------------+------+---------+-------+------+----------------------------------------------+

Article conclusion:

The

case2: order by predicate is done on the first table T1, so only needs to use filesort on the T1 table, and then sort the result set join T2 table.
case 3: the field of order by is on the T2 table, so you need to save the result of the T1 strong T2 table join to the temporary table , then filesort the temporary table, and finally output the result.

my question:

Why does case2 case3 need to do join, and why does case3 need to save to the temporary table, but case2 doesn"t?
ask the boss to solve the problem.


for case2, instead of using join, you first filter out all the records of t1.id=1, and then sort them. Finally, you take the col1 of these records, go to T2 to check the records of t1.col1 = t2.col2, return the id of the satisfied records, and finally return
for case3, because you have to use the col1 of T2 for sorting, and you are using the index of T1, and you have no information about T2. You can only use temporary tables to save t2.col1. In order to sort
another sentence, for case3, if you do not force the index of t1.id, then according to the actual amount of data, the optimizer may take the index of t2.id, then it is the same as case2

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b39013-2c151.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b39013-2c151.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?