當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

Oracle 快速創(chuàng)建 N 個表空間數(shù)據(jù)文件
2021-09-13 10:34:49

Oracle 數(shù)據(jù)庫是由無數(shù)個表空間組成,表空間是由無數(shù)個數(shù)據(jù)文件組成,數(shù)據(jù)文件存放在磁盤中。

隨著時間和業(yè)務(wù)量的增長,數(shù)據(jù)文件會不斷的增長,默認(rèn)的數(shù)據(jù)文件一個為 32G,因此,需要不斷的新增數(shù)據(jù)文件!

那么,問題來了!需要新增很多數(shù)據(jù)文件怎么辦?

以下示例以 LUCIFER 表空間進行演示!默認(rèn)開啟 OMF!

?? 如何開啟 OMF 請參考:Oracle OMF參數(shù)

1、新增一個數(shù)據(jù)文件,小意思,一行命令搞定!

alter tablespace LUCIFER add datafile size 30G autoextend off;

2、新增 10 個數(shù)據(jù)文件,麻煩點,復(fù)制 10 行也能搞定!

alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;
alter tablespace LUCIFER add datafile size 30G autoextend off;

3、新增 100 個數(shù)據(jù)文件,頭疼,復(fù)制 100 行?那 1000 個呢?10000個呢?

當(dāng)然,只是打個比方,無需較真,只是為了說明一個理念!

像這種需要一次性增加多個表空間數(shù)據(jù)文件的,可以直接通過循環(huán)語句,短短幾行代碼就可以搞定:

begin
  for i in 1 .. 100 loop
    execute immediate 'alter tablespace LUCIFER add datafile size 30G autoextend off';
  end loop;
end;
/

通過以上短短的代碼,就可以實現(xiàn)創(chuàng)建 100 個數(shù)據(jù)文件,如果需要 10000 個,就把 100 改成 10000 就行了!

如果你說你不使用 OMF 參數(shù),當(dāng)然可以,稍微改一下就行:

begin
  for i in 1 .. 100 loop
    execute immediate 'alter tablespace LUCIFER add datafile ''/oradata/orcl/lucifer'||i||'.dbf'' size 30G autoextend off';
  end loop;
end;
/

只需要將數(shù)據(jù)文件路徑 /oradata/orcl/ 和 數(shù)據(jù)文件名稱 lucifer 拼接以下,然后傳入 i 作為編號即可!

?? 記住,本文講的是一個技巧,也是一個理念,不要鉆牛角尖!


本次分享到此結(jié)束啦~

如果覺得文章對你有幫助,點贊、收藏、關(guān)注、評論,一鍵四連支持,你的支持就是我創(chuàng)作最大的動力。

?? 技術(shù)交流可以 關(guān)注公眾號:Lucifer三思而后行 ??

本文摘自 :https://blog.51cto.com/l

開通會員,享受整站包年服務(wù)立即開通 >