Speed things up by using subquery instead of join