Search code examples
mysqlsqlquery-optimization

SQL : Join after Where Clause


Ive got a big SQL statement.

But i want to test the where clause on the firsttable (T1) and after that, make all the joins on the rows selected using the where clause.

Because actually the query is very slow, cause MySQL join all the tables to t1 and then test the where clause !

SELECT * from FIRSTTABLE T1
        LEFT JOIN T2 on (....)
        LEFT JOIN T3 on (....)
WHERE T1.column = '1' OR T1.column= '5'

Any ideas ?


Solution

  • You can do something like:

    SELECT * from 
    (
    SELECT * from FIRSTTABLE T1
    WHERE T1.column = '1' OR T1.column= '5'
    ) as T2 
    
    LEFT JOIN T3 on (....)
    LEFT JOIN T4 on (....)