你的方法是正確的,但效率不高,(恕我直言)過於複雜。
我做了一個簡單的測試用類似下面的表格:
create table old_table(a number, b number);
begin
for i in 1..5 loop
insert into old_table select level, -level from dual connect by level <= 1000000;
commit;
end loop;
end;
隨着UNION
你:
create table new_table as
(
select * from old_table
UNION
select * from old_table
);
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 9934K| 246M| | 86405 (51)| 00:17:17 |
| 1 | LOAD AS SELECT | NEW_TABLE | | | | | |
| 2 | SORT UNIQUE | | 9934K| 246M| 343M| 86405 (51)| 00:17:17 |
| 3 | UNION-ALL | | | | | | |
| 4 | TABLE ACCESS FULL | OLD_TABLE | 4967K| 123M| | 3039 (1)| 00:00:37 |
| 5 | TABLE ACCESS FULL | OLD_TABLE | 4967K| 123M| | 3039 (1)| 00:00:37 |
--------------------------------------------------------------------------------------------
您可以嘗試通過在第二應用過濾器,以簡化UNION
掃描表格:
create table new_table1 as
(
select * from old_table
UNION
select * from old_table where null is not null
);
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 4967K| 123M| | 43291 (1)| 00:08:40 |
| 1 | LOAD AS SELECT | NEW_TABLE | | | | | |
| 2 | SORT UNIQUE | | 4967K| 123M| 171M| 43291 (1)| 00:08:40 |
| 3 | UNION-ALL | | | | | | |
| 4 | TABLE ACCESS FULL | OLD_TABLE | 4967K| 123M| | 3039 (1)| 00:00:37 |
|* 5 | FILTER | | | | | | |
| 6 | TABLE ACCESS FULL | OLD_TABLE | 4967K| 123M| | 3039 (1)| 00:00:37 |
--------------------------------------------------------------------------------------------
這就是DISTINCT
作用:
create table new_table2 as
(
select distinct * from old_table
);
---------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 4967K| 123M| | 43203 (1)| 00:08:39 |
| 1 | LOAD AS SELECT | NEW_TABLE2 | | | | | |
| 2 | HASH UNIQUE | | 4967K| 123M| 171M| 39759 (1)| 00:07:58 |
| 3 | TABLE ACCESS FULL | OLD_TABLE | 4967K| 123M| | 3039 (1)| 00:00:37 |
---------------------------------------------------------------------------------------------
無論方法,結果是一樣的,沒有必要明確寫入所有列名:
SQL> select count(1) from new_table union all
2 select count(1) from new_table1 union all
3 select count(1) from new_table2;
COUNT(1)
----------
1000000
1000000
1000000
這是完全有效的。我會添加一條評論,說' - 有意使用聯盟刪除重複「。 –
使用'select distinct * from old_table'有什麼問題? –
'UNION'是'UNION DISTINCT'的縮寫。 (和'SELECT'是'SELECT ALL'的簡寫形式...) – jarlh