I have the following MySQL query:
SELECT
table1.*, table2.*
FROM
`Table1` AS table1,
`Table2` AS table2,
`Table3`
WHERE
table1.col1 = table2.col1 AND
table1.col1 = Table3.col1 AND
table1.col1 != '0' AND
table1.col1 NOT IN (1,2)
table1.col2 LIKE 'A' AND
(table1.col3 LIKE 'A' OR table1.col3 LIKE 'B') AND
Table3.col4 = 0 AND
Table3.col5 = 0 AND
Table3.col6 = 0
ORDER BY
table1.col10 ASC
LIMIT 0, 5
Without the ORDER BY statement, it executes in 0.002 seconds.
With the ORDER BY statement, it executes in 2 seconds.
I saw this answer which seemed to work for the OP, but when I tried it on mine I got a Duplicate column name 'col1'
error.
Any ideas on how I can speed this up with the ORDER BY ?
EDIT:
As requested, below is the modified Query that I tried based on the link above which gave me the Duplicate column name 'col1'
error:
SELECT * FROM (
SELECT
table1.*, table2.*
FROM
`Table1` AS table1,
`Table2` AS table2,
`Table3`
WHERE
table1.col1 = table2.col1 AND
table1.col1 = Table3.col1 AND
table1.col1 != '0' AND
table1.col1 NOT IN (1,2)
table1.col2 LIKE 'A' AND
(table1.col3 LIKE 'A' OR table1.col3 LIKE 'B') AND
Table3.col4 = 0 AND
Table3.col5 = 0 AND
Table3.col6 = 0
) AS t1
ORDER BY
table1.col10 ASC
LIMIT 0, 5
Apparently, this is supposed to ensure the ORDER BY is only done after the final result set is determined.