Maintaining LOB Columns- Large Objects
The following sections describe some common maintenance tasks that are performed on LOB columns or that otherwise involve LOB columns, including moving columns between tablespaces and adding new LOB columns to a table.
Moving a LOB Column
As mentioned previously, if you create a table with a LOB column and do not specify a tablespace, then, by default, the LOB is created in the same tablespace as its table. If the LOB column has started to consume large amounts of disk space and the DBA didn’t think about it before, you can use the ALTER TABLE … MOVE … STORE AS statement to move a LOB column to a different tablespace.
SQL> alter table patchmain move lob(patch_desc)
store as securefile (tablespace lob_data2);
You can verify that the LOB was moved by querying USER_LOBS:
SQL> select table_name, column_name, tablespace_name from user_lobs;
To summarize, if the LOB column is populated with large amounts of data, you almost always want to store the LOB in a tablespace separate from that of the rest of the table data. In these scenarios, the LOB data has different growth and storage requirements and is best maintained in its own tablespace.
Adding a LOB Column
If you have an existing table to which you want to add a LOB column, use the ALTER TABLE … ADD statement. The next statement adds the INV_IMAGE column to a table:
SQL> alter table patchmain add(inv_image blob);
This statement is fine for quickly adding a LOB column to a development environment. For anything else, you should specify the storage characteristics. For instance, this command specifies the LOB tablespace:
SQL> alter table patchmain add(inv_image blob) lob(inv_image) store as securefile(tablespace lob_data);
Removing a LOB Column
You may a have scenario in which your business requirements change and you no longer need a column. Before you remove a column, consider renaming it so that you can better identify whether any applications or users are still accessing it. LOBs have larger amounts (really, sizes) of data, and restoring can take longer.
SQL> alter table patchmain rename column patch_desc to patch_desc_old;
After you determine that nobody is using the column, use the ALTER TABLE … DROP statement to just remove that column:
SQL> alter table patchmain drop (patch_desc_old);
You can also remove a LOB column by dropping and re-creating a table (without the LOB column). This, of course, permanently removes any data as well.
Also, keep in mind that if your recycle bin is enabled, then when you do not drop a table with the PURGE clause, space is still consumed by the dropped table. If you want to remove the space associated with the table, use the PURGE clause, or purge the recycle bin after dropping the table.
Caching LOBs
By default, when reading and writing LOB columns, Oracle does not cache LOBs in memory. You can change the default behavior by setting the cache-related storage options. This example specifies that Oracle should cache a LOB column in memory:
SQL> create table patchmain ( patch_id number, patch_desc clob)
lob(patch_desc) store as (tablespace lob_data cache);
You can verify LOB caching with this query:
SQL> select table_name, column_name, cache from user_lobs;
Here is some sample output:
TABLE_NAME COLUMN_NAME CACHE
PATCHMAIN PATCH_DESC YES
Leave a Reply