1. 程式人生 > >Oracle三種table: 堆表Heap Table、索引組織表IOT和聚簇表Cluster

Oracle三種table: 堆表Heap Table、索引組織表IOT和聚簇表Cluster

常用資料庫支援情況:
Oracle支援堆表,索引組織表,聚簇表Cluster;
PostgreSQL只支援堆表,不支援索引組織表;
Innodb只支援索引組織表;
MyISAM只支援堆表。

Oracle使用rowid資料型別儲存行地址,rowid可以分成兩種,分別適於不同的物件,
Physical rowids:儲存ordinary table,clustered table,table partition and subpartition,indexe,index partition and subpartition;
Logical rowids :儲存IOT的行地址

Heap table

:無序的集合儲存。表和主鍵索引分成兩個segment。建立的主鍵索引葉結點儲存ROWID。

插入和更新快,結果無序。

從成本上計算,CBO並不是因為回表動作才確定執行計劃,而是Clustering Factor的影響。IOT一個突出優勢就是直接消滅了Clustering Factor的成本因素。

IOT: 主鍵和表合成一個segment。資料是按主鍵有序的儲存在B樹索引結構中。葉結點儲存了行記錄。查詢快,不回表,結果有序,插入和更新慢。

create table t88(

  ID varchar2 ( 10 ),

  NAME varchar2 ( 20 ),

  constraint pk_id 

primary key ( ID )

  )

organization index

PCTTHRESHOLD 20

overflow tablespace users

INCLUDING name ;

注意兩點:

    ● 建立IOT時,必須要設定主鍵,否則報錯。

    ● 索引組織表實際上將所有資料都放入了索引中。

索引組織表的適用情況:適用於資訊檢索、空間和OLAP程式。
1、 程式碼查詢表。
2、 經常通過主碼訪問的表。
3、 構建自己的索引結構。
4、 加強資料的共同定位,要資料按特定順序物理儲存。
5、 經常用between…and…對主碼或唯一碼進行查詢。資料物理上分類查詢。如一張訂單表,按日期裝載資料,想查單個客戶不同時期的訂貨和統計情況。


經常更新的表當然不適合IOT。 如果不是經常使用主鍵訪問表,就不要使用IOT。

Cluster table:多個表合成一個segment儲存,多個數據表按照連線鍵的順序儲存在一起。經常和另外一個數據表進行連線查詢(Join)顯示。

create cluster emp_dept (deptno number) size 600;
create table emp (empno number, empname varchar2(10), deptno number) cluster emp_dept(deptno);
create table dept (deptno number primary key, deptname varchar2(10)) cluster emp_dept(deptno);
create index idx_emp_dept on cluster emp_dept;
insert into dept select deptno, dname from scott.dept;
insert into emp select empno, ename, deptno from scott.emp;
drop cluster emp_dept including tables;