So I have a query like this:
SELECT tablea.name, tablea.views from tablea inner
join tableb on (tablea.id = tableb.id and tablea.balance > 0)
order by tablea.views asc limit 1
However, the problem is that when I run it, it runs quite slow (4+ seconds). Interestingly, when the 'order by' clause is removed, while keeping the limit 1, it runs in 0.005 seconds (approx).
Even more interestingly: when I don't join it to tableb, i.e.:
SELECT tablea.name, tablea.views from tablea
where tablea.balance > 0
order by tablea.views asc limit 1
The query runs in 0.005 seconds usually.
Notes:
- The column views in tablea is indexed
- tablea and tableb have a 1 to 1 relationship in terms of id, and roughly have the same amount of rows.
Why is there such a drastic difference in performance between the first query, the first query when 'order by' is removed, and the second query?
Would there anyway to make the sorting much faster when joining two tables?