# 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!

## Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

## Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

## Welcome Back the T-SQL Debugger with SQL Complete – SQL Debugger

Debug SQL stored procedures and develop your SQL database project with dbForge SQL Complete, a new add-in for Visual Studio and SSMS. When you develop large chunks of T-SQL code with the help of the SQL Server Management Studio tool, it is essential to test the “Live” behavior of your code by making sure that each small piece of code works fine and being able to allocate any error message that may cause a failure within that code.

## 3 Tips to Write SQL Queries That Are Easier to Read and Modify

3 practice tips to write SQL queries which are easier to read and modify even after six month and one year. Use this tips to write professional quality SQL.There is no doubt that writing code is more art than science and every coder cannot write beautiful code which is both readable and maintainable, even with the experience.

## Distributed SQL: An Evolution of the Database

The next step in the evolution of database architecture is distributed SQL. Take a look at some of the characteristics here.As organizations transition to the cloud, they eventually find that the legacy relational databases that are behind some of their most critical applications simply do not take advantage of the promise of the cloud and are difficult to scale.