I have a MySql query that looks like the following:
SELECT trace_access.Employe_Code, trace_access.Employe_Prenom,
trace_access.Employe_Nom, , trace_access.Evenement_Date, trace_access.Evenement_Heure
FROM trace_access
INNER JOIN emp
ON trace_access.Employe_Code = emp.Employe_CodeEmploye
LEFT JOIN user
ON emp.Employe_ID = user.Employe_ID
LEFT JOIN role
ON role.User_ID = user.User_ID
WHERE trace_access.Employe_Nom Not Like "TEST%NU"
ORDER BY trace_access.Evenement_Date DESC , trace_access.Evenement_Heure DESC
The table "trace_access"contains almost 20 million entries.
When I explain the query:
My question is why MySql didn't use the key for the emp table and how to avoid "Using temporary; Using filesort" ??
I have tried to froce it to use my index but that didn't work. The query lasts one hour and more, writes a file on /tmp folder that exceeds 8Go !!!
Any help ?
Thanks a lot.
With your current execution plan (to start with emp
), a full table scan is required and there is no usefull index on emp
. This can make sense when you only get a relatively small amount of rows after your join
to trace_access
, which probably isn't the case here.
To prevent the filesort
, you need an index that supports your order by
. So add, if it doesn't exist yet, the index trace_access(Evenement_Date, Evenement_Heure)
.
This might already be enough to make MySQL start with trace_access
. If not, replace INNER JOIN emp
with STRAIGHT_JOIN emp
. This will force MySQL to do so.
Also add an index for emp(Employe_CodeEmploye)
.
Depending on your data, you can try to add Employe_Code
and/or Employe_Nom
as 3rd and/or 4th column to the index on trace_access
, though it will probably not have much effect.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.