Optimize this query: SQL I have this query in MySQL:

SELECT 
    p.idSentence, 
    p.idPath, 
    p.token, 
    p.posTag, 
    p.ner, 
    p.isTV, 
    r.relationType, 
    t.tag     
FROM path p 
 LEFT OUTER JOIN relation r ON(p.idTokenN=r.idTokenN) 
 JOIN contentDoc t ON p.idContDoc=t.idContDoc
 JOIN (
    SELECT idPath, 
           MAX(isTV='true') AS hasTV,
           COUNT(*) AS pathLength
    FROM path
    GROUP BY idPath 

  ) pf 
 ON p.idPath=pf.idPath 
    AND pf.hasTV =1 
    AND pf.pathLength BETWEEN 2 AND 5
 JOIN (
     SELECT idSentence,COUNT(DISTINCT(idPath)) AS paths
     FROM path
     GROUP BY idSentence
    ) ps
 ON p.idSentence=ps.idSentence
    AND ps.paths BETWEEN 1 AND 3 ;


Because the data are so many, the query takes a long time to run, how can I optimize it to speed it up?
Here is some recommentations:

Make sure all the columns used in the joins are indexed properly.

if possible, replace subqueries with indexed temporary tables.

Move when possible the filtering clause from the join statement to the where statement (e.g. Where  pf.pathLength BETWEEN 2 AND 5)

Try to move the joins order to start filtering with the table which reduce the more efficiently the returned dataset.