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 过程已成功完成。