I have a query that is returning 17,000 rows on a table called 'history'. It has about 15 left joins to other tables mainly based on history.id = othertable.primary_id
.
When I remove order by history.primary_id DESC
the query is very fast. When order by history.primary_id DESC
is there MySQL takes about 26 seconds to write to a temp table..so very slow.
However when "order by" is present, but the joins are omitted, it is quick.
I do not understand why the data being written to the temp table is dependent on the joins? Wouldn't MySQL already know what the 17,000 rows consist of by that point as it is after the query executes?
According to "EXPLAIN" my indexes are set up pretty well, but for the life of me I cannot sort this query. I want to use a limit of 1000 but it is useless when I cannot sort. I cant seem to get it to not use a temp table.