Oracle Scratchpad

July 9, 2009

Concatenating LOBs

Filed under: Infrastructure, Performance, Troubleshooting — Jonathan Lewis @ 6:24 pm UTC Jul 9,2009

If you have to handle LOBs, it’s worth checking for “unusual” activity. Here’s an example of unexpected behaviour that I came across a couple of years ago.

The client had a table with data that had to be written to a flat file so that a number of other databases could import it using SQL*Loader. The table definition and the query to dump the data are shown below – note, particularly, the CLOB sitting in the middle of the table:


Name                    Null?    Type
----------------------- -------- ----------------
ID                               NUMBER(10)
VIEW_NAME                        VARCHAR2(32)
USER_ID                          VARCHAR2(16)
XML_FILE                         CLOB
FILE_PATH                        VARCHAR2(127)
ACCOUNT_ID                       VARCHAR2(16)
AREA_ID                          VARCHAR2(16)
LABEL_ID                         VARCHAR2(16)
COMPLIANCY_FLAG                  VARCHAR2(1)

select
	id  		|| chr(27) ||
	view_name	|| chr(27) ||
	user_id		|| chr(27) ||
	xml_file	|| chr(27) ||
	file_path 	|| chr(27) ||
	account_id	|| chr(27) ||
	area_id		|| chr(27) ||
	label_id	|| chr(27) ||
	compliancy_flag
from
	t1
;

The dump code is fairly simple – it just concatenates the columns in order, inserting a separator between them using a value that’s not allowed to appear in the data. (The client’s code was a little more subtle, but essentially the same).

I’ve inserted 10,000 rows into this table, with 500 bytes of data for each LOB, and when I execute a simple; “select dbms_lob.getlength(xml_file) from t1;” it takes 20,653 logical I/Os – 653 for the tablescan and 2 to read each LOB value (since I’ve disabled storage in row).

So why, when I run the dump code, do I see the following stats – the number of buffer visits is obviously a little surprising but notice, in particular the number of “lob writes” and the number of “db block changes”:


session logical reads              1,840,063
db block gets                      1,540,034
consistent gets                      300,029
db block changes                     550,037
consistent changes                   550,015
free buffer requested                119,891
lob reads                             10,000
lob writes                           220,000

Despite all the block changes, by the way, the session generated less than 2KB of redo log – and that’s a bit of a clue to what’s going on.

Here are the results of the same job after I’ve optimised the code in two different ways:


session logical reads                240,006
db block gets                        140,000
consistent gets                      100,006
db block changes                      50,000
consistent changes                    50,000
free buffer requested                 19,910
lob reads                             10,000
lob writes                            20,000

session logical reads                400,006
db block gets                        280,000
consistent gets                      120,006
db block changes                     100,000
consistent changes                   100,000
free buffer requested                 29,907
lob reads                             10,000
lob writes                            40,000

Of course, you might be more interested in the timing rather than the simple counts – the original code used 13.95 CPU seconds, the best version 3.49 CPU seconds, and the third version 4,73 seconds – and it was the third option that we had to implement.

So where does the extra work come from? The answer is in the different versions of the code … the point at which you introduce the LOB into the list of concatenated columns makes a big difference:


select
	id  		|| chr(27) ||
	view_name	|| chr(27) ||
	user_id		|| chr(27) ||
	file_path 	|| chr(27) ||
	account_id	|| chr(27) ||
	area_id		|| chr(27) ||
	label_id	|| chr(27) ||
	compliancy_flag	|| chr(27) ||
	xml_file
from
	t1
;

select
	start_piece || xml_file || end_piece
from
	(
	select
		id  		|| chr(27) ||
		view_name	|| chr(27) ||
		user_id		|| chr(27) 	start_piece,
/*								*/
		chr(27)		||
		file_path 	|| chr(27) ||
		account_id	|| chr(27) ||
		area_id		|| chr(27) ||
		label_id	|| chr(27) ||
		compliancy_flag			end_piece,
/*								*/
		xml_file
	from
		t1
	)
;

The minimum work appeared when the LOB was the last thing added to the concatenation; if the LOB wasn’t at the end of the list then every column (including all those chr(27)’s) resulted in the following extra work for every row selected:

	session logical reads                16
	db block gets                        14
	consistent gets                       2
	db block changes                      5
	consistent changes                    5
	free buffer requested                 1
	lob writes                            2

Essentially, every time you add a piece on the end of the LOB, Oracle creates and updates a new temporary LOB (hence two lob writes). Since they are temporary LOBs – associated therefore with the temporary tablespace – all those “db block changes” don’t cause any redo to be generated, but the overhead is significant.

In fact, it’s quite easy to miss what may be the most significant figure – the “free buffer requested”. If you use code like this to dump a large amount of data you may find that the system starts doing more reads and writes because you keep demanding free buffers.

