 |
Donald K. Burleson
Advanced Oracle Tips and Tricks for the Oracle DBA |
Debugging Oracle 11g Applications
by Ben
Prusinski
Oracle 11g has another new type of partitioning called
Virtual Column Based partitioning. Virtual Column based partitioning allows
data partitioning to be based on virtual column as the partitioning key.
Virtual columns are new feature in 11g that allow derivation from a function
or expression evaluation results. As such, values for virtual columns are not
stored within the table, rather the values for the virtual columns are
evaluated on demand when calculated. Indexes and tables can be partitioned as
such on new virtual columns.
For instance, to add a new virtual column to an existing
table, SCOTT.EMP, we would execute the following statement.
SQL> ALTER TABLE EMP ADD (yearly_sal as (sal*comm));
Table altered.
SQL> desc emp
Name Null?
Type
----------------------------------------- --------
---------------------------
EMPNO NOT NULL
NUMBER(4)
ENAME
VARCHAR2(10)
JOB
VARCHAR2(9)
MGR
NUMBER(4)
HIREDATE
DATE
SAL
NUMBER(7,2)
COMM
NUMBER(7,2)
DEPTNO
NUMBER(2)
YEARLY_SAL
NUMBER
SQL> select ename, sal, comm, yearly_sal
2 from emp;
ENAME SAL COMM YEARLY_SAL
---------- ---------- ---------- ----------
SMITH 800
ALLEN 1600 300 480000
WARD
1250 500
625000
JONES 2975
MARTIN 1250 1400 1750000
SQL> CREATE TABLE virtual_emp (
2 empno NUMBER PRIMARY KEY,
3 ename VARCHAR2(20) NOT NULL,
4 social_id NUMBER(9),
5 sal NUMBER(7,2),
6 hrly_pay NUMBER(7,2) GENERATED ALWAYS AS (sal/2000));
Table created.
Figure 1.10: Creating
table with virtual column in Oracle 11g
SQL> CREATE TABLE v_emp
2 (name VARCHAR2(20),
3 hr_rate NUMBER(7,2),
4 income NUMBER(7,2) generated always as (hr_rate*2080)
5 virtual
6 );
Table created.
SQL> create table workers
2 (
3 emp_id number not null,
4 job_no number,
5 fname varchar2(20),
6 mname varchar2(20),
7 lname varchar2(20),
8 salary number,
9 bonus number,
10 position varchar2(50),
11 tcomp as (salary+bonus) virtual
12 )
13 partition by range(tcomp)
14 (partition p_10g values less than (20000),
15 partition p_20g values less than (30000),
16 partition p_50g values less than (60000),
17 partition p_100g values less than (200000),
18 partition p_500g values less than (600000),
19 partition p_more values less than (maxvalue));
Table created.
SQL> insert into workers
2 (emp_id,job_no,fname,mname,lname,salary,bonus,position)
3 values
4 (1,1,'Scott','Smith','Henry',30000,1200,'DBA');
1 row created.
SQL> insert into workers
2 (emp_id,job_no,fname,mname,lname,salary,bonus,position)
3 values
4 (2,2,'Howard','M','Ostro',250000,25000,'Director');
1 row created.
SQL> commit;
Commit complete.
SQL> select fname, lname, salary, bonus, tcomp
2 from workers partition (p_50g);
FNAME LNAME
SALARY BONUS TCOMP
-------------------- -------------------- ----------
---------- ----------
Scott Henry
30000 1200 31200
SQL> select fname, lname, salary, bonus, tcomp
2 from workers partition (p_500g);
FNAME LNAME
SALARY BONUS TCOMP
-------------------- -------------------- ----------
---------- ----------
Howard Ostro
250000 25000 275000
Figure 1.14: Query
virtual column based partition results for Oracle 11g
SQL> explain plan for
2 select fname, lname, salary, bonus, tcomp
3 from workers partition (p_500g);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------Plan
hash
value: 475732617
--------------------------------------------------------------------------------------------
-----
| Id | Operation | Name | Rows |
Bytes | Cost (%CPU)| Time
| Pstart| Pstop |
--------------------------------------------------------------------------------------------
-----
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------|
0 |
SELECT STATEMENT | | 1 | 63 | 3 (0)| 00:00:01
| | |
| 1 | PARTITION RANGE SINGLE|
| 1 | 63 |
3 (0)| 00:00:01 | 5 | 5 |
| 2 | TABLE ACCESS FULL | WORKERS | 1 |
63 | 3 (0)| 00:00:01 | 5
| 5 |
--------------------------------------------------------------------------------------------
-----
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
9 rows selected.

|