Howto: Oracle Vertical Partitioning
What is vertical partitioning?
Vertical partitioning is if you partition (physicaly separate) by columns (not by rows).
How can I implement vertical partitions?
There are basicaly two methods.
Method 1: With a Index
Demo Script:
connect test8/test8
create table t1 (
id0 number,
id1 number,
id2 number,
id3 number,
id4 number
);
insert into t1 select 1,2,3,4,5 from dual connect by level <= 100000;
commit;
explain plan for select id0,id1 from t1;
select * from table(dbms_xplan.display);
create index x1 on t1 (id0,id1);
alter table t1 modify (id0 not null);
alter table t1 modify (id1 not null);
exec DBMS_STATS.GATHER_table_STATS (OWNNAME => 'TEST8', TABNAME => 'T1',cascade=>TRUE );
explain plan for select id0,id1 from t1 t;
select * from table(dbms_xplan.display);
Output:
PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------- Plan hash value: 3617692013 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 95936 | 2435K| 84 (2)| 00:00:02 | | 1 | TABLE ACCESS FULL| T1 | 95936 | 2435K| 84 (2)| 00:00:02 | -------------------------------------------------------------------------- Note ----- - dynamic sampling used for this statement PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------------------------------------------------------- Plan hash value: 3702471258 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 99418 | 582K| 55 (2)| 00:00:01 | | 1 | INDEX FAST FULL SCAN| X1 | 99418 | 582K| 55 (2)| 00:00:01 | -----------------------------------------------------------------------------
Note:
It is essential, that all the columns in the index are not NULL. Since this garanties, that all rows are in the index.
Method 2: Split a table to tow tables with a 1:1 relation ship and access them by view
Demo Script:
create table t1 (
id0 number,
id1 number,
id2 number
);
create table t2 (
id0 number,
id3 number,
id4 number
);
create view v1 as select t1.id0,id1,id2,id4 from t1,t2 where t1.id0=t2.id0;
insert into t1 select rownum,2,3 from dual connect by level <= 100000;
insert into t2 select rownum,4,5 from dual connect by level <= 100000;
commit;
explain plan for select id0,id1 from v1;
select * from table(dbms_xplan.display);
ALTER TABLE t1 add CONSTRAINT p1 PRIMARY KEY (id0);
ALTER TABLE t2 add CONSTRAINT p2 PRIMARY KEY (id0);
exec DBMS_STATS.GATHER_table_STATS (OWNNAME => 'TEST8', TABNAME => 'T1',cascade=>TRUE );
exec DBMS_STATS.GATHER_table_STATS (OWNNAME => 'TEST8', TABNAME => 'T2',cascade=>TRUE );
explain plan for select id0,id1 from t1 t;
select * from table(dbms_xplan.display);
Output:
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2959412835
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 83107 | 3165K| | 387 (2)| 00:00:05 |
|* 1 | HASH JOIN | | 83107 | 3165K| 2544K| 387 (2)| 00:00:05 |
| 2 | TABLE ACCESS FULL| T2 | 104K| 1322K| | 56 (2)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T1 | 83107 | 2110K| | 56 (2)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."ID0"="T2"."ID0")
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3617692013
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100K| 783K| 56 (2)| 00:00:01 |
| 1 | TABLE ACCESS FULL| T1 | 100K| 783K| 56 (2)| 00:00:01 |
--------------------------------------------------------------------------