Re-writing MySQl query for execute faster

Try to cut the query in smaller parts, using temporary tables. Having sub queries is always time, memory and cpu consuming.
That should do the trick.