oracle--set unused

一.使用set unused的情况

当表中的某一个列不再使用的时候,我们可以删除这个列

alter table xxx drop column xxx;

或者

alter table xxx drop (xxx,xxx);

当删除列的时候,需要注意的是,不能删除一个表的所有列和删除sys下面表的列。

You cannot drop all columns from a table, nor can you drop columns from a table owned by SYS. Any attempt to do so results in an error

当我们删除一个大表中的数据,这个操作会需要一定的时间。这个时候,我们就可以用ALTER TABLE XXX SET UNUSED;

This statement marks one or more columns as unused, but does not actually remove the target column data or restore the disk space occupied by these columns. However, a column that is marked as unused is not displayed in queries or data dictionary views, and its name is removed so that a new column can reuse that name. In most cases, constraints, indexes, and statistics defined on the column are also removed. The exception is that any internal indexes for LOB columns that are marked unused are not removed.

此语句将一个或多个列标记为未使用,但实际上并不删除目标列数据或恢复这些列占用的磁盘空间。但是,被标记为未使用的列不会显示在查询或数据字典视图中,它的名称将被删除,以便新列可以重用该名称。在大多数情况下,还会删除列上定义的约束、索引和统计信息。例外情况是,LOB列中标记为未使用的任何内部索引都不会被删除。

You can later remove columns that are marked as unused by issuing an ALTER TABLE...DROP UNUSED COLUMNS statement. Unused columns are also removed from the target table whenever an explicit drop of any particular column or columns of the table is issued.

稍后,您可以通过发出ALTER TABLE来删除标记为未使用的列…删除未使用的列语句。每当发出任何特定列或表的列的显式删除时,还将从目标表中删除未使用的列

The data dictionary views USER_UNUSED_COL_TABSALL_UNUSED_COL_TABS, or DBA_UNUSED_COL_TABS can be used to list all tables containing unused columns. The COUNT field shows the number of unused columns in the table.

USER_UNUSED_COL_TABS、ALL_UNUSED_COL_TABS或DBA_UNUSED_COL_TABS可用于列出包含未使用列的所有表。COUNT字段显示表中未使用的列的数量。

只是可以查看表明和unused的列的数量。无法查看具体的unused的列名

For external tables, the SET UNUSED statement is transparently converted into an ALTER TABLE DROP COLUMN statement. Because external tables consist of metadata only in the database, the DROP COLUMN statement performs equivalently to the SET UNUSED statement.

对于外部表,SET used语句被透明地转换为ALTER TABLE DROP COLUMN语句。因为外部表只包含数据库中的元数据,所以DROP列语句的执行与SET used语句相当。

The ALTER TABLE...DROP UNUSED COLUMNS statement is the only action allowed on unused columns. It physically removes unused columns from the table and reclaims disk space.

ALTER TABLE……删除未使用的列语句是未使用的列上唯一允许的操作。它从物理上删除表中未使用的列并回收磁盘空间。

 

In the ALTER TABLE statement that follows, the optional clause CHECKPOINT is specified. This clause causes a checkpoint to be applied after processing the specified number of rows, in this case 250. Checkpointing cuts down on the amount of undo logs accumulated during the drop column operation to avoid a potential exhaustion of undo space.

在随后的ALTER TABLE语句中,指定了可选子句检查点。此子句导致在处理指定行数(本例中为250)后应用检查点。检查点减少了drop列操作期间积累的撤消日志数量,以避免潜在的撤消空间耗尽。 

ALTER TABLE hr.admin_emp DROP UNUSED COLUMNS CHECKPOINT 250;

二.恢复unused的列

设置unused的作用是为了在cpu、内存等资源不充足的时候,先做上unused标记再等数据库资源空闲的时候用drop set unused删除

设置unused列之后,并不是将该列数据立即删除,而是被隐藏起来,物理上还是存在的,以下为恢复步骤:对数据字典不熟悉的朋友测试前做好备份工作
SQL> conn scott/oracle 
Connected.
SQL> create table xs (id number, name char(10),age number);
Table created.
SQL> insert into xs values(1,'JACK',20); 
1 row created.
SQL> insert into xs values(2,'BILL',21);
1 row created.
SQL> insert into xs values(3,'TOM',22);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from xs;
>select * from xs;
 ID      NAME              AGE
---------- ----------         ----------
         1 JACK               20
         2 BILL               21
         3 TOM                22
SQL> alter table xs set unused column AGE;
Table altered.
SQL> select * from xs;
 ID      NAME
---------- ----------
         1 JACK
         2 BILL
         3 TOM
SQL> SET LINESIZE 200 设置显示列宽
SQL> SELECT OBJECT_ID,OBJECT_NAME FROM USER_OBJECTS;
OBJECT_ID OBJECT_NAME
--------------------------------------------------------------------------------------------------     51147 PK_DEPT
     51146 DEPT
     51148 EMP
     51149 PK_EMP
     51150 BONUS
     51151 SALGRADE
     52613 D
     52614 D1
     52615 D3
     52696 BIG
     52717 XS------------------------ OBJECT_ID=52717
11 rows selected.
SQL> conn / as sysdba
Connected.
SQL> select col#,intcol#,name from col$ where obj#=52717;
      COL#    INTCOL# NAME
---------- ---------- ------------------------------
         1          1 ID
         2          2 NAME
         0          3 SYS_C00003_12092313:06:51$----------原来的列名为C,被系统修了.
SQL> select cols from tab$ where obj#=71930;
COLS
----------
      2    -----------------------系统的字段数目也发生了变化
SQL> update col$ set col#=intcol# where obj#=52717;
3 rows updated.
SQL> update tab$ set cols=cols+1 where obj#=52717;
1 row updated.
SQL> update col$ set name='AGE' where obj#=52717 and col#=3;
1 row updated.
SQL> update col$ set property=0 where obj#=52717;
3 rows updated.
SQL> commit;
Commit complete.
SQL> startup force;   -----------这一步是必不可少的
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size                  1218992 bytes
Variable Size              92276304 bytes
Database Buffers          188743680 bytes
Redo Buffers                2973696 bytes
Database mounted.
Database opened.
SQL> select * from scott.xs; 
ID     NAME      AGE
---------- ---------- ----------
         1 JACK       20
         2 BILL        21
         3 TOM        22
 
我们可以看到,对于设置了unused的列,恢复的话我们需要知道设置之前列的名称。
 
 
 

 

 

猜你喜欢

转载自www.cnblogs.com/liang-ning/p/12132601.html
今日推荐