在 Oracle 版本 11g 中,Oracle 引入了一种新的优化技术来提高 DDL 操作的性能。这项新功能在添加NOT NULL具有默认值的列到现有表。自版本 12c 以来,DDL 优化已扩展到包括NULL具有默认值的列。
考虑以下包含 1.000.000 行的测试表:
sql> create table xxy
as select rownum a from dual connect by level <= 1e6
;
sql> select /*+ gather_plan_statistics */ count(1) from xxy;
sql> select * from table(dbms_xplan.display_cursor);
现在我们将在 11g 和 12c 的不同会话中添加一个额外的非空列,该列具有默认值:
11g> alter table xxy add b number default 1;
--Table XXY altered. Elapsed: 00:01:00.998
12c> alter table xxy add b number default 1;
--Table XXY altered. Elapsed: 00:00:00.052
请注意执行时间的差异:5 毫秒内更新了 100 万行!?
执行计划显示:
11g> select count(1) from xxy where b = 1;
COUNT(1)
----------
1000000
11g> select * from table(dbms_xplan.display_cursor);
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1040 (100)| |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
|* 2 | TABLE ACCESS FULL| XXY | 898K| 11M| 1040 (1)| 00:00:13 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("B"=1)
Note
-----
- dynamic sampling used for this statement (level=2)
12c> select count(1) from xxy where b = 1;
12c> select * from table(dbms_xplan.display_cursor);
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 429 (100)| |
| 1 | SORT AGGREGATE | | 1 | 5 | | |
|* 2 | TABLE ACCESS FULL| XXY | 1000K| 4882K| 429 (2)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter(DECODE(TO_CHAR(SYS_OP_VECBIT("SYS_NC00002$",0)),NULL,NVL("
B",1),'0',NVL("B",1),'1',"B")=1)
Note
-----
- statistics feedback used for this statement
与 11g 相比,12c 上的执行计划显示了涉及新内部列的复杂谓词部分SYS_NC00006$
.
该谓词表明,Oracle 在内部仍然认为 B 列可能包含非默认值。这意味着 - Oracle 首先不会用默认值物理更新每一行。
为什么要建立新的内部专栏SYS_NC00006$
被建造?
12c> select column_name, virtual_column, hidden_column, user_generated
from user_tab_cols
where table_name = 'XXY'
;
COLUMN_NAME VIR HID USE
---------------- --- --- ---
B NO NO YES
SYS_NC00002$ NO YES NO
A NO NO YES
12c> select a, b, SYS_NC00002$ hid from xxy where a in (1,10);
A B HID
---------- ---------- ----------------
1 1
10 1
12c> update xxy set b=1 where a=10 and b=1;
1 row updated.
12c> select a, b, SYS_NC00002$ hid from xxy where a in (1,10);
A B HID
---------- ---------- ----------------
1 1
10 1 01
请注意 B 和相关内部列的值的差异。 Oracle 只是通过其系统生成的内部列进行检查(例如SYS_NC00006$
)并通过SYS_OP_VECBIT
函数是否考虑 B 列的默认值或通过显式 DML 语句修改的实际值。
两个单独的 alter 语句有什么用?
12c> alter table xxy add (b integer);
12c> alter table xxy modify b default 1;
12c> select count(b), count(coalesce(b,0)) nulls from xxy where b = 1 or b is null;
COUNT(B) NULLS
---------- ----------
0 1000000
所有行的新列值均保持为 NULL。不需要真正的更新,因此 DDL 语句不会被优化。
Here http://www.oracle.com/technetwork/articles/database/ddl-optimizaton-in-odb12c-2331068.html是一篇 OTN 文章,更详细地解释了新的 DDL 优化。