'Which of these SQL queries will be better for query performance?

So I have some tables with millions of rows of data, and the current query I have is like the following:

WITH first_table AS
(
    SELECT 
        A.column1, A.column2, B.column1 AS column3, C.column1 AS column4 
    FROM
        tableA AS A 
    LEFT JOIN 
        tableB AS B ON A.id = B.id
    LEFT JOIN 
        tableC AS C ON A.id = C.id
    UNION ALL
    SELECT 
        D.column1, D.column2, NULL AS column3, D.column4 
    FROM
        tableD AS D
    UNION ALL
    ...
)
SELECT 
    column1, column2, column3, column4, A.col5, A.col6... until A.col20
FROM 
    first_table 
LEFT JOIN 
    tableA AS A ON first_table.id = A.id

I'm basically appending two tables at least to table A and then joining again table A in the final SELECT statement. I do this because I need like 30 columns from table A and I don't want to fill with NULL values the append statement since I only need 4 or 5 columns from the tables appended to the main one (tableA).

I was wondering if it would be better to avoid the join and then fill all the columns I need since the WITH statement or should I keep my code as it is. All of this is for query performance and improve execution time.

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source