Oracle设置和删除不可用列
1、不可用列是什么?
就是表中的1个或多个列被ALTER TABLE…SET UNUSED 语句设置为无法再被程序利用的列。
2、使用场景?
If you are concerned about the length of time it could take to drop column data from
all of the rows in a large table, you can use the ALTER TABLE…SET UNUSED statement.
如果你担心从一个大表中删除一列可能花费大量时间,你可以使用ALTER TABLE…SET UNUSED语句。
如果你有这个需求,要删除某一个读写频繁的大表上的某些列,
如果你在业务繁忙时间直接执行 ALTER TABLE ABC DROP (COLUMN);
可能会收到 ORA-01562 - failed to extend rollback segment number string,
这是因为在这个删除列的过程中你可能会可能消耗掉整个回滚表空间,造成这样的错误出现。
3、使用理由(原理和优势)?
3.1 设置不可用列
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。
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列的内部索引不会被移除。
3.2 删除不可用列
ALTER TABLE…DROP UNUSED COLUMNS 语句仅针对不可用列,用于正式删除被标识为不可用的列(物理上删除列同时回收被占用的空间)。
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语句中个,指定了可选条件 CHECKPOINT。
这个条件将在处理过程达到指定行数时触发一个检查点,此处为250. 检查点削减了在删除列操作中累积的undo logs的数量,从而避免潜在的undo空间耗尽。
ALTER TABLE hr.admin_emp DROP UNUSED COLUMNS CHECKPOINT 250;
4、使用限制?
1)无法删除属于 SYS 的表中的列
2)
5、语法结构?
ALTER TABLE…SET UNUSED(C1,C2..)
ALTER TABLE…DROP UNUSED COLUMNS
例如:
ALTER TABLE hr.admin_emp SET UNUSED (hiredate, mgr);
ALTER TABLE hr.admin_emp DROP UNUSED COLUMNS;
5、数据字典
USER_UNUSED_COL_TABS
ALL_UNUSED_COL_TABS
DBA_UNUSED_COL_TABS
SELECT * FROM DBA_UNUSED_COL_TABS;
OWNER TABLE_NAME COUNT
--------------------------- --------------------------- -----
HR ADMIN_EMP 2–count列代表不可用列数量
6、使用案例?
SCOTT@orcl> create table tmp_all_objects
2 AS
3 SELECT object_id, object_name
4 from dba_objects
5 ;
表已创建。SYS@orcl> exec show_space('TMP_ALL_OBJECTS','SCOTT');
Unformatted Blocks .................... 0
FS1 Blocks (0-25) .................... 0
FS2 Blocks (25-50) .................... 0
FS3 Blocks (50-75) .................... 0
FS4 Blocks (75-100) .................... 0
Full Blocks .................... 352
Total Blocks ........................... 384
Total Bytes ........................... 3,145,728
Total MBytes ........................... 3
Unused Blocks........................... 18
Unused Bytes ........................... 147,456
Last Used Ext FileId.................... 4
Last Used Ext BlockId................... 14,592
Last Used Block......................... 110
PL/SQL 过程已成功完成。
SYS@orcl> DESC DBA_UNUSED_COL_TABS
名称 是否为空? 类型
---------------------------------------- -------- ---------------------------
OWNER NOT NULL VARCHAR2(30)
TABLE_NAME NOT NULL VARCHAR2(30)
COUNT NUMBER
SYS@orcl> SELECT * FROM DBA_UNUSED_COL_TABS;
未选定行
SCOTT@orcl> ALTER TABLE TMP_ALL_OBJECTS SET UNUSED(OBJECT_NAME);
表已更改。
SYS@orcl> SELECT * FROM DBA_UNUSED_COL_TABS;
OWNER TABLE_NAME COUNT
------------------------------ ------------------------------ ----------
SCOTT TMP_ALL_OBJECTS 1
SYS@orcl> exec show_space('TMP_ALL_OBJECTS','SCOTT');
Unformatted Blocks .................... 0
FS1 Blocks (0-25) .................... 0
FS2 Blocks (25-50) .................... 0
FS3 Blocks (50-75) .................... 0
FS4 Blocks (75-100) .................... 0
Full Blocks .................... 352
Total Blocks ........................... 384
Total Bytes ........................... 3,145,728
Total MBytes ........................... 3
Unused Blocks........................... 18
Unused Bytes ........................... 147,456
Last Used Ext FileId.................... 4
Last Used Ext BlockId................... 14,592
Last Used Block......................... 110
PL/SQL 过程已成功完成。
--删除不可用列
SCOTT@orcl> ALTER TABLE TMP_ALL_OBJECTS DROP UNUSED COLUMNS CHECKPOINT 250;
表已更改。
SYS@orcl> SELECT * FROM DBA_UNUSED_COL_TABS;
未选定行
SYS@orcl> exec show_space('TMP_ALL_OBJECTS','SCOTT');
Unformatted Blocks .................... 0
FS1 Blocks (0-25) .................... 0
FS2 Blocks (25-50) .................... 1
FS3 Blocks (50-75) .................... 350
FS4 Blocks (75-100) .................... 1
Full Blocks .................... 0
Total Blocks ........................... 384
Total Bytes ........................... 3,145,728
Total MBytes ........................... 3
Unused Blocks........................... 18
Unused Bytes ........................... 147,456
Last Used Ext FileId.................... 4
Last Used Ext BlockId................... 14,592
Last Used Block......................... 110
PL/SQL 过程已成功完成。
--move操作,减少碎片
SCOTT@orcl> ALTER TABLE TMP_ALL_OBJECTS MOVE;
表已更改。
SYS@orcl> exec show_space('TMP_ALL_OBJECTS','SCOTT');
Unformatted Blocks .................... 0
FS1 Blocks (0-25) .................... 0
FS2 Blocks (25-50) .................... 0
FS3 Blocks (50-75) .................... 0
FS4 Blocks (75-100) .................... 0
Full Blocks .................... 113
Total Blocks ........................... 128
Total Bytes ........................... 1,048,576
Total MBytes ........................... 1
Unused Blocks........................... 5
Unused Bytes ........................... 40,960
Last Used Ext FileId.................... 4
Last Used Ext BlockId................... 14,808
Last Used Block......................... 3
PL/SQL 过程已成功完成。
--可以看到总块数下��
--删除测试表
SCOTT@orcl> drop table TMP_ALL_OBJECTS;
表已删除。1
7、关于不可用列的恢复(以下摘自网络)
刚才有个人问我如何修复被设置为UNUSED的字段,我考虑了一下,以下的方法可以恢复(以下步骤执行前要做好备份),没有经验的DBA不要轻易尝试。
1、创建实验表TTTA
SQL> CREATE TABLE TTTA ( A INTEGER,B INTEGER,C VARCHAR2(10),D INTEGER);
表已创建。
SQL> INSERT INTO TTTA VALUES (1,2,'3',4);
已创建 1 行。
SQL> INSERT INTO TTTA VALUES (2,3,'4',5);
已创建 1 行。
SQL> COMMIT;
提交完成。
ALTER TABLE TTTA SET UNUSED COLUMN C;
2、以下进行恢复
SQL> SELECT OBJ# FROM OBJ$ WHERE NAME='TTTA';
OBJ#
----------
32067
SELECT COL#,INTCOL#,NAME FROM COL$ WHERE OBJ#=32067;
COL# INTCOL# NAME
---------- ---------- ------------------------------
1 1 A
2 2 B
0 3 SYS_C00003_08031720:09:55$ 被UNUSED的字段
3 4 D
SQL> SELECT COLS FROM TAB$ WHERE OBJ#=32067;
COLS
----------
3 ------字段数变为3了
SQL> UPDATE COL$ SET COL#=INTCOL# WHERE OBJ#=32067;
已更新4行。
SQL> UPDATE TAB$ SET COLS=COLS+1 WHERE OBJ#=32067;
已更新 1 行。
UPDATE COL$ SET NAME='C' WHERE OBJ#=32067 AND COL#=3;
UPDATE COL$ SET PROPERTY=0 WHERE OBJ#=32067;
SQL> COMMIT;3、重启数据库
SQL> SELECT * FROM SCOTT.TTTA;
A B C D
---------- ---------- ---------- ----------
1 2 3 4
2 3 4 5恢复完成
: