1.可恢復的空間分配
## 1.1 了解可恢復的空間分配 ## 一般情況,我們發出一個大型數據庫操作,比如創建大表索引,如果表空間不足,數據庫最終會終止操作。 而可恢復的空間分配功能可以使得這類操作掛起,等待DBA去處理,等成功處理之后大型數據庫操作自動恢復,這樣就避免了這類棘手問題,節省了時間。 ## 1.2 配置可恢復的空間分配 ## 相關參數和用法: ``` --參數resumable_timeout show parameter resumable_timeout --實例級別啟用resumable alter system set resumable_timeout = 7200; --實例級別禁用resumable alter system set resumable_timeout = 0; --賦權resumable權限給某個用戶 grant resumable to jingyu;--會話級別修改
--啟用resumable
alter session enable resumable;
--設定掛起時長
alter session enable resumable timeout 144000;
--設定名稱
alter session enable resumable name 'Create Big Index';
--禁用resumable
alter session disable resumable;
--查詢DBA_RESUMABLE了解掛起語句的詳細信息
set linesize 140
col name for a40
col error_msg for a30
select USER_ID, SESSION_ID, INSTANCE_ID, STATUS, NAME, ERROR_MSG from dba_resumable;
實驗-配置使用當前會話的可恢復的空間分配:
--如果表空間不足,創建表會直接失敗
SQL> show user
USER is "JINGYU"
SQL> create table t_s1 as select * from dba_objects;
create table t_s1 as select * from dba_objects
*
ERROR at line 1:
ORA-01652: unable to extend temp segment by 128 in tablespace DBS_D_JINGYU
--如果啟用session的resumable,再創建表就會掛起操作
SQL> alter session enable resumable timeout 7200;
Session altered.
SQL> create table t_s1 as select * from dba_objects;
...這里長時間掛起
--此時我們從告警日志中可以看到對應的日志信息,操作被掛起:
Wed Jan 13 09:51:55 2016
statement in resumable session 'User JINGYU(99), Session 62, Instance 1' was suspended due to
ORA-01652: unable to extend temp segment by 128 in tablespace DBS_D_JINGYU
--擴展表空間
SQL> alter tablespace DBS_D_JINGYU add datafile size 30M;
Tablespace altered.
--此時告警日志信息可以看到操作繼續執行:
Wed Jan 13 10:01:12 2016
statement in resumable session 'User JINGYU(99), Session 62, Instance 1' was resumed
Completed: alter tablespace DBS_D_JINGYU add datafile size 30M autoextend on
--再去看剛剛掛起的建表語句已經執行成功
SQL> create table t_s1 as select * from dba_objects;
Table created.
--禁用當前會話的resumable
alter session disable resumable;
## 1.3 使用可恢復的空間分配功能 ##
--觸發器基本結構如下,需要根據具體需求來完善
create or replace trigger resumable_notify
after suspend on database
declare
-- variables, if required
begin
-- check DBA_RESUMABLE for user ID, type of
-- object, then send e-mail
dbms_resumable.space_error_info(. . .);
if object_type = 'TABLE' and object_owner = 'HR' then
-- give DBA 2 hours to resolve
dbms_resumable.set_timeout(7200);
utl_mail.send('DBA@company.com',. . .);
else
dbms_resumable.abort(. . .);
end if;
end;
<h1 id="2">2.可移動表空間</h1>
使用Data Pump導出表空間中對象的元數據,將組成表空間的數據文件復制到目的數據庫,然后把表空間的元數據導入到目的數據庫中。
## 2.1 配置可移動表空間 ##
在平台間傳輸數據,源平台和目的平台必須是Oracle支持的平台列表中的成員。
### 2.1.1 確定兼容性需求 ###
Oracle數據庫的功能兼容性由COMPATIBLE初始化參數進行控制,此參數啟用或禁用數據庫中的某些功能。創建可移動表空間集合時,Oracle將確定目標數據庫的最低兼容性級別,並將相應值存儲在可移動數據集的元數據中。從Oracle Database 11g開始,不管對於哪種目標平台,始終可以將表空間移動到擁有相同或更高兼容性級別的另一個數據庫中。
另外,兩個數據庫必須使用相同的字符集。
### 2.1.2 確定字節序要求 ###
--查詢可移動表空間的所有支持平台及其ENDIAN_FORMAT:
SQL> select platform_id, platform_name, endian_format from v$transportable_platform order by 3,1;
PLATFORM_ID PLATFORM_NAME ENDIAN_FORMAT
1 Solaris[tm] OE (32-bit) Big
2 Solaris[tm] OE (64-bit) Big
3 HP-UX (64-bit) Big
4 HP-UX IA (64-bit) Big
6 AIX-Based Systems (64-bit) Big
9 IBM zSeries Based Linux Big
16 Apple Mac OS Big
18 IBM Power Based Linux Big
5 HP Tru64 UNIX Little
7 Microsoft Windows IA (32-bit) Little
8 Microsoft Windows IA (64-bit) Little
10 Linux IA (32-bit) Little
11 Linux IA (64-bit) Little
12 Microsoft Windows x86 64-bit Little
13 Linux x86 64-bit Little
15 HP Open VMS Little
17 Solaris Operating System (x86) Little
19 HP IA Open VMS Little
20 Solaris Operating System (x86-64) Little
21 Apple Mac OS (x86-64) Little
20 rows selected.
--查詢當前平台的字節序格式
select PLATFORM_NAME my_platform, ENDIAN_FORMAT my_endian_format from v$transportable_platform join v$database using(platform_name);
SQL> select PLATFORM_NAME my_platform, ENDIAN_FORMAT my_endian_format
2 from v$transportable_platform
3 join v$database using(platform_name);
MY_PLATFORM MY_ENDIAN_FORMAT
Linux x86 64-bit Little
## 2.2 傳輸表空間 ##
**傳輸表空間核心步驟:**
1)源數據庫上的表空間設為只讀模式;
2)源數據庫上提取表空間元數據;
3)如果源和目的的字節序格式不同,轉換表空間內容;
4)拷貝表空間數據文件以及表空間元數據到目的數據庫;
5)目的數據庫導入表空間元數據;
6)源數據庫和目的數據庫的表空間設置為讀寫模式。
--用戶的默認表空間和默認臨時表空間
select username, DEFAULT_TABLESPACE, TEMPORARY_TABLESPACE from user_users;
--用戶用到的表空間
select tablespace_name from user_tables union
select tablespace_name from user_tab_partitions union
select tablespace_name from user_indexes union
select tablespace_name from user_ind_partitions;
### 2.2.1 使用EM傳輸表空間 ###
web圖形化界面操作,
1.點擊數據移動 -> 傳輸表空間 -> 生成可傳輸的表空間集,然后按提示一步步生成;
2.在目的地數據庫 點擊數據移動 -> 傳輸表空間 -> 集成現有可傳輸表空間集,然后按提示一步步集成在目的數據庫中。
我這里實驗在生成可傳輸的表空間集時報錯:ORA-02097,ORA-19529。
--根據MOS文檔:Received ORA-19529 Attempting To Do A Transportable Tablespace Export Through OEM (文檔 ID 2073402.1)
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-19529: Pattern /home/kqualls/transport/epsdev/o1_mf_ekb_cons_b6qc1hbm_.dbf in initialization parameter db_file_name_convert has an Oracle Managed Files file name.
--原因:
EM Cloud Control 12c does not support conversion of OMF files. Currently only user managed files are supported using the EM feature. There is an enhancement logged, Bug 21233808 - Cloud Control TTS generates invalid db_file_name_convert command ORA-02097
--解決:
Create RMAN Job in EM Cloud Control Job system, enter command line script commands there.
Using the RMAN Job in the EM Job system will allow the process to use the database parameters to convert the datafiles rather than using the EM perl scripts that don't support OMF files.
PS:即使沒有問題,一般生產環境傳輸表空間也不采用EM,因為很多生產環境都沒有安裝EM,或者復雜的網絡環境因素導致不方便使用EM。還是手動使用SQL傳輸表空間更加靠譜。
### 2.2.2 使用SQL傳輸表空間 ###
參考:
- [Oracle使用SQL傳輸表空間](http://www.cnblogs.com/jyzhao/p/5148905.html)
<h1 id="3">3.Oracle段收縮功能</h1>
參考:
- [Oracle段收縮功能](http://www.cnblogs.com/jyzhao/p/5150772.html)
<h1 id="4">4.Oracle數據庫資源管理</h1>
參考:
- [Oracle數據庫資源管理](http://www.cnblogs.com/jyzhao/p/5151168.html)
<h1 id="5">Reference</h1>
- OCP 認證考試指南 (1Z0-053)[M]. 清華大學出版社, 2010.