Mysql performs optimization

The

database has such a table User and a user"s log table User_log
want to query the user according to the log, so the user has such a sql

SELECT * FROM (SELECT * FROM User_log Order By create_time LIMIT 0, 50) JOIN User ON User.user_id=User_log.user_id

the idea of this sql is that the User table is very large, so the direct subquery first finds 50 log and then connects the table to the user information, but it turns out that there is a problem with the efficiency. Explain later found that the execution order of mysql did not find 50 items before join as I imagined, but join first and then check, resulting in the User table being traversed. Excuse me, why is this?

PS:

1.mysql5.6.16
2.sqlmysql5.6.33
3.log
Nov.09,2021

according to the sql above, it is impossible to query user according to log

.
SELECT * FROM user WHERE user_id in 
  (
    SELECT user_id FROM user_log ORDER BY create_time LIMIT 0, 50
  )

this should meet your needs


I don't understand what you mean by "the User table has been traversed". Do you mean a full table scan?

list the results of explain, the index of the table, and the amount of data, otherwise there is no way to judge whether the execution plan is reasonable or not.
in theory, it is normal to use a full table scan if the user table does not have many records.


join: http://www.runoob.com/mysql/m.
INNER JOIN (inner join, or equivalent join): gets the record of the matching relationship between the fields in the two tables.
LEFT JOIN (left join): get all the records in the left table, even if there is no corresponding matching record in the right table.
RIGHT JOIN (right join): contrary to LEFT JOIN, it is used to get all the records in the right table, even if there are no matching records in the left table.

Menu