I have large tables in Postgres 10.4 (many millions rows) and three piped SELECT
queries. The percentage of rows contributed to the total result for each SELECT
is roughly:
- SELECT 1 : ~5 % rows
- SELECT 2 : ~5 % rows
- SELECT 3 : ~90 % rows
I can use UNION
and ORDER BY
and a RANK
column, but it's very slow because Postgres collects rows from all subqueries and applies ORDER BY RANK
on the result set.
Is there a way to have SELECT 1
executed, and only if that does not provide enough rows SELECT 2
is executed, etc.