move key WQL restrictions from WHERE/IN to JOIN

We were seeing order of magnitude performance impacts for using things like this:
````
...WHERE type_id IN (SELECT id from cards....
````
instead of this:
````
..JOIN (select id from cards...) AS type_table ON cards.type_id = type_table.id
````

This PR attempts to implement and generalize that approach.
would be nice to add a SQL comment here (at least when warnings make it appropriate) so that we can tell at a glance what each join is for.