-
Oooh... I forgot all about SELECT DISTINCT ON despite having used it once in the past.
Did you forget or hit performance issues and drop the idea? DISTINCT can put a significant load on query execution. People often end up trying to use a combination of careful database design and GROUP BY to get better speed while still eliminating or minimising duplicates. In my experience, DISTINCT is most often useful with simpler, smaller datasets.
Proof is in the testing, ofc.
Maybe just whack a
distinct on
on the big union? (and update the order by clause appropriately)I haven't grokked the whole query so I could be way off.