Managing SecureFiles-Next Generation LOBs
-------------------------------------------------
Be aware that in Oracle Database 11g, the default type of LOB is still BasicFiles. However, in Oracle Database 12c, the default type of LOB is now SecureFiles.
Prerequisites for SecureFiles:
- A SecureFile LOB must be stored in a tablespace using the automated segment space management feature (ASSM-segment space management auto).
- The DB_SECUREFILE initialization must be either PERMITTED(default) or ALWAYS.
NEVER: The LOB is created as a BasicFile regardless of whether the SECUREFILE option is specified.
PERMITTED: SecureFile LOBs can be created.
ALWAYS: The LOB is created as a SecureFile type unless the underlying tablespace isn’t using ASSM.
IGNORE: The SECUREFILE option is ignored, along with any SecureFile settings.
If you use LOBs in Oracle Database 11g or higher, use the new SecureFiles architecture.
SecureFiles is the new LOB architecture going forward; provides new features such as compression, encryption, and deduplication
create table patchmain(
patch_id number
,patch_desc clob)
lob(patch_desc) store as securefile (tablespace lob_data);
If you don’t specify the SECUREFILE clause when creating a table with a LOB column, then by default the LOB is created as a BasicFile LOB.
SQL> insert into patchmain values(1,'clob text');
select
segment_name
,segment_type
,segment_subtype
from user_segments;
select
table_name
,segment_name
,index_name
,securefile
,in_row
from user_lobs;
Encrypting LOBs:
You can transparently encrypt a SecureFile LOB column (just like any other column). Before you use
encryption features, you must set up an encryption wallet.
The ENCRYPT clause enables SecureFile encryption using Oracle Transparent Data Encryption (TDE).
CREATE TABLE patchmain(
patch_id number
,patch_desc clob)
LOB(patch_desc) STORE AS SECUREFILE (encrypt)
tablespace inv_clob;
CREATE TABLE patchmain(
patch_id number
,patch_desc clob encrypt)
LOB (patch_desc) STORE AS SECUREFILE;
You can verify the encryption details by querying the DBA_ENCRYPTED_COLUMNS view:
select
table_name
,column_name
,encryption_alg
from dba_encrypted_columns;
If you’ve already created the table, you can alter a column to enable encryption:
alter table patchmain modify
(patch_desc clob encrypt);
You can also specify an encryption algorithm. For example:
alter table patchmain modify
(patch_desc clob encrypt using '3DES168');
You can disable encryption for a SecureFile LOB column via the DECRYPT clause:
alter table patchmain modify
(patch_desc clob decrypt);
Modify table to be encrypted using the alter table encrypt clause. The following example shows how you can enable encryption for SecureFiles LOBs:
SQL> ALTER TABLE SECURE_DOCS MODIFY (BLOB_column ENCRYPT USING 'AES192');
You can query the USER_/DBA_ENCRYPTED_COLUMNS view to confirm that LOB
columns are encrypted.
SQL> SELECT TABLE_NAME, COLUMN_NAME, ENCRYPTION_ALG, SALT
FROM USER_ENCRYPTED_COLUMNS;
Once a SecureFiles LOB column is encrypted, you can disable encryption using the decrypt
keyword. The decrypt option will convert the encrypted column to clear text, as shown here:
SQL> ALTER TABLE SECURE_DOCS MODIFY (BLOB_CONTENT DECRYPT)
Compressing LOBs:
If you’re using SecureFile LOBs, then you can specify a degree of compression. The benefit is that the
LOBs consume much less space in the database. The downside is that reading and writing the LOBs may
take longer.
CREATE TABLE patchmain(
patch_id NUMBER
,patch_desc CLOB)
LOB(patch_desc) STORE AS SECUREFILE
(COMPRESS LOW)
TABLESPACE inv_clob;
HIGH: Highest degree of compression. Incurs higher latency when reading and writing the LOB.
MEDIUM: Medium level of compression. Default value if compression is specified but with no degree.
LOW: Lowest level of compression. Provides the lowest latency when reading and writing the LOB.
COMPRESS clause isn’t specified: No compression is used if you don’t specify the COMPRESS clause.
ALTER TABLE patchmain
MODIFY LOB(patch_desc)
(COMPRESS HIGH);
You can change the compression algorithm by specifying the compress high syntax to the alter table command, as shown here:
SQL> ALTER TABLE SECURE_DUP_COMPRESSED_DOCS
MODIFY LOB (BLOB_CONTENT) (COMPRESS HIGH);
If you need to uncompress the LOB segment for one reason or another, you can use the nocompress keyword:
SQL> ALTER TABLE SECURE_DOCS_HIGH MODIFY LOB (BLOB_CONTENT) (NOCOMPRESS);
ALTER TABLE patchmain
MODIFY LOB(patch_desc)
(NOCOMPRESS);
Tip: Try to enable compression, deduplication, and encryption through a CREATE TABLE statement. If you use an ALTER TABLE statement, the table is locked while the LOB is modified.
Deduplicating LOBs:
If you have an application where identical LOBs are associated with two or more rows, you should
consider using the SecureFile deduplication feature. When enabled, this instructs Oracle to check when
a new LOB is inserted into a table and see whether that LOB is already stored in another row (for the
same LOB column). If it’s already stored, then Oracle stores a pointer to the existing identical LOB. This
can potentially mean huge space savings for your application.
CREATE TABLE patchmain(
patch_id NUMBER
,patch_desc CLOB)
LOB(patch_desc) STORE AS SECUREFILE
(DEDUPLICATE)
TABLESPACE inv_clob;
alter table patchmain
modify lob(patch_desc) (deduplicate);
Here’s another example that modifies a partitioned LOB to enable deduplication:
alter table patchmain modify partition p2
lob (patch_desc) (deduplicate lob);
If you decide that you don’t want deduplication enabled, use the KEEP_DUPLICATES clause:
alter table patchmain
modify lob(patch_desc) (keep_duplicates);
Creating a Partitioned LOB:
You can partition LOBs by RANGE, LIST, or HASH.
CREATE TABLE patchmain(
patch_id NUMBER
,region VARCHAR2(16)
,patch_desc CLOB)
LOB(patch_desc) STORE AS (TABLESPACE patch1)
PARTITION BY LIST (REGION) (
PARTITION p1 VALUES ('EAST')
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch1 COMPRESS HIGH)
TABLESPACE inv_data1
,
PARTITION p2 VALUES ('WEST')
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch2 DEDUPLICATE NOCOMPRESS)
TABLESPACE inv_data2
,
PARTITION p3 VALUES (DEFAULT)
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch3 COMPRESS LOW)
TABLESPACE inv_data3
);
select
table_name
,column_name
,partition_name
,tablespace_name
,compression
,deduplication
from user_lob_partitions;
LOB compression can be enabled at the partition level:
SQL> create table docs_compressed_mix2
2 (document_id number,
3 blob_content blob,
4 document_category varchar2(55))
5 partition by list (document_category)
6 (
7 partition p_dba1 values ('Data Guard') lob(blob_content)
8 store as securefile(compress high),
9 partition p_dba2 values ('ASM') lob(blob_content)
10 store as securefile(compress medium),
11 partition p_dba3 values ('Java') lob(blob_content)
12 store as securefile(nocompress),
13 partition p_dba4 values (default) lob(blob_content)
14 store as securefile(compress high)
15* )
SQL> /
alter table patchmain modify partition p1
lob (patch_desc) (compress high);
The next example modifies a partitioned LOB so it doesn’t keep duplicate values (via the
DEDUPLICATE clause):
alter table patchmain modify partition p2
lob (patch_desc) (deduplicate lob);
Moving a LOB Column:
You can use the ALTER TABLE...MOVE...STORE AS statement to move a LOB column to a separate tablespace (from the table’s tablespace). Here’s the basic syntax:
alter table move lob() store as (tablespace
The next example moves the LOB column to the INV_CLOB tablespace:
alter table patchmain
move lob(patch_desc)
store as basicfile (tablespace inv_clob);
If the LOB column is populated with large amounts of data, you almost always want to store the LOB
in a tablespace separate from 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:
SQL> alter table inv add(inv_image blob);
OR
alter table inv add(inv_image blob)
lob(inv_image) store as securefile(tablespace lob_data);
Removing a LOB Column:
Before you remove a column, consider renaming it so that you can better identify whether any
applications or users are still accessing it:
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 drop it:
SQL> alter table patchmain drop(patch_desc_old);
Caching LOBs:
By default, when reading and writing LOB columns, Oracle doesn’t cache LOBs in memory. You can
change the default behavior by setting the cache-related storage options.
create table patchmain(
patch_id number
,patch_desc clob)
lob(patch_desc) store as (tablespace lob_data cache);
CACHE: Oracle should place LOB data in the buffer cache for faster access.
CACHE READS: Oracle should place LOB data in the buffer cache for reads but not for writes.
NOCACHE: LOB data shouldn’t be placed in the buffer cache. This is the default for both SecureFile and BasicFile LOBs.
You can verify the LOB caching with this query:
SQL> select table_name, column_name, cache from user_lobs;
Storing LOBs In and Out of Line:
By default, up to approximately 4000 characters of a LOB column are stored in line with the table row. If
the LOB is over 4000 characters, then Oracle automatically stores the LOB outside of the row data. The
main advantage of storing a LOB in row is that small LOBs (less than 4000 characters) require less I/O,
because Oracle doesn’t have to search out of row for the LOB data.
However, storing LOB data in row isn’t always desirable. The disadvantage of storing LOBs in row is
that the table row sizes are potentially longer. This can impact the performance of full-table scans, range
scans, and updates to columns other than the LOB column. In these situations, you may want to disable
storage in the row. For example, you explicitly instruct Oracle to store the LOB outside of the row with
the DISABLE STORAGE IN ROW clause:
create table patchmain(
patch_id number
,patch_desc clob
,log_file blob)
lob(patch_desc, log_file)
store as (
tablespace lob_data
disable storage in row);
If you want to store up to 4000 characters of a LOB in the table row, use the ENABLE STORAGE IN ROW clause when creating the table:
create table patchmain(
patch_id number
,patch_desc clob
,log_file blob)
lob(patch_desc, log_file)
store as (
tablespace lob_data
enable storage in row);
You can’t modify the LOB storage in a row after the table has been created. The only ways to alter
storage in row is to either move the LOB column or drop and re-create the table.
alter table patchmain
move lob(patch_desc)
store as (enable storage in row);
Migrating BasicFiles to SecureFiles
------------------------------------
You can migrate BasicFile LOB data to SecureFiles via one of the following methods:
1. Create a new table, load the data from the old table, and rename the tables.
2. Move the table.
3. Redefine the table online.
1. Creating a New Table:
create table patchmain_new(
patch_id number
,patch_desc clob)
lob(patch_desc) store as securefile (tablespace lob_data);
Next, load the newly created table with data from the old table:
SQL> insert into patchmain_new select * from patchmain;
Now, rename the tables:
SQL> rename patchmain to patchmain_old;
SQL> rename patchmain_new to patchmain;
When using this technique, be sure any grants that were pointing at the old table are reissued for the new table.
2. Moving a Table to a SecureFile Architecture:
alter table patchmain
move lob(patch_desc)
store as securefile (tablespace inv_clob);
You can verify that the column is now a SecureFile via this query:
SQL> select table_name, column_name, securefile from user_lobs;
3. Online Redefinition:
You can also redefine a table while it’s online via the DBMS_REDEFINITION package. Use the following steps to do an online redefinition:
a. Ensure that the table has a primary key. If the table doesn’t have a primary key, then create one:
alter table patchmain
add constraint patchmain_pk
primary key (patch_id);
b. Create a new table that defines the LOB column(s) as SecureFile:
create table patchmain_new(
patch_id number
,patch_desc clob)
lob(patch_desc)
store as securefile (tablespace lob_data);
c. Map the columns of the new table to the original table:
declare
l_col_map varchar2(2000);
begin
l_col_map := 'patch_id patch_id, patch_desc patch_desc';
dbms_redefinition.start_redef_table('DARL','PATCHMAIN','PATCHMAIN_NEW',l_col_map);
end;
/
d. Copy the data (this can take a long time if there are many rows):
set serverout on size 1000000
declare
l_err_cnt integer :=0;
begin
dbms_redefinition.copy_table_dependents(
'DARL','PATCHMAIN','PATCHMAIN_NEW',1,TRUE, TRUE, TRUE, FALSE, l_err_cnt
);
dbms_output.put_line('Num Errors: ' || l_err_cnt);
end;
/
e. Finish the redefinition:
begin
dbms_redefinition.finish_redef_table('DARL','PATCHMAIN','PATCHMAIN_NEW');
end;
/
You can confirm that the table has been redefined via this query:
SQL> select table_name, column_name, securefile from user_lobs;
Loading LOBs
-------------
Loading LOB data isn’t typically the DBA’s job, but you should be familiar with techniques used to
populate LOB columns. Developers may come to you for help with troubleshooting, performance, or space-related issues.
Loading a CLOB:
First, create an Oracle database directory object that points to the operating system directory in which the CLOB file is stored. This directory object is used when loading the CLOB. In this example, the Oracle directory object is named LOAD_LOB and the operating system directory is /home/oracle/scripts:
SQL> create or replace directory load_lob as '/home/oracle/scripts';
For reference, listed next is the DDL used to create the table in which the CLOB file is loaded:
create table patchmain(
patch_id number primary key
,patch_desc clob
,patch_file blob)
lob(patch_desc, patch_file)
store as securefile (compress low) tablespace lob_data;
This example also uses a sequence named PATCH_SEQ. Here’s the sequence-creation script:
SQL> create sequence patch_seq;
The following bit of code uses the DBMS_LOB package to load a text file (named patch.txt) into a CLOB
column. In this example, the table name is PATCHMAIN and the CLOB column is PATCH_DESC:
declare
src_clb bfile; -- point to source CLOB on file system
dst_clb clob; -- destination CLOB in table
src_doc_name varchar2(300) := 'patch.txt';
src_offset integer := 1; -- where to start in the source CLOB
dst_offset integer := 1; -- where to start in the target CLOB
lang_ctx integer := dbms_lob.default_lang_ctx;
warning_msg number; -- returns warning value if bad chars
begin
src_clb := bfilename('LOAD_LOB',src_doc_name); -- assign pointer to file
--
insert into patchmain(patch_id, patch_desc) -- create LOB placeholder
values(patch_seq.nextval, empty_clob())
returning patch_desc into dst_clb;
--
dbms_lob.open(src_clb, dbms_lob.lob_readonly); -- open file
--
-- load the file into the LOB
dbms_lob.loadclobfromfile(
dest_lob => dst_clb,
src_bfile => src_clb,
amount => dbms_lob.lobmaxsize,
dest_offset => dst_offset,
src_offset => src_offset,
bfile_csid => dbms_lob.default_csid,
lang_context => lang_ctx,
warning => warning_msg
);
dbms_lob.close(src_clb); -- close file
--
dbms_output.put_line('Wrote CLOB: ' || src_doc_name);
end;
/
You can place this code in a file and execute it from the SQL command prompt. In this example, the file that contains the code is named clob.sql:
SQL> set serverout on size 1000000
SQL> @clob.sql
Here’s the expected output:
Wrote CLOB: patch.txt
PL/SQL procedure successfully completed.
Loading a BLOB
---------------
Loading a BLOB is similar to loading a CLOB. This example uses the same directory object, table, and sequence from the previous example, which loaded a CLOB. Loading a BLOB is simpler than loading a CLOB because you don’t have to specify character-set information.
This example loads a file named patch.zip into the PATCH_FILE BLOB column:
declare
src_blb bfile; -- point to source BLOB on file system
dst_blb blob; -- destination BLOB in table
src_doc_name varchar2(300) := 'patch.zip';
src_offset integer := 1; -- where to start in the source BLOB
dst_offset integer := 1; -- where to start in the target BLOB
begin
src_blb := bfilename('LOAD_LOB',src_doc_name); -- assign pointer to file
--
insert into patchmain(patch_id, patch_file)
values(patch_seq.nextval, empty_blob())
returning patch_file into dst_blb; -- create LOB placeholder column first
dbms_lob.open(src_blb, dbms_lob.lob_readonly);
--
dbms_lob.loadblobfromfile(
dest_lob => dst_blb,
src_bfile => src_blb,
amount => dbms_lob.lobmaxsize,
dest_offset => dst_offset,
src_offset => src_offset
);
dbms_lob.close(src_blb);
dbms_output.put_line('Wrote BLOB: ' || src_doc_name);
end;
/
You can place this code in a file and run it from the SQL command prompt. In this example, the file that contains the code is named blob.sql:
SQL> set serverout on size 1000000
SQL> @blob.sql
Here’s the expected output:
Wrote BLOB: patch.zip
PL/SQL procedure successfully completed.
References:
http://www.oracle.com/technetwork/articles/sql/11g-securefiles-084075.html
http://dbashahid.blogspot.com/search?updated-max=2012-06-27T00:24:00-07:00&max-results=7&start=33&by-date=false(Insert an Image File into Oracle Database)
http://oracle-base.com/articles/8i/export-blob.php
http://oracle-base.com/articles/8i/export-clob.php
http://oracle-base.com/articles/8i/import-blob.php
http://oracle-base.com/articles/8i/import-clob.php
http://oracle-base.com/articles/8i/intermedia-import-export-of-images.php
Master Note: Overview of Oracle Large Objects (BasicFiles LOBs and SecureFiles LOBs) (Doc ID 1490228.1)
-------------------------------------------------
Be aware that in Oracle Database 11g, the default type of LOB is still BasicFiles. However, in Oracle Database 12c, the default type of LOB is now SecureFiles.
Prerequisites for SecureFiles:
- A SecureFile LOB must be stored in a tablespace using the automated segment space management feature (ASSM-segment space management auto).
- The DB_SECUREFILE initialization must be either PERMITTED(default) or ALWAYS.
NEVER: The LOB is created as a BasicFile regardless of whether the SECUREFILE option is specified.
PERMITTED: SecureFile LOBs can be created.
ALWAYS: The LOB is created as a SecureFile type unless the underlying tablespace isn’t using ASSM.
IGNORE: The SECUREFILE option is ignored, along with any SecureFile settings.
If you use LOBs in Oracle Database 11g or higher, use the new SecureFiles architecture.
SecureFiles is the new LOB architecture going forward; provides new features such as compression, encryption, and deduplication
create table patchmain(
patch_id number
,patch_desc clob)
lob(patch_desc) store as securefile (tablespace lob_data);
If you don’t specify the SECUREFILE clause when creating a table with a LOB column, then by default the LOB is created as a BasicFile LOB.
SQL> insert into patchmain values(1,'clob text');
select
segment_name
,segment_type
,segment_subtype
from user_segments;
select
table_name
,segment_name
,index_name
,securefile
,in_row
from user_lobs;
Encrypting LOBs:
You can transparently encrypt a SecureFile LOB column (just like any other column). Before you use
encryption features, you must set up an encryption wallet.
The ENCRYPT clause enables SecureFile encryption using Oracle Transparent Data Encryption (TDE).
CREATE TABLE patchmain(
patch_id number
,patch_desc clob)
LOB(patch_desc) STORE AS SECUREFILE (encrypt)
tablespace inv_clob;
CREATE TABLE patchmain(
patch_id number
,patch_desc clob encrypt)
LOB (patch_desc) STORE AS SECUREFILE;
You can verify the encryption details by querying the DBA_ENCRYPTED_COLUMNS view:
select
table_name
,column_name
,encryption_alg
from dba_encrypted_columns;
If you’ve already created the table, you can alter a column to enable encryption:
alter table patchmain modify
(patch_desc clob encrypt);
You can also specify an encryption algorithm. For example:
alter table patchmain modify
(patch_desc clob encrypt using '3DES168');
You can disable encryption for a SecureFile LOB column via the DECRYPT clause:
alter table patchmain modify
(patch_desc clob decrypt);
Modify table to be encrypted using the alter table encrypt clause. The following example shows how you can enable encryption for SecureFiles LOBs:
SQL> ALTER TABLE SECURE_DOCS MODIFY (BLOB_column ENCRYPT USING 'AES192');
You can query the USER_/DBA_ENCRYPTED_COLUMNS view to confirm that LOB
columns are encrypted.
SQL> SELECT TABLE_NAME, COLUMN_NAME, ENCRYPTION_ALG, SALT
FROM USER_ENCRYPTED_COLUMNS;
Once a SecureFiles LOB column is encrypted, you can disable encryption using the decrypt
keyword. The decrypt option will convert the encrypted column to clear text, as shown here:
SQL> ALTER TABLE SECURE_DOCS MODIFY (BLOB_CONTENT DECRYPT)
Compressing LOBs:
If you’re using SecureFile LOBs, then you can specify a degree of compression. The benefit is that the
LOBs consume much less space in the database. The downside is that reading and writing the LOBs may
take longer.
CREATE TABLE patchmain(
patch_id NUMBER
,patch_desc CLOB)
LOB(patch_desc) STORE AS SECUREFILE
(COMPRESS LOW)
TABLESPACE inv_clob;
HIGH: Highest degree of compression. Incurs higher latency when reading and writing the LOB.
MEDIUM: Medium level of compression. Default value if compression is specified but with no degree.
LOW: Lowest level of compression. Provides the lowest latency when reading and writing the LOB.
COMPRESS clause isn’t specified: No compression is used if you don’t specify the COMPRESS clause.
ALTER TABLE patchmain
MODIFY LOB(patch_desc)
(COMPRESS HIGH);
You can change the compression algorithm by specifying the compress high syntax to the alter table command, as shown here:
SQL> ALTER TABLE SECURE_DUP_COMPRESSED_DOCS
MODIFY LOB (BLOB_CONTENT) (COMPRESS HIGH);
If you need to uncompress the LOB segment for one reason or another, you can use the nocompress keyword:
SQL> ALTER TABLE SECURE_DOCS_HIGH MODIFY LOB (BLOB_CONTENT) (NOCOMPRESS);
ALTER TABLE patchmain
MODIFY LOB(patch_desc)
(NOCOMPRESS);
Tip: Try to enable compression, deduplication, and encryption through a CREATE TABLE statement. If you use an ALTER TABLE statement, the table is locked while the LOB is modified.
Deduplicating LOBs:
If you have an application where identical LOBs are associated with two or more rows, you should
consider using the SecureFile deduplication feature. When enabled, this instructs Oracle to check when
a new LOB is inserted into a table and see whether that LOB is already stored in another row (for the
same LOB column). If it’s already stored, then Oracle stores a pointer to the existing identical LOB. This
can potentially mean huge space savings for your application.
CREATE TABLE patchmain(
patch_id NUMBER
,patch_desc CLOB)
LOB(patch_desc) STORE AS SECUREFILE
(DEDUPLICATE)
TABLESPACE inv_clob;
alter table patchmain
modify lob(patch_desc) (deduplicate);
Here’s another example that modifies a partitioned LOB to enable deduplication:
alter table patchmain modify partition p2
lob (patch_desc) (deduplicate lob);
If you decide that you don’t want deduplication enabled, use the KEEP_DUPLICATES clause:
alter table patchmain
modify lob(patch_desc) (keep_duplicates);
Creating a Partitioned LOB:
You can partition LOBs by RANGE, LIST, or HASH.
CREATE TABLE patchmain(
patch_id NUMBER
,region VARCHAR2(16)
,patch_desc CLOB)
LOB(patch_desc) STORE AS (TABLESPACE patch1)
PARTITION BY LIST (REGION) (
PARTITION p1 VALUES ('EAST')
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch1 COMPRESS HIGH)
TABLESPACE inv_data1
,
PARTITION p2 VALUES ('WEST')
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch2 DEDUPLICATE NOCOMPRESS)
TABLESPACE inv_data2
,
PARTITION p3 VALUES (DEFAULT)
LOB(patch_desc) STORE AS SECUREFILE
(TABLESPACE patch3 COMPRESS LOW)
TABLESPACE inv_data3
);
select
table_name
,column_name
,partition_name
,tablespace_name
,compression
,deduplication
from user_lob_partitions;
LOB compression can be enabled at the partition level:
SQL> create table docs_compressed_mix2
2 (document_id number,
3 blob_content blob,
4 document_category varchar2(55))
5 partition by list (document_category)
6 (
7 partition p_dba1 values ('Data Guard') lob(blob_content)
8 store as securefile(compress high),
9 partition p_dba2 values ('ASM') lob(blob_content)
10 store as securefile(compress medium),
11 partition p_dba3 values ('Java') lob(blob_content)
12 store as securefile(nocompress),
13 partition p_dba4 values (default) lob(blob_content)
14 store as securefile(compress high)
15* )
SQL> /
alter table patchmain modify partition p1
lob (patch_desc) (compress high);
The next example modifies a partitioned LOB so it doesn’t keep duplicate values (via the
DEDUPLICATE clause):
alter table patchmain modify partition p2
lob (patch_desc) (deduplicate lob);
Moving a LOB Column:
You can use the ALTER TABLE...MOVE...STORE AS statement to move a LOB column to a separate tablespace (from the table’s tablespace). Here’s the basic syntax:
alter table
The next example moves the LOB column to the INV_CLOB tablespace:
alter table patchmain
move lob(patch_desc)
store as basicfile (tablespace inv_clob);
If the LOB column is populated with large amounts of data, you almost always want to store the LOB
in a tablespace separate from 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:
SQL> alter table inv add(inv_image blob);
OR
alter table inv add(inv_image blob)
lob(inv_image) store as securefile(tablespace lob_data);
Removing a LOB Column:
Before you remove a column, consider renaming it so that you can better identify whether any
applications or users are still accessing it:
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 drop it:
SQL> alter table patchmain drop(patch_desc_old);
Caching LOBs:
By default, when reading and writing LOB columns, Oracle doesn’t cache LOBs in memory. You can
change the default behavior by setting the cache-related storage options.
create table patchmain(
patch_id number
,patch_desc clob)
lob(patch_desc) store as (tablespace lob_data cache);
CACHE: Oracle should place LOB data in the buffer cache for faster access.
CACHE READS: Oracle should place LOB data in the buffer cache for reads but not for writes.
NOCACHE: LOB data shouldn’t be placed in the buffer cache. This is the default for both SecureFile and BasicFile LOBs.
You can verify the LOB caching with this query:
SQL> select table_name, column_name, cache from user_lobs;
Storing LOBs In and Out of Line:
By default, up to approximately 4000 characters of a LOB column are stored in line with the table row. If
the LOB is over 4000 characters, then Oracle automatically stores the LOB outside of the row data. The
main advantage of storing a LOB in row is that small LOBs (less than 4000 characters) require less I/O,
because Oracle doesn’t have to search out of row for the LOB data.
However, storing LOB data in row isn’t always desirable. The disadvantage of storing LOBs in row is
that the table row sizes are potentially longer. This can impact the performance of full-table scans, range
scans, and updates to columns other than the LOB column. In these situations, you may want to disable
storage in the row. For example, you explicitly instruct Oracle to store the LOB outside of the row with
the DISABLE STORAGE IN ROW clause:
create table patchmain(
patch_id number
,patch_desc clob
,log_file blob)
lob(patch_desc, log_file)
store as (
tablespace lob_data
disable storage in row);
If you want to store up to 4000 characters of a LOB in the table row, use the ENABLE STORAGE IN ROW clause when creating the table:
create table patchmain(
patch_id number
,patch_desc clob
,log_file blob)
lob(patch_desc, log_file)
store as (
tablespace lob_data
enable storage in row);
You can’t modify the LOB storage in a row after the table has been created. The only ways to alter
storage in row is to either move the LOB column or drop and re-create the table.
alter table patchmain
move lob(patch_desc)
store as (enable storage in row);
Migrating BasicFiles to SecureFiles
------------------------------------
You can migrate BasicFile LOB data to SecureFiles via one of the following methods:
1. Create a new table, load the data from the old table, and rename the tables.
2. Move the table.
3. Redefine the table online.
1. Creating a New Table:
create table patchmain_new(
patch_id number
,patch_desc clob)
lob(patch_desc) store as securefile (tablespace lob_data);
Next, load the newly created table with data from the old table:
SQL> insert into patchmain_new select * from patchmain;
Now, rename the tables:
SQL> rename patchmain to patchmain_old;
SQL> rename patchmain_new to patchmain;
When using this technique, be sure any grants that were pointing at the old table are reissued for the new table.
2. Moving a Table to a SecureFile Architecture:
alter table patchmain
move lob(patch_desc)
store as securefile (tablespace inv_clob);
You can verify that the column is now a SecureFile via this query:
SQL> select table_name, column_name, securefile from user_lobs;
3. Online Redefinition:
You can also redefine a table while it’s online via the DBMS_REDEFINITION package. Use the following steps to do an online redefinition:
a. Ensure that the table has a primary key. If the table doesn’t have a primary key, then create one:
alter table patchmain
add constraint patchmain_pk
primary key (patch_id);
b. Create a new table that defines the LOB column(s) as SecureFile:
create table patchmain_new(
patch_id number
,patch_desc clob)
lob(patch_desc)
store as securefile (tablespace lob_data);
c. Map the columns of the new table to the original table:
declare
l_col_map varchar2(2000);
begin
l_col_map := 'patch_id patch_id, patch_desc patch_desc';
dbms_redefinition.start_redef_table('DARL','PATCHMAIN','PATCHMAIN_NEW',l_col_map);
end;
/
d. Copy the data (this can take a long time if there are many rows):
set serverout on size 1000000
declare
l_err_cnt integer :=0;
begin
dbms_redefinition.copy_table_dependents(
'DARL','PATCHMAIN','PATCHMAIN_NEW',1,TRUE, TRUE, TRUE, FALSE, l_err_cnt
);
dbms_output.put_line('Num Errors: ' || l_err_cnt);
end;
/
e. Finish the redefinition:
begin
dbms_redefinition.finish_redef_table('DARL','PATCHMAIN','PATCHMAIN_NEW');
end;
/
You can confirm that the table has been redefined via this query:
SQL> select table_name, column_name, securefile from user_lobs;
Loading LOBs
-------------
Loading LOB data isn’t typically the DBA’s job, but you should be familiar with techniques used to
populate LOB columns. Developers may come to you for help with troubleshooting, performance, or space-related issues.
Loading a CLOB:
First, create an Oracle database directory object that points to the operating system directory in which the CLOB file is stored. This directory object is used when loading the CLOB. In this example, the Oracle directory object is named LOAD_LOB and the operating system directory is /home/oracle/scripts:
SQL> create or replace directory load_lob as '/home/oracle/scripts';
For reference, listed next is the DDL used to create the table in which the CLOB file is loaded:
create table patchmain(
patch_id number primary key
,patch_desc clob
,patch_file blob)
lob(patch_desc, patch_file)
store as securefile (compress low) tablespace lob_data;
This example also uses a sequence named PATCH_SEQ. Here’s the sequence-creation script:
SQL> create sequence patch_seq;
The following bit of code uses the DBMS_LOB package to load a text file (named patch.txt) into a CLOB
column. In this example, the table name is PATCHMAIN and the CLOB column is PATCH_DESC:
declare
src_clb bfile; -- point to source CLOB on file system
dst_clb clob; -- destination CLOB in table
src_doc_name varchar2(300) := 'patch.txt';
src_offset integer := 1; -- where to start in the source CLOB
dst_offset integer := 1; -- where to start in the target CLOB
lang_ctx integer := dbms_lob.default_lang_ctx;
warning_msg number; -- returns warning value if bad chars
begin
src_clb := bfilename('LOAD_LOB',src_doc_name); -- assign pointer to file
--
insert into patchmain(patch_id, patch_desc) -- create LOB placeholder
values(patch_seq.nextval, empty_clob())
returning patch_desc into dst_clb;
--
dbms_lob.open(src_clb, dbms_lob.lob_readonly); -- open file
--
-- load the file into the LOB
dbms_lob.loadclobfromfile(
dest_lob => dst_clb,
src_bfile => src_clb,
amount => dbms_lob.lobmaxsize,
dest_offset => dst_offset,
src_offset => src_offset,
bfile_csid => dbms_lob.default_csid,
lang_context => lang_ctx,
warning => warning_msg
);
dbms_lob.close(src_clb); -- close file
--
dbms_output.put_line('Wrote CLOB: ' || src_doc_name);
end;
/
You can place this code in a file and execute it from the SQL command prompt. In this example, the file that contains the code is named clob.sql:
SQL> set serverout on size 1000000
SQL> @clob.sql
Here’s the expected output:
Wrote CLOB: patch.txt
PL/SQL procedure successfully completed.
Loading a BLOB
---------------
Loading a BLOB is similar to loading a CLOB. This example uses the same directory object, table, and sequence from the previous example, which loaded a CLOB. Loading a BLOB is simpler than loading a CLOB because you don’t have to specify character-set information.
This example loads a file named patch.zip into the PATCH_FILE BLOB column:
declare
src_blb bfile; -- point to source BLOB on file system
dst_blb blob; -- destination BLOB in table
src_doc_name varchar2(300) := 'patch.zip';
src_offset integer := 1; -- where to start in the source BLOB
dst_offset integer := 1; -- where to start in the target BLOB
begin
src_blb := bfilename('LOAD_LOB',src_doc_name); -- assign pointer to file
--
insert into patchmain(patch_id, patch_file)
values(patch_seq.nextval, empty_blob())
returning patch_file into dst_blb; -- create LOB placeholder column first
dbms_lob.open(src_blb, dbms_lob.lob_readonly);
--
dbms_lob.loadblobfromfile(
dest_lob => dst_blb,
src_bfile => src_blb,
amount => dbms_lob.lobmaxsize,
dest_offset => dst_offset,
src_offset => src_offset
);
dbms_lob.close(src_blb);
dbms_output.put_line('Wrote BLOB: ' || src_doc_name);
end;
/
You can place this code in a file and run it from the SQL command prompt. In this example, the file that contains the code is named blob.sql:
SQL> set serverout on size 1000000
SQL> @blob.sql
Here’s the expected output:
Wrote BLOB: patch.zip
PL/SQL procedure successfully completed.
http://dbashahid.blogspot.com/search?updated-max=2012-06-27T00:24:00-07:00&max-results=7&start=33&by-date=false(Insert an Image File into Oracle Database)
http://oracle-base.com/articles/8i/export-blob.php
http://oracle-base.com/articles/8i/export-clob.php
http://oracle-base.com/articles/8i/import-blob.php
http://oracle-base.com/articles/8i/import-clob.php
http://oracle-base.com/articles/8i/intermedia-import-export-of-images.php
Master Note: Overview of Oracle Large Objects (BasicFiles LOBs and SecureFiles LOBs) (Doc ID 1490228.1)