Oracle數據庫中分區表的操作方法

jopen 10年前發布 | 16K 次閱讀 Oracle 數據庫服務器

一、為什么要做分區表?

當數據量非常大,比如幾百GB或是到T的時候。那查詢的速度可想而知,Oracle提供了對表和索引進行分區的技術,以改善大型應用系統的性能。 

使用分區的優點: 
  ·增強可用性:如果表的某個分區出現故障,表在其他分區的數據仍然可用; 
  ·維護方便:如果表的某個分區出現故障,需要修復數據,只修復該分區即可; 
  ·均衡I/O:可以把不同的分區映射到磁盤以平衡I/O,改善整個系統性能; 
  ·改善查詢性能:對分區對象的查詢可以僅搜索自己關心的分區,提高檢索速度。
  Oracle數據庫提供對表或索引的分區方法有三種: 
  ·范圍分區   ·Hash分區(散列分區)   ·復合分區 

二、下邊分別對三種分區方法作操作

    為了方便,先建立三個表空間

create tablespace test1 datafile 'd:/分區test/test1.dnf' size 50M;
create tablespace test2 datafile 'd:/分區test/test2.dnf' size 50M;
create tablespace test3 datafile 'd:/分區test/test3.dnf' size 50M;

 1.范圍分區

1.1根據序號進行分區建表

SQL> create table fenqutest(
  2  id number,
  3  name varchar2(50)
  4  )
  5  partition by range(id)
  6  (partition part1 values less than(5) tablespace test1,
  7  partition part2 values less than(10) tablespace test2,
  8  partition part3 values less than(maxvalue) tablespace test3);

這是我自己的做的小測試,很簡寫。

那么當表建完了,數據也添加好了,怎么來查看某個數據在哪張表里呢?

很簡單:select * from fenqutest partition(part1);

1.2根據日期進行分區建表

SQL> create table fenqutest(
    id number,
    time_test date,
    name varchar2(50)
    )
    partition by range(time_test)
    (partition part1 values less than(to_date(’2011-02-27’,’yyyy-mm-dd’)) tablespace test1,
    partition part2 values less than(to_date(’2014-02-28’,’yyyy-mm-dd’)) tablespace test2,
    partition part3 values less than(maxvalue) tablespace test3);

當然你也可以根據別的來分區

2.Hash分區(散列分區)

散列分區為通過指定分區編號來均勻分布數據的一種分區類型,因為通過在I/O設備上進行散列分區,使得這些分區大小一致

SQL> create table fenqutest(
    id number,
    time_test date,
    name varchar2(50)
    )
    partition by hash(id)
    (partition part1 tablespace test1,
    partition part2 tablespace test2,
    partition part3 tablespace test3);

3.復合分區

有時候我們需要根據范圍分區后,每個分區內的數據再散列地分布在幾個表空間中,這樣我們就要使用復合分區。復合分區是先使用范圍分區,然后在每個分區內再使用散列分區的一種分區方法

SQL> create table fenqutest(
    id number,
    time_test date,
    name varchar2(50)
    )
    partition by range(time_test) subpartition by hash(id)
    subpartitions 3 store in (test1,test2,test3) 
   (partition part1 values less than(to_date(’2011-02-27’,’yyyy-mm-dd’)) tablespace test1,
    partition part2 values less than(to_date(’2014-02-28’,’yyyy-mm-dd’)) tablespace test2,
    partition part3 values less than(maxvalue) tablespace test3);

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!