The problem is most likely the inner sort.
You should have indexes on surfed(site, user, cpc)
, surf(active, user, site)
, and user(id, coins)
.
You can also perhaps make minor improvements by switching the join
to inner joins from outer joins. The where
clause is undoing the left outer join
anyway, so this won't affect the results.
But I don't think these changes will really help. The problem is the sort of the result set in the inner query. The outer sort by rand()
is a minor issue, because you are only doing that on 100 rows.
If you are running this 1,000/minute, you will need to rethink your data structures and develop an approach that has the data you need more readily available.