Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop columns in a compressed table?

After compressing a table, it's not possible to drop a column any more:

CREATE TABLE foo (p NUMBER, i NUMBER, j NUMBER) COMPRESS BASIC;

ALTER TABLE foo DROP COLUMN j;
ORA-39726: unsupported add/drop column operation on compressed tables

It is possible if advance compression is used:

CREATE TABLE foo (p NUMBER, i NUMBER, j NUMBER) COMPRESS FOR OLTP;

ALTER TABLE foo DROP COLUMN j;
Table FOO altered.

However, the column is not really dropped, just hidden:

SELECT column_name, data_type, hidden_column 
  FROM user_tab_cols WHERE table_name = 'FOO';

COLUMN_NAME                 DATA_TYPE  HIDDEN_COLUMN
P                           NUMBER     NO
I                           NUMBER     NO
SYS_C00002_18030204:09:26$  NUMBER     YES

This causes an error when partitions are exchanged:

CREATE TABLE par (p NUMBER, i NUMBER) 
 PARTITION BY LIST(p)(
 PARTITION p1 VALUES(1), 
 PARTITION p2 VALUES(2)
);

ALTER TABLE par EXCHANGE PARTITION p1 WITH TABLE foo;
ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION

How can the hidden column be removed? I tried

ALTER TABLE foo DROP UNUSED COLUMNS;

but it doesn't help:

SELECT column_name, data_type, hidden_column 
  FROM user_tab_cols WHERE table_name = 'BAR';

COLUMN_NAME                 DATA_TYPE  HIDDEN_COLUMN
P                           NUMBER     NO
I                           NUMBER     NO
SYS_C00002_18030204:09:26$  NUMBER     YES
like image 700
wolφi Avatar asked May 30 '18 08:05

wolφi


People also ask

How do I drop a column in a compressed table?

ALTER TABLE foo MOVE NOCOMPRESS; ALTER TABLE foo DROP UNUSED COLUMNS; ALTER TABLE foo MOVE COMPRESS FOR OLTP; SELECT column_name, data_type, hidden_column FROM user_tab_cols WHERE table_name = 'FOO'; COLUMN_NAME DATA_TYPE HIDDEN_COLUMN P NUMBER NO I NUMBER NO ALTER TABLE par EXCHANGE PARTITION p1 WITH TABLE foo; Table ...

How do you drop a column?

Right-click the column you want to delete and choose Delete Column from the shortcut menu. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.

How do I get rid of unused columns?

Logical Deletealter table table_name set unused (column_name); alter table table_name set unused (column_name1, column_name2); Once this is done the columns will no longer be visible to the user. If at a later date you have time to physically delete the columns this can be done using the following.


2 Answers

Oracle support document 1987500.1 "How to Drop Columns in Compressed Tables" had the solution: the table needs to be uncompressed first, the the columns removed, then it can be compressed again:

ALTER TABLE foo MOVE NOCOMPRESS;
ALTER TABLE foo DROP UNUSED COLUMNS;
ALTER TABLE foo MOVE COMPRESS FOR OLTP;

SELECT column_name, data_type, hidden_column 
  FROM user_tab_cols WHERE table_name = 'FOO';

COLUMN_NAME                 DATA_TYPE  HIDDEN_COLUMN
P                           NUMBER     NO
I                           NUMBER     NO

ALTER TABLE par EXCHANGE PARTITION p1 WITH TABLE foo;
Table PAR altered.

N.B. The compression/recompression is fast if the table is empty.

like image 125
wolφi Avatar answered Sep 20 '22 00:09

wolφi


update foo set j = null;
commit;
alter table foo set unused column j;

Read more

like image 25
Toolkit Avatar answered Sep 20 '22 00:09

Toolkit