Insert Oracle clob Column Oracle tips by Burleson Consulting May 5, 2010 |
Question:I have a table with a clob column (xmltype datatype) and I want to know the best way to do an insert into this large object column. so the SQL insert basically inserts a record with New XML, and a SQL updated replaces the old XML. what is the fastest way to do a large batch insert of XML into an unstructured clob table column?
Answer:When dealing with large objects, where are relatively few tuning options other than using a large enough blocksize to ensure that the xmltype column does not fragment onto multiple data blocks.
Also, super-fast storage makes a big difference for inserts, and SSD have do well over 100,000 rows per second insert rates. Here are the speed options for various insert methods:
When storing a lob, you can either load it inline, or leave it in a flat file and use bfile:
- Store the clob as bfile-You can also leave the clob in a flat file, and use the bfile utility to map the clob Into ORACLE usingDbms_lob.loadfromfileUtility. the advantage is that you don't have to actually load the clob into an oracle tablespace, but the disadvantage is that the clob is outside of Oracle, and it cannot be managed (backed-up, kept consistent) is if the clob resided inside a table. see here, how to store image files into ORACLE tables. bfile is fastest because the clob is never really loaded into a tablespace, it's still outside oracle in a flat file.
- Store the clob inside the table-The third option is to use a PL/SQL procedure to store the clob directly into an oracle table, and you use the sameDbms_lob.loadfromfileUtility.
- Store the clob using PHP:Some use PHP to store clob columns.
- Use SQL * Loader:SQL * loader is the fastest way to bulk load data. See here how to load a clob with SQL * loader.
Storing a clob column into a table
Below are procedures for loading a PDF file, very similar to loading any clob file:
Procedure to load a PDF as a bfile:
Create or replace procedure load_lob
ID number;
Image1 blob;
Locator bfile;
Bfile_len number;
Bf_desc varchar2 (30 );
Bf_name varchar2 (30 );
Bf_dir varchar2 (30 );
Bf_typ varchar2 (4 );
CTR integer;
Cursor get_id is
Select bfile_id, bfile_desc, bfile_type from graphics_table;
Begin
Open get_id;
Loop
Fetch get_id into ID, bf_desc, bf_typ;
Exit when get_id % notfound;
Dbms_output.put_line ('Id: '| to_char (ID ));
Select bfile_loc into locator from graphics_table where bfile_id = ID;
Dbms_lob.filegetname (
Locator, bf_dir, bf_name );
Dbms_output.put_line ('dir: '| bf_dir );
Dbms_lob.fileopen (locator, dbms_lob.file_readonly );
Bfile_len: = dbms_lob.getlength (Locator );
Dbms_output.put_line ('Id: '| to_char (ID) | 'length:' | to_char (bfile_len ));
Select temp_blob into image1 from temp_blob; bfile_len: = dbms_lob.getlength (Locator );
Dbms_lob.loadfromfile (image1, locator, bfile_len, 1, 1 );
Insert into internal_graphics values (ID, bf_desc, image1, bf_typ );
Dbms_output.put_line (bf_desc | 'length: '| to_char (bfile_len) |
'Name: '| bf_name | 'dir:' | bf_dir | ''| bf_typ );
Dbms_lob.fileclose (Locator );
End loop;
End;
/
Procedure to load a PDF into a blob column of a table:
Code
Create or replace procedure load_lob
ID number;
Image1 blob;
Locator bfile;
Bfile_len number;
Bf_desc varchar2 (30 );
Bf_name varchar2 (30 );
Bf_dir varchar2 (30 );
Bf_typ varchar2 (4 );
CTR integer;
Cursor get_id is
Select bfile_id, bfile_desc, bfile_type from graphics_table;
Begin
Open get_id;
Loop
Fetch get_id into ID, bf_desc, bf_typ;
Exit when get_id % notfound;
Dbms_output.put_line ('Id: '| to_char (ID ));
Select bfile_loc into locator from graphics_table where bfile_id = ID;
Dbms_lob.filegetname (
Locator, bf_dir, bf_name );
Dbms_output.put_line ('dir: '| bf_dir );
Dbms_lob.fileopen (locator, dbms_lob.file_readonly );
Bfile_len: = dbms_lob.getlength (Locator );
Dbms_output.put_line ('Id: '| to_char (ID) | 'length:' | to_char (bfile_len ));
Select temp_blob into image1 from temp_blob;
Bfile_len: = dbms_lob.getlength (Locator );
Dbms_lob.loadfromfile (image1, locator, bfile_len, 1, 1 );
Insert into internal_graphics values (ID, bf_desc, image1, bf_typ );
Dbms_output.put_line (bf_desc | 'length: '| to_char (bfile_len) |
'Name: '| bf_name | 'dir:' | bf_dir | ''| bf_typ );
Dbms_lob.fileclose (Locator );
End loop;
End;
/