Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL UNION Optimization

I have 4 tables named A1, A2, B1, B2.

To fulfill a requirement, I have two ways to write SQL queries. The first one is:

(A1 UNION ALL A2) A JOIN (B1 UNION ALL B2) B ON A.id = B.a_id WHERE ...

And the second one is:

(A1 JOIN B1 on A1.id = B1.a_id WHERE ...) UNION ALL (A2 JOIN B2 on A2.id = B2.a_id WHERE ... )

I tried both approaches and realized they both give the same execution time and query plans in some specific cases. But I'm unsure whether they will always give the same performance or not.

So my question is when the first/second one is better in terms of performance?

In terms of coding, I prefer the first one because I can create two views on (A1 UNION ALL A2) as well as (B1 UNION ALL B2) and treat them like two tables.

like image 363
Anh Duc Ng Avatar asked Mar 19 '26 10:03

Anh Duc Ng


1 Answers

The second one is better:

(A1 JOIN B1 on A1.id = B1.a_id WHERE ...) UNION ALL (A2 JOIN B2 on A2.id = B2.a_id WHERE ... )

It gives more information to Oracle CBO optimizer about how your tables are related to each other. CBO can calculate potentials plans' costs more precisely. It's all about cardinality, column statistics, etc.

like image 73
Sayan Malakshinov Avatar answered Mar 21 '26 23:03

Sayan Malakshinov