The problem is that in a UNION, Oracle finds all the qualifying rows and then “deduplicates” them;
SQL> select * from dual
2 union
3 select * from dual;
D
-
X
SQL> select * from dual
2 union ALL
3 select * from dual;
D
-
X
X
SQL> create table test_table(pid number, name varchar2(200), sal number)
2 /
Table created.
SQL> insert into test_table values(1, ‘Scott’, 5000)
2 /
1 row created.
SQL> insert into test_table values(2, ‘Tiger’, 5000)
2 /
1 row created.
SQL>
SQL> insert into test_table values(1, ‘Scott’, 5000)
2 /
1 row created.
SQL> insert into test_table values(2, ‘Tiger’, 5000)
2 /
1 row created.
SQL>
SQL> create table test_table_1 as
2 select * from test_table where 1=2
3 /
Table created.
SQL> select * from test_table_1
2 /
no rows selected
SQL>
SQL> select pid, name, sal from test_table_1
2 union
3 select pid, name, sal from test_table
4 /
PID NAME SAL
———- ——————————– ———-
1 Scott 5000
2 Tiger 5000
SQL> select pid, name, sal from test_table_1
2 union all
3 select pid, name, sal from test_table
4 /
PID NAME SAL
———- ——————————– ———-
1 Scott 5000
2 Tiger 5000
1 Scott 5000
2 Tiger 5000
The bottom line: If you can use UNION ALL, by all means use it over UNION to avoid a costly deduplication step—a step that is probably not even necessary most of the time.
http://www.oracle.com/technology/oramag/oracle/04-nov/o64asktom.html