Reading a LOB in Oracle is often quite easy: it's just a column in the query and all the libraries available today are doing the work necessary to make the LOBs content appear in the desired variable. What a nice world.
Unfortunately it is still a LOB and requires special methods to fetch the data. And especially because this is covered by libraries, LOBs can be called slow.
If fast access to many rows with many LOBs is required, leaving all the work to a single session might not meet the requirements. But LOBs also can be fetched in other sessions - kind of in parallel.
(this must NOT be mixed up with parallel queries - instead they are separate sessions doing the LOB interactions while the main session processes the tables queries).
As the specific implementation might be led by the requirements, my example here only shows what's possible.
I have a very small table with only 1 row:
create table T (id number, B clob);
insert into T values (1, 'aäoöuüsß');
commit;
A small perl program selects the row in one session and reads it content in another one.
(please do NOT use this script as a blueprint!)
my $sql = "SELECT id, B LOB_LOCATOR FROM T WHERE ROWNUM = 1";
my $sth = $dbh->prepare($sql, { ora_auto_lob => 0 })
or die "Couldn't prepare statement: $DBI::errstr";
$sth->execute() or die "Couldn't execute statement: $DBI::errstr";
# Get the first row
my ($id, $lob_locator) = $sth->fetchrow_array();
print "id: $id \n";
# Close the first connection
$sth->finish;
$dbh->disconnect;
print "ll: $lob_locator \n";
print " --- \n";
$dbh2 = DBI->connect("dbi:Oracle:host=$hostname;port=$port;service_name=$service_name", $username, $password)
or die "Couldn't connect to database: $DBI::errstr";
my $chunk_size = 1034; # Arbitrary chunk size, for example
my $offset = 1; # Offsets start at 1, not 0
my $data = $dbh2->ora_lob_read( $lob_locator, $offset, $chunk_size ) ;
print "lob: $data \n";
where the LOBs content is read and printed.
the execution looks like[oracle@localhost ~]$ perl lob.pl
id: 1
ll: OCILobLocatorPtr=SCALAR(0x2b388a8)
---
lob: aaoouus?
[oracle@localhost ~]$
all fine here.
exec dbms_monitor.database_trace_enable();
and so I got 2 tracefiles:
FREE_ora_70047.trc with the SELECT
=====================
PARSING IN CURSOR #140276220704640 len=48 dep=0 uid=131 oct=3 lid=131 tim=22284540551 hv=1723986746 ad='61d7dbe0' sqlid='c2zwqytmc3wtu'
SELECT id, B LOB_LOCATOR FROM T WHERE ROWNUM = 1
END OF STMT
and FREE_ora_70050.trc with the LOBREAD
LOBREAD: type=PERSISTENT LOB,bytes=8,c=1934,e=1693,p=0,cr=1,cu=0,tim=22284648543
WAIT #0: nam='SQL*Net message from client' ela=3182 driver id=1952673792 #bytes=1 p3=0 obj#=4294967295 tim=22284651786
XCTEND rlbk=0, rd_only=1, tim=22284651919
all the files can be found here. This shows how we can read a lob locators content in another session. That somehow conflicts with the documentation which states
A LOB locator points to the actual LOB contents. The locator is returned when you retrieve the LOB, not the LOB's contents. LOB locators cannot be saved in one transaction or session and used again in a later transaction or session.
Keine Kommentare:
Kommentar veröffentlichen