Faster way to create materialized view from catenated queries

I often need to create a materialized view as a table constructed as the result of N SELECT statements like below.

But running the following query can take hours. Is there a faster way to do this?

  • I tried using CREATE VIEW instead of CREATE TABLE but querying that is 1000x slower per query
  • Most of the time I'm just adding a new UNION ALL statement at the end with a new wave of data.
CREATE TABLE myschema.catenated AS
    SELECT 1 AS wave, a1, COALESCE(a2, 0) AS a2, a3::!INT, ...., x93*prob AS a1500 FROM table1 WHERE v
UNION ALL
    SELECT 2  AS wave, a1, COALESCE(a2, 0) AS a2, a3::!INT, ...., x93*prob AS a1500 FROM table2 WHERE <condition>
UNION ALL
    SELECT 3 AS wave, a1, COALESCE(a2, 0) AS a2, a3::!INT, ...., x93*prob AS a1500 FROM table3 WHERE <condition>
UNION ALL
    SELECT 4 AS wave, a1, COALESCE(a2, 0) AS a2, a3::!INT, ...., x93*prob AS a1500 FROM table4 WHERE <condition>
...
UNION ALL
    SELECT 52 AS wave, a1, COALESCE(a2, 0) AS a2, a3::!INT, ...., x93*prob AS a1500 FROM table52 WHERE <condition>
Tagged:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file