为什么Oracle在这里添加隐藏列?

Mar*_*urg 18 oracle oracle12c

我们最近将客户系统迁移到Oracle 12c和我们产品的最新版本.此过程包括运行大量迁移脚本,这些脚本主要添加或更改表.我们注意到,在向表中添加列同时还提供默认值时,会创建一个额外的隐藏列SYS_NC00002$.

您应该能够使用以下代码重现此问题

create table xxx (a integer);
alter table xxx add (b integer default 1);

select table_name, column_name, data_type, data_length, column_id, default_length, data_default from user_tab_cols where table_name='XXX';

Table_Name|column_Name |data_Type|data_Length|column_Id|default_Length|data_Default|
------------------------------------------------------------------------------------
XXX       |A           |NUMBER   |         22|        1|              |            |
XXX       |SYS_NC00002$|RAW      |        126|         |              |            |
XXX       |B           |NUMBER   |         22|        2|             1|1           |
Run Code Online (Sandbox Code Playgroud)

当我填充表并查看该隐藏列中的值时,它们都是相同的:

select distinct SYS_NC00002$ from xxx;

Sys_Nc00002$|
-------------
01          |
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,当我没有立即设置默认值但在一个额外的声明中,没有创建额外的隐藏列.

create table xxy (a integer);
alter table xxy add (b integer);
alter table xxy modify b default 1;

select table_name, column_name, data_type, data_length, column_id, default_length, data_default from user_tab_cols where table_name='XXY';

Table_Name|column_Name|data_Type|data_Length|column_Id|default_Length|data_Default|
-----------------------------------------------------------------------------------
XXY       |A          |NUMBER   |         22|        1|              |            |
XXY       |B          |NUMBER   |         22|        2|             1|1           |
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释这个隐藏列是什么以及为什么它只在第一个例子中创建,而不是在第二个例子中创建?

0xd*_*xdb 7

在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); 
Run Code Online (Sandbox Code Playgroud)

现在我们将在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
Run Code Online (Sandbox Code Playgroud)

注意执行时间的差异:在5毫秒内更新了1M行!

执行计划显示:

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
Run Code Online (Sandbox Code Playgroud)

12c上的执行计划与11g相比,显示了涉及新内部列的复杂谓词部分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              
Run Code Online (Sandbox Code Playgroud)

注意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
Run Code Online (Sandbox Code Playgroud)

对于所有行,新列的值保持为NULL.不需要真正的更新,因此不会优化DDL语句.

是一篇OTN文章,更详细地解释了新的DDL优化.