This is a general comment about union(..) in sqlalchemy

For this particular case I can't tell you as the code scrolls off to the right of the page for subscription_query so I can't read it easily.  I suspect that a nicer query would be possible using a join but I'm not sure of your data model off hand