Oracle 数据库是由无数个表空间组成,表空间是由无数个数据文件组成,数据文件存放在磁盘中。

随着时间和业务量的增长,数据文件会不断的增长,默认的数据文件一个为 32G,因此,需要不断的新增数据文件!

那么,问题来了!需要新增很多数据文件怎么办?

以下示例以 LUCIFER 表空间进行演示!默认开启 OMF!

如何开启 OMF 请参考:[Oracle OMF参数
](https://luciferliu.blog.csdn.net/article/details/120199071)

1、新增一个数据文件,小意思,一行命令搞定!

alter tablespace LUCIFER add datafile size 30G autoextend off;

2、新增 10 个数据文件,麻烦点,复制 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 个数据文件,头疼,复制 100 行?那 1000 个呢?10000个呢❓

当然,只是打个比方,无需较真,只是为了说明一个理念!

像这种需要一次性增加多个表空间数据文件的,可以直接通过循环语句,短短几行代码就可以搞定:

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

通过以上短短的代码,就可以实现创建 100 个数据文件,如果需要 10000 个,就把 100 改成 10000 就行了!

如果你说你不使用 OMF 参数,当然可以,稍微改一下就行:

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;
/

只需要将数据文件路径 /oradata/orcl/ 和 数据文件名称 lucifer 拼接以下,然后传入 i 作为编号即可!

文章作者: Lucifer三思而后行 -文章来源:https://www.modb.pro/db/125945

最后修改:2021 年 11 月 14 日
如果觉得我的文章对你有用,请随意赞赏