Simple Understanding First
UNION and UNION ALL both combine results from multiple queries.
Difference:
| Operator | What it does |
|---|---|
UNION | Combines data AND removes duplicates |
UNION ALL | Combines data WITHOUT removing duplicates |
Because UNION removes duplicates, Oracle must do an additional SORT UNIQUE operation.
That sorting consumes:
- CPU
- TEMP tablespace
- Memory
- Time
So if duplicate removal is NOT needed, always use UNION ALL.
Step-by-Step Example Using EMP Table
Assume classic EMP table:
| EMPNO | ENAME | DEPTNO | SAL |
|---|---|---|---|
| 7369 | SMITH | 20 | 800 |
| 7499 | ALLEN | 30 | 1600 |
| 7521 | WARD | 30 | 1250 |
| 7566 | JONES | 20 | 2975 |
| 7698 | BLAKE | 30 | 2850 |
| 7782 | CLARK | 10 | 2450 |
| 7839 | KING | 10 | 5000 |
No comments:
Post a Comment