Why is a UNION on two numbers more efficient than their sum?

Why is a UNION on two numbers more efficient than their sum?

We want to ensure that there are no more than maxCount palettes at a location (with a certain type of inventory). In order to ensure that, we sum up locations with palettes on them or locations to which palettes will be located. Both statements work fine. However, my initial draft was not very performant. My advisor came up with a different draft which is much faster, but he could not explain why.

We want to ensure that there are no more than maxCount palettes at a location (with a certain type of inventory). In order to ensure that, we sum up locations with palettes on them or locations to which palettes will be located. Both statements work fine. However, my initial draft was not very performant. My advisor came up with a different draft which is much faster, but he could not explain why.

Slow statement:

 AND maxCount > ( 
    SELECT 
        trptoloc+atloc
    FROM
        (
            SELECT
                COUNT(DISTINCT loc2.locnam) AS atloc
            FROM
                loc_t loc2,
                pal_t pal,
                inv_t inv,
                art_t art
            WHERE
                loc.group = loc2.group
                AND   pal.locnam = loc2.locnam
                AND   inv.palnam = pal.palnam
                AND   inv.artid = art.artid
                AND   AND   art.feature = value
        ),
        (
            SELECT
                COUNT(DISTINCT loc2.locnam) AS trptoloc
            FROM
                trp_t trp,
                loc_t loc2,
                inv_t inv,
                art_t art
            WHERE
                trp.locnamezl = loc2.locnam
                AND   trp.palnam = inv.palnam
                AND   loc.group = loc2.group
                AND   inv.artid = art.artid
                AND   art.feature = value;
        )
)

Faster statement:

AND maxCount > (
 SELECT
 SUM(invpal)
 FROM
 (SELECT
    COUNT(distinct inv.palnam) AS invpal
    FROM
        inv_t inv,
        art_t art,
        pal_t pal,
        loc_T loc2
    WHERE
        inv.artid = art.artid
        AND   loc.group = loc2.group
        AND   pal.locnam = loc2.locnam
        AND   inv.palnam = pal.palnam
        AND   art.feature = value;
  UNION ALL
    SELECT COUNT(distinct inv.palnam) AS invpal
    FROM
        inv_t inv,
        art_t art,
        trp_t trp,
        loc_T loc2
    WHERE
        trp.locnamezl = loc2.locnam
        AND   trp.palnam = inv.palnam
        AND   loc.group = loc2.group
        AND   inv.artid = art.artid
        AND   art.feature = value;
)
)

Any hint or link that will help me understand the performance difference is appreciated!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Learn to Build SQL Query| Ultimate SQL and DataBase Concepts|Simpliv

Learn to Build SQL Query| Ultimate SQL and DataBase Concepts

An Introduction to Oracle SQL

An Introduction to Oracle SQL: A quick tour of the six principal clauses of the SELECT statement.

Real time Oracle database administration interview questions | Simpliv

Real time Oracle database administration interview questions