Originally from @jon-chuang on #2248 (comment), copied to new ticket so it didn't get lost:
Some additional ideas for subquery optimizations:
More generally, one can attempt this approach, which is a lot less messy than relying on countless rewriting heuristics (which is also highly error-prone):