Transparently Externalize BLOB To BFILE - Blog Dbi Services
Transparently Externalize BLOB To BFILE - Blog Dbi Services
Transparently Externalize BLOB To BFILE - Blog Dbi Services
By Franck Pachot
.
Storing documents within the database is the easiest, especially because you get them consistent with the metadata stored in the database. If
you store them externally, then you need to manage their backup, their synchronization to standby site, the consistency in case of flashback or
PITR, etc. However, documents grow (in number and in size thanks to better resolution of scan) and you don’t want a database where half of
the size are documents in read only. If you have no option (partitioning, compression, etc) then you may choose to store the documents
externally. This is usually a complete re-design of the application.
In this blog post, I’ve done a quick test I’ve done to transform some BLOB into External LOB (aka BFILE) and make it transparent to the
application.
It’s just a test of concept. Any comments are welcome if you think something is wrong here.
And I will fill it with the content of 3 binary files. Let’s take them in $ORACLE_HOME/bin just for the fun of it:
I’m using SQL*Loader to load them to the BLOB: Reject Read More
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 1/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
Infrastructure at your
SQL> host echo "load data Service
infile '/tmp/files.txt' into table DEMOTAB fields terminated by ' ' ( id char(10),filename
SQL>host cat /tmp/sqlldr.ctl
load data infile '/tmp/files.txt' into table DEMOTAB fields terminated by ' ' ( id char(10),filename char(255),doc l
OFFERING EXPERTISE TRAINING JOBS & CAREER NEWSROOM ABOUT DBI CONTACT BLOG
SQL> host cd $ORACLE_HOME/bin ; sqlldr demo/demo@//localhost/pdb control=/tmp/sqlldr.ctl
Table DEMOTAB:
3 Rows successfully loaded.
I’m creating a folder to store the files externally, and create a DIRECTORY for it:
My idea is not to move all BLOB to External LOB, but only part of them. For example, old documents can be externalized whereas current ones
stay in the database. That helps to control the database size without taking any risk about consistency in case of PITR.
I’ve there an inline procedure ‘lob_to_file’ that reads a LOB and writes it to a file. In the body of the PL/SQL block I call the procedure for the 2
first rows of my table, and once the files are externalized, I empty the DOC column (the BLOB) and set the EXTERNAL_DOC one (the BFILE):
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 2/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
exception when others then
Infrastructure at your Service
utl_file.fclose(filehandle);
raise;
end;
begin
OFFERING
for
EXPERTISE
c in ( select
TRAINING
* from DEMOTAB where id <=2JOBS
& CAREER NEWSROOM ABOUT DBI
) loop CONTACT BLOG
lob_to_file (c.doc, 'DEMODIR',c.filename);
update DEMOTAB set doc=null,external_doc=bfilename('DEMODIR',c.filename) where id=c.id;
end loop;
end;
/
PL/SQL procedure successfully completed.
Note: don’t take my code as an example. I did it quickly. You should know that best place for code examples is Tim Hall www.oracle-base.com
You see that first two rows have empty BLOB but a BFILE addressing the files in DEMODIR
The third row is untouched.
Now, my idea is to make it transparent for the application, so I create a view on it which transparently retrieves the External LOB when LOB is
null:
And now time to query. The application does a select into a BLOB so let’s do the same:
DOC
--------------------------------------------------------------------------------
23212F62696E2F7368200A230A230A232061636673726F6F740A23200A2320436F70797269676874
This is the LOB coming from the external file. I get it as a BLOB when I query the view.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept
And now querying the one that is still stored in the database: Reject Read More
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 3/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
Infrastructure at your Service
SQL> exec select doc into :doc from DEMOVIEW where id=3;
procedure successfully completed.
PL/SQL
SQL> print doc
OFFERING EXPERTISE
TRAINING JOBS & CAREER NEWSROOM ABOUT DBI CONTACT BLOG
DOC
--------------------------------------------------------------------------------
7F454C4602010100000000000000000002003E000100000000124000000000004000000000000000
Querying the view instead of the table (and you can play with synonyms for that) the application get the document without knowing wheter it
comes from the database or the external directory. It seems that externalizing binary documents do not require a re-design of the application.
One Comment
Hello Franck,
do you know whether there are already BFILE cloud adapters available or ahead to take this approach one step further?
Best regards,
Rainer Stenzel
Leave a Reply
My comment is..
Name *
Email *
Website
Save myuses
This website name, email,to
cookies and websiteyour
improve in this browser forWe'll
experience. the next time you're
assume I comment.
ok with this, but you can opt-out if you wish. Accept
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 4/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
Infrastructure at your Service
This site uses Akismet to reduce
OFFERING spam.Learn
EXPERTISE how yourcomment
TRAINING JOBS data is processed.
& CAREER NEWSROOM ABOUT DBI CONTACT BLOG
Search...
CATEGORIES
AWS
Big Data
Business Intelligence
Cloud
Control-M
Database management
DevOps
Docker
Kubernetes
MariaDB
MySQL
NoSQL
Operation systems
Oracle
Postgres
Security
SQL Server
Technology Survey
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.
Unclassified Accept
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 5/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
RECENT ARTICLES
Infrastructure at your Service
Getting started with Exasol – Distribution keys
OFFERING EXPERTISE TRAINING JOBS & CAREER NEWSROOM ABOUT DBI CONTACT BLOG
Control M/ EM Send Mail and Alert Window notification with HHMMSS format with a job
Control-M/EM Logon issue when connecting to the CCM Cannot get UTF8 locale
TAG CLOUD
12.2 Alfresco AlwaysOn Availability groups AWS CDB Cloud Cluster database Data Guard DBA Docker Documentum enterprisedb
High availability Installation Linux Linux/UNIX Microsoft Migration Monitoring multitenant ODA Oracle
Oracle 11g
Oracle 12c Oracle 18c Oracle Enterprise Manager Oracle Enterprise Manager Cloud 12c Oracle OpenWorld PDB Performance Pluggable
Databases PostgreSQL PowerShell RMAN Security SQL SQL Server SQL Server 2008 SQL Server 2012 SQL Server 2014
SQL Server 2016 Troubleshooting Upgrade
BLOG ROLL
Blog of Adar-Consult
Franck Pachot
Principal Consultant / Database Evangelist
Oracle ACE Director
Oracle Database OCM 12c certified
AWS Database Specialty certified
Oak Table member
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 6/7
4/11/2020 Transparently externalize BLOB to BFILE - Blog dbi services
Infrastructure at your Service
OFFERING EXPERTISE TRAINING JOBS & CAREER NEWSROOM ABOUT DBI CONTACT BLOG
EXPERTISE IN DATABASE & MIDDLEWARE
Microsoft
Oracle
Open Source DB
Operating system
USEFUL INFORMATION
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept
https://blog.dbi-services.com/transparently-externalize-blob-to-bfile/ 7/7