将数据加载到数据库时,我们经常使用临时表。我们可以将数据加载到一个或多个临时表中,然后在将其加载到目标表之前对其进行转换。因此,这些暂存表中的数据是不稳定的。
Oracle 数据库 23c 在命令中引入了该子句,用于创建堆表的变体,这些堆表具有用于快速数据摄取的最佳配置。FOR STAGING``CREATE TABLE
创建暂存表
在下面的示例中,我们创建两个堆表,其中一个使用子句。FOR STAGING
drop table if exists staging_tab purge;
drop table if exists normal_tab purge;
create table staging_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
)
for staging;
create table normal_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
);
视图中的列指示该表是临时表。STAGING``{USER|ALL|DBA|CDB}_TABLES
column table_name format a30
column staging format a10
select table_name,
staging
from user_tables
where table_name like '%TAB';
TABLE_NAME STAGING
------------------------------ ----------
NORMAL_TAB NO
STAGING_TAB YES
SQL>
我们可以使用该命令将现有堆表转换为暂存表。ALTER TABLE
alter table normal_tab for staging;
select table_name,
staging
from user_tables
where table_name like '%TAB';
TABLE_NAME STAGING
------------------------------ ----------
NORMAL_TAB YES
STAGING_TAB YES
SQL>
我们可以使用子句将其转换回来。NOT FOR STAGING
alter table normal_tab not for staging;
select table_name,
staging
from user_tables
where table_name like '%TAB';
TABLE_NAME STAGING
------------------------------ ----------
NORMAL_TAB NO
STAGING_TAB YES
SQL>
压缩
对暂存表禁用压缩。我们可以创建一个带有压缩子句的临时表,但压缩仍将被禁用。
drop table if exists staging_tab purge;
drop table if exists normal_tab purge;
create table staging_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
)
for staging
compress;
create table normal_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
)
compress;
select table_name,
staging,
compression
from user_tables
where table_name like '%TAB';
TABLE_NAME STAGING COMPRESS
------------------------------ ---------- --------
NORMAL_TAB NO ENABLED
STAGING_TAB YES DISABLED
SQL>
包含压缩数据的现有表可以切换到临时表,但将来的插入不会被压缩。在下面的示例中,我们将一些数据插入到启用了压缩的普通表中。然后,我们将其切换到暂存表。
insert into normal_tab (data1, data2, data3, data4)
select 'data1 value', 'data1 value', 'data1 value', 'data1 value'
from dual
connect by level <= 1000;
alter table normal_tab for staging;
select table_name,
staging,
compression
from user_tables
where table_name like '%TAB';
TABLE_NAME STAGING COMPRESS
------------------------------ ---------- --------
NORMAL_TAB YES DISABLED
STAGING_TAB YES DISABLED
SQL>
我们无法更改临时表以在创建后添加压缩。
drop table if exists staging_tab purge;
create table staging_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
)
for staging;
alter table staging_tab compress;
alter table staging_tab compress
*
ERROR at line 1:
ORA-38500: Invalid operation on Staging Table
Help: https://docs.oracle.com/error-help/db/ora-38500/
SQL>
可以对临时表进行分区,但我们不能执行任何分区维护操作,这会导致数据被压缩。
统计学
暂存表仅使用动态采样,因此我们无法收集表统计信息。
exec dbms_stats.gather_table_stats(null,'STAGING_TABLE');
BEGIN dbms_stats.gather_table_stats(null,'STAGING_TABLE'); END;
*
ERROR at line 1:
ORA-20005: object statistics are locked (stattype = ALL)
ORA-06512: at "SYS.DBMS_STATS", line 42112
ORA-06512: at "SYS.DBMS_STATS", line 41397
ORA-06512: at "SYS.DBMS_STATS", line 9071
ORA-06512: at "SYS.DBMS_STATS", line 10135
ORA-06512: at "SYS.DBMS_STATS", line 40597
ORA-06512: at "SYS.DBMS_STATS", line 41545
ORA-06512: at "SYS.DBMS_STATS", line 42093
ORA-06512: at line 1
Help: https://docs.oracle.com/error-help/db/ora-20005/
SQL>
回收站
暂存表不受回收站保护。
首先,我们以原始形式重新创建表。
drop table if exists staging_tab purge;
drop table if exists normal_tab purge;
create table staging_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
)
for staging;
create table normal_tab (
id number generated always as identity,
data1 varchar2(100),
data2 varchar2(100),
data3 varchar2(100),
data4 varchar2(100)
);
我们检查回收站,它是空的。
SQL> show recyclebin
SQL>
我们删除了没有选项的表。PURGE
drop table if exists staging_tab;
drop table if exists normal_tab;
我们检查回收站,只有普通表存在。暂存表尚未放入回收站。
SQL> show recyclebin
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
NORMAL_TAB BIN$E4gmN27IIGDgZQAAAAAAAQ==$0 TABLE 2024-03-12:20:10:42
SQL>