Oracle Scratchpad

April 16, 2009

Virtual Columns

Filed under: Performance,Statistics,Tuning — Jonathan Lewis @ 7:25 pm UTC Apr 16,2009

A recent post on comp.databases.oracle.server asked:

I have read various articles on virtual columns? I still do not understand their advantages other than they save some disk space, one can put logic of virtual columns in a trigger which will save information in a real column. Real column will need some additional disk space, but it will save some cpu time when one is doing selects on “virtual” columns.

In my opinion it’s the optimizer statistics that are likely to be the most common benefit of virtual columns: If you write the query:

    select * from emp where sal + nvl(comm,0) > 10000

then the selectivity is 5% (under the standard rule for “range based predicate involving unknown values”), and it would be 1% (“column expression with equality) for:

    select * from emp where sal + nvl(comm,0) = 10000

In earlier versions of Oracle you could create a function based index on (sal + nvl(comm,0)) which would give you a hidden column definition and allow you to generate real statistics on the expression – but you have to allow the space for the supporting index when (possibly) all you really want is the set of column statistics. [but see footnote **1]

In 11g if you declare a virtual column, tot_remun say, as (sal + nvl(comm,0)) then you can have the statistics on tot_remun without using up any extra space (beyond the small number of entries in the data dictionary). [see footnote **2]

As 11g gets taken up in more production systems, I expect to see sites solving a number of optimizer problems simply by creating virtual columns on expressions that appear frequently in important queries.

Footnotes:
**1: In fact, you can collect statistics on “virtual columns” pre-11g by creating function-based indexes in an unusable state. The method works, but it’s not really a good strategy.

**2: In fact, by making a call to dbms_stats.create_extended_stats()  you can collect stats on an expression such as (sal + nvl(comm,0)) without creating a virtual column But this will create exactly the same hidden virtual column that you would have had anyway, with the drawback (or possibly benefit, depending on your point of view) that you don’t have a user-friendly column name to reference in your code.

3: [November 2010] I see that Richard Foote has been uncovering some problems with virtual columns. As so often happens with new Oracle features combinations cause problems. In this case it’s materialized views with fast refresh that run into trouble when you start using virtual columns.

4: [April 2011] At Miracle Open World 2011, I discovered in a conversation with Morten Egan of MiracleAS another issue with virtual columns – exp (the old export mechanism) won’t export a table if it’s been defined with a virtual column. Of course, if you’re up to version 11g, you should be using datapump (expdp) rather than the older product, but it’s a trap that’s still going to catch someone out.

5: [June 2011] And Karen Morton has found another nasty problem if you’re on 11.2.0.2

7 Comments »

  1. In addition to the extended statistics, there is the opportunity to partition by it, the opportunity to create foreign keys on them – think about that legacy system with the so called ‘intelligent key’ where the part number is the substr(primary_key,5,10). You can put a foreign key on it.

    Ease of use as well – add the virtual column and you can treat it like any other column – index it, constrain it, select it, gather statistics on it, partition by it… Most everything except.. alter it’s datatype :)

    Comment by Thomas Kyte — April 16, 2009 @ 7:53 pm UTC Apr 16,2009 | Reply

  2. Tom,

    The foreign key constraint is a very nice idea – I remember seeing you describe it on AskTom some time ago.

    As for “changing data type” – no, not quite – but you can certainly create a virtual column of the form to_date(char_col,’yyyymmdd’) to deal with some of the problems of writing reports against “database independent” applications.

    Comment by Jonathan Lewis — April 16, 2009 @ 9:53 pm UTC Apr 16,2009 | Reply

  3. Gents,

    thank you for sharing your time & knowledge !!

    Very simple and explanatory.

    Kind regards,
    Dani

    Comment by Danyc — April 17, 2009 @ 8:17 pm UTC Apr 17,2009 | Reply

  4. [...] to the columns) has been given the fixed “unknown range” selectivity of 5%. (In this case, the virtual column technology introduced in Oracle 11g would have helped correct the cardinality error.) But where [...]

    Pingback by Predicate Problems « Oracle Scratchpad — February 20, 2010 @ 6:53 pm UTC Feb 20,2010 | Reply

  5. [...] you could ask to fill in further details here. For example, if you created a “genuine” virtual column in 11g using one of my “unusable” decode() expressions, and then indexed the virtual [...]

    Pingback by FBI oddities « Oracle Scratchpad — December 16, 2010 @ 6:18 pm UTC Dec 16,2010 | Reply

  6. I’ve just picked up an interesting little detail about virtual columns at the MOW 2011 conference. exp doesn’t like them, and won’t export a table with a virtual column. (Still, for 11g you should be using expdp anyway.)

    Comment by Jonathan Lewis — April 18, 2011 @ 11:54 am UTC Apr 18,2011 | Reply

  7. great article for oracle 11g virtual column support and its discrepancies

    Comment by lkafle — July 1, 2011 @ 4:53 am UTC Jul 1,2011 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 1,392 other followers