I have two queries that are entirely equivalent in their results.
Method 1: Do everything in one query (takes 235 seconds)
Method 2: hold intermediate data in mytemptable (total about 18 seconds)
Method 2 appears to be WAY faster because it uses a nested loop, seq scan, index scan instead of some merge join approach. Anyway to force the faster query plan on query 1? (I have no need for saving the temporary table...)
Full source of original queries:
Method 1
Explain Analyze result for query 1
Explain Analyze Result for Query 1b