Is it time to always declare string variables as CLOBs and forget about VARCHAR2?

I received this email from a longtime Oracle Database developer:

Why shouldn't we always use clobs instead of varchar2 in PL/SQL? Memory will be allocated once used so there should not be any overhead always to use clobs instead of a varchar2. There must be a reason, however I'm unable to come up with a explanation. If it is true,  you might say the varchar2 is deprecated, ;-)

I had to laugh a bit when I saw this - not because I thought his question was funny or silly, but because it reminded me of what I sometimes say in my trainings.

I ask students:

Why don't we always declare all string variables as:
my_variable VARCHAR2(32767);
That way, we don't have to worry about a VALUE_ERROR exception if the string gets "too long". And since memory is allocated dynamically (for lengths above 4000), it won't cause excessive memory consumption.

And my answer(s) to this question are:

  1. This declaration - especially appearing all over the place - will raise questions in a developer's mind, including: "Will my strings get that large? Really?" and "What was wrong with the person who wrote this code?" Your code should answer not raise questions.
  2. You are assuming the maximum length of a VARCHAR2 will always be 32767. Maybe someday the maximum length will go up. Maybe someday there will be no difference between VARCHAR2 and CLOBs!

And now a developer wants to know: is there a difference? Maybe it's time to go all-out-CLOBBERS!

Or maybe not. It's true that you can now use many of the typical string functions on CLOBs, such as SUBSTR. You are no longer forced to use DBMS_LOB (though that can still be very helpful). But is there really no difference "under the covers"? Is there no penalty to be paid by switching to CLOBs?

Turns out that, yes, there are still significant reasons to stick with VARCHAR2, including performance and memory utilization. My blog post explores this in more detail.

Nowadays people doesn't matter about data nature. They think only in high levels of information. That's why they got confuse about issues like that.

Like
Reply

I think we should only apply a length constraint when it isl intrinsically part of the domain of the column. We shouldn't even need to limit size when the column is part of an index. This is something I like about the PostgreSQL 'text' type.

LOBs are cool, until you need to do something you can't do with them. I think we'll see VARCHAR2's get closer to CLOBs as the db evolves, so folks can have their cake and eat it too. Mmmmm, cake.

Like
Reply

To view or add a comment, sign in

More articles by Steven Feuerstein

Others also viewed

Explore content categories