In the short term, we adopted the second, slightly more expense option – because the process was delivering data to lots of other systems to be loaded by SQL*Loader and the optimum code would have rearranged the column order and forced us to supply a code release to all those remote systems. The slightly more expensive code kept the result looking the same while concatenaring just one extra column afte the LOB.

Conclsion:

Test carefully when you start using code that mixes concatenation and LOBs, and check for any unexpected increases in buffer activity.

10 Comments »

  1. Excellent post.

    Comment by Asif Momen — July 10, 2009 @ 10:04 am UTC Jul 10,2009 | Reply

  2. Jonathan,

    By setting up a test case with 10000 rows and CLOBs of 500, by simply replacing


    xml_file || chr(27) ||

    with


    dbms_lob.substr(XML_FILE, dbms_lob.getlength(XML_FILE)) || chr(27) ||

    I get (CPU time converted to seconds for convenience and clarity):


    NAME                                             DIFF
    ------------------------------------------ ----------
    session logical reads                            1783
    db block gets                                       0
    consistent gets                                  1783
    db block changes                                    0
    consistent changes                                  0
    free buffer requested                               0
    lob reads                                           0
    lob writes                                          0
    CPU used by this session                          .19

    9 rows selected.

    Compare with what I get for the original baseline of the simple dump (comparable with your own

    results)


    NAME                                             DIFF
    ------------------------------------------ ----------
    session logical reads                         1770019
    db block gets                                 1540000
    consistent gets                                230019
    db block changes                               550000
    consistent changes                             550000
    free buffer requested                          110000
    lob reads                                       10000
    lob writes                                     220000
    CPU used by this session                         5.57

    9 rows selected.

    Would this have been viable in your case?

    Feel free to edit if this comes out badly it’s my first attempt to post anything substantial :)

    Chris

    Comment by Chris Poole — July 10, 2009 @ 10:29 am UTC Jul 10,2009 | Reply

  3. Chris,

    Nice idea – but dbms_lob.substr() returns a varchar2, which means the entire concatenated select list is limited to 4,000 bytes.

    If you change the rpad(‘x’,500) to rpad(‘x’,4000), you’ll find that your modified code fails with error:
    ORA-01489: result of string concatenation is too long

    Comment by Jonathan Lewis — July 10, 2009 @ 11:13 am UTC Jul 10,2009 | Reply

    • Absolutely. This is what I was driving it with ‘Would this have been viable?’. I wasn’t sure if your test case was limiting to 500b CLOBs for convenience or was genuinely representative of what they had.

      I knew the problem was because of the underlying CLOB conversions going on that could be avoided by using DBMS_LOB. So my first thought was to write a PL/SQL function to do the work, during the writing of said function I wondered if you had, as had I, merely overlooked something simple.

      Did you therefore discount a tiny PL/SQL function returning a CLOB?

      Using a function that returns a CLOB and assuming the CLOBs are less than 32kb (is this fair?) I can get results that compare favourably with your best case. Of course if you had restrictions on using PL/SQL functions which you haven’t told us or the CLOBs are in fact over 32kb then this is not a fair comparison.

      Comment by Chris Poole — July 10, 2009 @ 1:15 pm UTC Jul 10,2009 | Reply

      • Chris,

        Sorry about the misunderstanding – but at least the dialogue will have been of benefit to some of the other readers.

        I can’t remember all the details now, but I think the mechanism for dumping the data had to be a SQL statement – something to do with a dynamic generated filtering clause, possibly.

        The CLOBs were also pretty open-ended in size – one of the other little jobs I did was work out whether or not to move the LOB segment into a tablespace with a different blocksize.

        Comment by Jonathan Lewis — July 10, 2009 @ 2:19 pm UTC Jul 10,2009 | Reply

  4. Jonathan,

    In case we are using LOBs but without concatenation, can we jump to a conclusion that we have better to put a CLOB at the end of our tables(for insert/update) or, as always in Oracle, “It depends”

    Thanks

    Comment by Mohamed — July 10, 2009 @ 1:21 pm UTC Jul 10,2009 | Reply

    • Mohamed,
      I don’t think there is any general conclusion you can make about position in the table based on this note.

      Depending on the size of your LOBs, and whether you enable storage in rows, you might decide that some LOBs ought to be at the end of the row – but there are several arguments you could make for or against.

      Comment by Jonathan Lewis — July 10, 2009 @ 2:12 pm UTC Jul 10,2009 | Reply

  5. [...] Jonathan Lewis – Concatenating LOBs [...]

    Pingback by Blogroll Report 03/07/2009 – 10/07/2006 « Coskan’s Approach to Oracle — July 10, 2009 @ 6:42 pm UTC Jul 10,2009 | Reply

  6. Hmm. Forcing a “bushy” expression eval when Oracle wants a “left deep” expression eval. I like it. :)

    Comment by Jason Bucata — July 16, 2009 @ 6:59 pm UTC Jul 16,2009 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.