use joins instead of subqueries

for better mysql perf

sits on top of #28