Orcle 12c 新特性---支持克隆PDB部分表空间

1 说明

从12.1.0.2开始,引入了User Tablespaces,简单的说就是可以按表空间(用户创建的)来克隆PDB。比如,当前PDB1中,用户新建了三个表空间tbs1,tbs2,tbs3,那么我们后期测试,可能只需要tbs1表空间中的数据,那么我们可以用USER_TABLESPACES子句来只克隆PDB1中的tbs1表空间,这样大大的缩短了可怜时间和不必要的空间开销。对于拆分数据也很有用,可以把一个库按照表空间拆分。

USER_TABLESPACES子句支持四种参数:

  1. List one or more tablespaces to include.
  2. Specify ALL, the default, to include all of the tablespaces.
  3. Specify ALL EXCEPT to include all of the tablespaces, except for the tablespaces listed.
  4. Specify NONE to exclude all of the tablespaces.

2 实验

2.1 环境

当前环境有LEI1的PDB

SQL> show pdbs;
    CON_ID CON_NAME	  OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
 3 LEI1   READ WRITE NO

2.2 创建测试数据

– 创建第一个表空间,表,并插入数据

CREATE TABLESPACE ts1
  DATAFILE '/u01/app/oracle/oradata/orcl/lei1/ts101.dbf'
  SIZE 1M AUTOEXTEND ON NEXT 1M;

CREATE USER test1 IDENTIFIED BY test1
  DEFAULT TABLESPACE ts1
  QUOTA UNLIMITED ON ts1;

CREATE TABLE test1.t1 (
  id NUMBER
);

INSERT INTO test1.t1 VALUES (1);
COMMIT;

– 创建第二个表空间,表,并插入数据

CREATE TABLESPACE ts2
  DATAFILE '/u01/app/oracle/oradata/orcl/lei1/ts201.dbf'
  SIZE 1M AUTOEXTEND ON NEXT 1M;

CREATE USER test2 IDENTIFIED BY test2
  DEFAULT TABLESPACE ts2
  QUOTA UNLIMITED ON ts2;

CREATE TABLE test2.t2 (
  id NUMBER
);

INSERT INTO test2.t2 VALUES (1);
COMMIT;

– 创建第三个表空间,表,并插入数据

CREATE TABLESPACE ts3
  DATAFILE '/u01/app/oracle/oradata/orcl/lei1/ts301.dbf'
  SIZE 1M AUTOEXTEND ON NEXT 1M;

CREATE USER test3 IDENTIFIED BY test3
  DEFAULT TABLESPACE ts3
  QUOTA UNLIMITED ON ts3;

CREATE TABLE test3.t3 (
  id NUMBER
);

INSERT INTO test3.t3 VALUES (1);
COMMIT;

2.3 查看创建的表空间

COLUMN owner FORMAT A20
COLUMN table_name FORMAT A20
COLUMN tablespace_name FORMAT A20
SELECT owner, table_name, tablespace_name
FROM   dba_tables
WHERE  table_name IN ('T1','T2','T3')
ORDER BY owner;
OWNER	     TABLE_NAME   TABLESPACE_NAME
-------------------- -------------------- --------------------
TEST1	     T1   TS1
TEST2	     T2   TS2
TEST3	     T3   TS3

2.4 以LEI1克隆pdb3

CREATE PLUGGABLE DATABASE pdb3 FROM lei1
FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/orcl/lei1/','/u01/app/oracle/oradata/orcl/pdb3/')
USER_TABLESPACES=('ts1', 'ts2');
Pluggable database created.

2.4.1 查看表空间

SQL> alter pluggable database pdb3 open;
Pluggable database altered.

SQL> alter session set container=pdb3;
Session altered.

SQL> SELECT tablespace_name from dba_tablespaces;
TABLESPACE_NAME
------------------------------------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
TS1
TS2
TS3
10 rows selected.

2.4.2 访问表空间中表的数据

SQL> SELECT * FROM test1.t1;
ID
----------
 1

SQL> SELECT * FROM test2.t2;
ID
----------
 1

SQL> SELECT * FROM test3.t3;
SELECT * FROM test3.t3
                    *
ERROR at line 1:
ORA-00376: file 63 cannot be read at this time
ORA-01110: data file 63: 'No file with this number, file does not exist'

可以看到tbs3表空间中表无法访问。就是没有被克隆过来。

–那么就可以删除tbs3

DROP TABLESPACE ts3 INCLUDING CONTENTS AND DATAFILES;

DROP USER test3 CASCADE;

同样支持下面的几个语法:

  • 所有用户创建的表空间都不克隆
    USER_TABLESPACES=NONE;
  • 所有表空间都克隆
    USER_TABLESPACES=ALL;
  • 除了ts3之外,其他表空间都克隆
    USER_TABLESPACES=ALL EXCEPT(‘ts3’);

官方文档:http://docs.oracle.com/database/121/ADMIN/cdb_plug.htm#ADMIN14241

猜你喜欢

转载自blog.csdn.net/qianglei6077/article/details/92578786