UNION ... LIMIT в постгресе работает восхитительно:
> EXPLAIN ANALYZE SELECT y FROM test WHERE x = 66 LIMIT 10;
Total runtime: 0.146 ms
> EXPLAIN ANALYZE (SELECT y FROM test WHERE x = 66) UNION (SELECT y FROM test WHERE x = 67) LIMIT 10;
Total runtime: 86.011 ms
Но зато:
> EXPLAIN ANALYZE (SELECT y FROM test WHERE x = 66) UNION ALL (SELECT y FROM test WHERE x = 67) LIMIT 10;
Total runtime: 0.183 ms