根据新的设置,如何进行合并?
CREATE TABLE TABLE_SEQ AS
SELECT * FROM
(SELECT ID, SEQ AS seq_a
FROM TABLE_A)
ALIAS_A,
(SELECT ID AS id_b, SEQ AS seq_b
FROM TABLE_B)
ALIAS_B
WHERE ALIAS_A.seq_a = ALIAS_B.seq_b
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.06 0 0 0 0
Execute 1 10.64 24.43 12186 12370 5677 1000000
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 10.64 24.49 12186 12370 5677 1000000
SELECT * FROM
(SELECT ID, SEQ AS seq_a
FROM TABLE_A)
ALIAS_A,
(SELECT ID AS id_b, SEQ AS seq_b
FROM TABLE_B)
ALIAS_B
WHERE ALIAS_A.seq_a = ALIAS_B.seq_b
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.00 0.06 0 0 0 0
Execute 1 10.64 24.43 12186 12370 5677 1000000
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 10.64 24.49 12186 12370 5677 1000000
有趣的是,既然数据并非如此不同,性能也只是略差。那么解释计划展示的是什么?使用ROWNUM原始测试,我们有:
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------
Plan hash value: 1354216904
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10G| 496G| | 15M (2)| 50:40:43 |
| 1 | LOAD AS SELECT | TABLE_ROWNUM | | | | | |
|* 2 | HASH JOIN | | 10G| 496G| 36M| 186K (97)| 00:37:19 |
| 3 | VIEW | | 1009K| 25M| | 1597 (10)| 00:00:20 |
| 4 | COUNT | | | | | | |
| 5 | TABLE ACCESS FULL | TABLE_B | 1009K| 4930K| | 1381 (11)| 00:00:17 |
| 6 | VIEW | | 1016K| 25M| | 1475 (10)| 00:00:18 |
| 7 | COUNT | | | | | | |
| 8 | TABLE ACCESS FULL | TABLE_A | 1016K| 3969K| | 1285 (12)| 00:00:16 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ALIAS_A"."ROWNUM_A"="ALIAS_B"."ROWNUM_B")
-------------------------------------------------------------------------------------------------
Plan hash value: 1354216904
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10G| 496G| | 15M (2)| 50:40:43 |
| 1 | LOAD AS SELECT | TABLE_ROWNUM | | | | | |
|* 2 | HASH JOIN | | 10G| 496G| 36M| 186K (97)| 00:37:19 |
| 3 | VIEW | | 1009K| 25M| | 1597 (10)| 00:00:20 |
| 4 | COUNT | | | | | | |
| 5 | TABLE ACCESS FULL | TABLE_B | 1009K| 4930K| | 1381 (11)| 00:00:17 |
| 6 | VIEW | | 1016K| 25M| | 1475 (10)| 00:00:18 |
| 7 | COUNT | | | | | | |
| 8 | TABLE ACCESS FULL | TABLE_A | 1016K| 3969K| | 1285 (12)| 00:00:16 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ALIAS_A"."ROWNUM_A"="ALIAS_B"."ROWNUM_B")
基于序列的合并似乎是一个更好的计划。
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------
Plan hash value: 1354216904
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10G| 496G| | 15M (2)| 50:40:43 |
| 1 | LOAD AS SELECT | TABLE_ROWNUM | | | | | |
|* 2 | HASH JOIN | | 10G| 496G| 36M| 186K (97)| 00:37:19 |
| 3 | VIEW | | 1009K| 25M| | 1597 (10)| 00:00:20 |
| 4 | COUNT | | | | | | |
| 5 | TABLE ACCESS FULL | TABLE_B | 1009K| 4930K| | 1381 (11)| 00:00:17 |
| 6 | VIEW | | 1016K| 25M| | 1475 (10)| 00:00:18 |
| 7 | COUNT | | | | | | |
| 8 | TABLE ACCESS FULL | TABLE_A | 1016K| 3969K| | 1285 (12)| 00:00:16 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ALIAS_A"."ROWNUM_A"="ALIAS_B"."ROWNUM_B")
-------------------------------------------------------------------------------------------------
Plan hash value: 1354216904
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 10G| 496G| | 15M (2)| 50:40:43 |
| 1 | LOAD AS SELECT | TABLE_ROWNUM | | | | | |
|* 2 | HASH JOIN | | 10G| 496G| 36M| 186K (97)| 00:37:19 |
| 3 | VIEW | | 1009K| 25M| | 1597 (10)| 00:00:20 |
| 4 | COUNT | | | | | | |
| 5 | TABLE ACCESS FULL | TABLE_B | 1009K| 4930K| | 1381 (11)| 00:00:17 |
| 6 | VIEW | | 1016K| 25M| | 1475 (10)| 00:00:18 |
| 7 | COUNT | | | | | | |
| 8 | TABLE ACCESS FULL | TABLE_A | 1016K| 3969K| | 1285 (12)| 00:00:16 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ALIAS_A"."ROWNUM_A"="ALIAS_B"."ROWNUM_B")
虽然这是一个相对较小的数据集,你可以明白为什么执行该计划的花费可能会引起误解。如果基于序列的表在同一会话中被删除和重新建立,创建表重新删除的时间到刚刚超过2秒。在表面上看,第二轮创建的表似乎要快得多,但真正要证明的是什么呢?
所要证明的是,数据块已经读入缓存,从缓存中读取数据块的速度将远远超过从磁盘双方读取的速度(这我们已经知道的事实) 。它实际意义是:你创建表需要多少时间?这通常是一次性完成。如果原始表被删除和重创,它的创建时间将大大加快。
通过清除共享池和缓存来恢复性能, 在ROWNUM和基于序的列情况下所花费的时间分别 14秒和10秒的。在这一点上,它可能看起来像是混为一谈。但在运行期间,其性能级别交换了。这也许是事实,但不要忘记设置了序列为基础的表格的费用(按时间)。
总结
从某种意义上说,最为相似的数据集,操作系统和平台依赖性(多少行,内存和I / O等) ,他们可以更快地在不同数据集之间添加一个共同的属性,然后在进行合并操作。对于较小的数据集,也许略高于100万行,我冒昧地说,使用ROWNUM这将永远是比新增一个合并关键字更快,即使使用常见的关键创建表的速度更快。那么,什么时候适当使用ROWNUM ?当在没有共同关键字的情况时,你不关心表之间的特殊关联,即使是正好就存在这样的事实。如果你正在处理相关表,他们基于一个共同的属性,并且这些关联必须排序,你一定不能依赖ROWNUM保持合并表之间的顺序。它事关,在一个表中具体行是否与第二个表中特定行匹配。