This is a less experimental rewrite of the query that fixes the exact problem.
The speedups from this query fix are also substantial, going from 2.5 hours to 11 minutes.
This is likely a fix to these issues:
#1699, #2048, #2140, #2178
This could help with issues:
#1391, #2050