oracle數(shù)據(jù)庫優(yōu)化實驗手冊

oracle數(shù)據(jù)庫優(yōu)化


實驗一:oracle安裝與環(huán)境準備

一、oracle 11g安裝

操作系統(tǒng):windows XP/windows 7

版本:oracle 11g

http://pan.baidu.com/s/1gd9w4rp

二、oracle 11g環(huán)境配置

將OracleDBConsoleXX,OracleJobSchedulerXX,XXTNSListener和OracleServiceXX設為手動。當需要時,再手動啟用。

創(chuàng)建新用戶,便于實驗的開展。如果是實驗數(shù)據(jù)庫,不影響其他用戶使用,當然也可以使用sys用戶。

sql> create user gmcc identified by gmcc;
sql> grant connect,dba to gmcc;

設置SQL*plus (oracle 11g后autotrace無需安裝)

sql> set autotrace on

三、建立實驗用表

建立一個BIG_TABLE用于整個過程的實驗。表通過all_objects這張表生成。為了在實驗中更好的驗證,建議大家在這個表中的數(shù)據(jù)達到100萬-500萬。數(shù)量級接近我們地市客戶數(shù)量。

create table big_table
as
select rownum id, a.* from all_objects a 
where 1=0;

設置為nologging,用以提高性能。

alter table big_table nologging;

輸入?yún)?shù)1為希望的行數(shù),往表中多次插入數(shù)據(jù), 直到數(shù)據(jù)至少幾百萬行:

declare
    l_cnt number;
    l_rows number:=&1;
begin
    insert /*+ append */ into big_table
    select rownum, a.* from all_objects a 
    where rownum <= l_rows;

    l_cnt :=sql%rowcount;
    commit;

    while(l_cnt < l_rows)
    loop
        insert /*+ append */ into big_table
        select rownum+l_cnt, OWNER, OBJECT_NAME, SUBOBJECT_NAME, 
        OBJECT_ID, DATA_OBJECT_ID, OBJECT_TYPE, CREATED, LAST_DDL_TIME, TIMESTAMP, 
        STATUS, TEMPORARY, GENERATED, SECONDARY, NAMESPACE, EDITION_NAME
        from big_table
        where rownum <= l_rows - l_cnt;
        l_cnt:=l_cnt+sql%rowcount;
        commit;
    end loop;
end;
/

最后增加關(guān)鍵字的約束,同時做一下表的統(tǒng)計。

alter table big_table add constraint
big_table_pk primary key(id);

exec dbms_stats.gather_table_stats(user, 'BIG_TABLE', estimate_percent=>1);

建立一個表common_table,用于后面關(guān)聯(lián)查詢

create table common_table
as
select rownum id, a.* from all_objects a where object_type='TABLE';

alter table common_table add constraint
common_table_pk primary key(id);

exec dbms_stats.gather_table_stats(user, 'COMMON_TABLE', estimate_percent=>1);

到此,課程的準備工作完成!


實驗二:oracle 的結(jié)構(gòu)體系

一、oracle實例與數(shù)據(jù)庫

實例包含進程和內(nèi)存。oracle的進程可以通過v$process這張表進行查詢。而內(nèi)存占用參數(shù)可以查詢memory_target(oracle 11g)或者sga_target和pga_target(oracle 10g)來查看。

select pid, pname, round(pga_used_mem/1024/1024,2)||'MB' 
from v$process;

數(shù)據(jù)庫包括一系列操作系統(tǒng)的文件,其中最重要的是數(shù)據(jù)文件。可以通過查詢v$datafile這張表進行查看。

select name, blocks from v$datafile;

二、pga, sga的查看與配置

pga的狀態(tài),通過查看v$pgastat這張表。在oracle 10g版本下,需要配置參數(shù)pga_target來設置;而在oracle 11g的版本下,只需要配置memory_target這個參數(shù)。

select name||' | '||round(value/1024/1024)||'MB' from v$pgastat;

sga的狀態(tài),通過查看v$sgastat這張表。同樣在oracle 11g下面,通過配置memory_target這個參數(shù)來設置。

select * from v$sgastat;

三、內(nèi)存參數(shù)設置。

內(nèi)存參數(shù)主要通過pga_aggregate_target, sga_target, sga_max_size, memory_target 以及 memory_max_target等參數(shù)。這些參數(shù)都是寫在spfile文件中的,所以參數(shù)的修改也是在spfile中。

SQL> show parameter sga_max_size

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
sga_max_size                         big integer 512M

SQL> alter system set sga_target=600M;
alter system set sga_target=600M
*
第 1 行出現(xiàn)錯誤:
ORA-02097: 無法修改參數(shù), 因為指定的值無效
ORA-00823: 指定的 sga_target 的值大于 sga_max_size

設置sga_target大于sga_max_size出現(xiàn)錯誤,要增大sga_target必須增大sga_max_size
sga_max_size為靜態(tài)的參數(shù),只能修改spfile的值

修改spfile文件中的sga_max_size后,系統(tǒng)運行的sga_max_size仍然為512M,需要重啟數(shù)據(jù)庫,參數(shù)才會修改。

alter system set sga_max_size=600M scope=spfile;

系統(tǒng)已更改。

SQL> show parameter sga_max_size

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
sga_max_size                         big integer 512M

也可以通過查看pfile并修改達到更改參數(shù)的目的

create pfile='e:/newpfile.ora' from spfile;

文件已創(chuàng)建。

create spfile from pfile=‘d:/newpfile.ora’ 

四、oracle的事務處理方式和并發(fā)方式

思考以下問題:

  • 數(shù)據(jù)庫并發(fā)讀寫應當如何實現(xiàn)?實現(xiàn)有什么困難?
  • oracle采取什么方式提高數(shù)據(jù)庫的并發(fā)能力?
  • oracle的行級鎖有什么特點?為什么要采用這種鎖?
  • 多版本控制是什么?與commit和rollback的關(guān)系?
  • redo和undo的作用分別是什么?

查看和修改redo日志文件。

查看日志文件組,如果發(fā)現(xiàn)日志文件過小,可以進行調(diào)整。

select group#, sequence#, members, bytes, status, archived from v$log;

新增文件組4

alter database add logfile 
group 4 (‘e:/oracle/product/10.0.2/oradata/ORCL/redo41.log') size 200m;

切換重做日志

alter system switch logfile;

實驗三:oracle的SQL調(diào)優(yōu)

一、oracle語句的執(zhí)行計劃

設置終端輸出執(zhí)行時間:

set timing on

為了每次執(zhí)行過程的公平,執(zhí)行前都將緩存給清理干凈。

alter system flush buffer_cache;

分別執(zhí)行一下以下兩個過程, 請分別記錄一下執(zhí)行時間:
過程1:

begin
  for i in 1..100000
  loop
      execute immediate 
    'select object_name from big_table where id=:x' using i;
  end loop;
end;
/

過程2:

begin
  for i in 1..100000
  loop
      execute immediate 
    'select object_name from big_table where id='||i ;
  end loop;
end;
/

思考一下問題:

  • 兩個語句的主要作用是什么?
  • 兩個語句的區(qū)別在哪里?為什么執(zhí)行過程時間差別這么大?
  • oracle的SQL調(diào)優(yōu),需不需要理解oracle語句執(zhí)行過程呢?
  • 通過這個案例,在應用服務的開發(fā)中,要避免的陷阱是什么?

我們來看一下一個典型的sql語句的執(zhí)行過程。

set autotrace on

select a.object_name 
from 
big_table a inner join common_table b 
on ( a.object_id=b.object_id)
where a.id=100;
  • 執(zhí)行計劃如何分析?
  • 統(tǒng)計結(jié)果如何分析?
  • 如果上述語句沒有where子句,執(zhí)行結(jié)果又會變成什么?

二、索引及連接方式

通過實驗比較B樹索引與位圖索引的性能差別。

像前面準備的時候一樣,通過all_objects這個視圖來創(chuàng)建兩個表btree_table, bitmap_table,分別用B樹索引和位圖索引來對這兩個表中的OBJECT_TYPE, OWNER, NAMESPACE分別進行索引。

  • 生成btree_table和bitmap_table的語句。
  • 生成btree_table的B樹索引的語句。
  • 生成bitmap_table的位圖索引的語句。
  • 比較當運行
    select * from btree_table where OBJECT_TYPE='TABLE' and OWNER='SYS' and NAMESPACE=1;

    select * from bitmap_table where OBJECT_TYPE='TABLE' and OWNER='SYS' and NAMESPACE=1;
    執(zhí)行的差別。
  • 在什么情況下使用位圖索引呢?

三、oracle語句優(yōu)化

下面那些語句會使用到btree_table表的索引?

語句一:

select * from btree_table where owner||'.'||object_type='SYS.TABLE';

語句二:

select * from btree_table where owner like '%S';

語句三:

select * from btree_table where owner like 'S%';

語句四:

select * from btree_table where object_type like 'TABLE%';

語句五:

select * from btree_table where object_type like '%TABLE';

語句六:

select * from btree_table where namepace-10>0;

語句七:

select * from btree_table where namepace>10;

語句八:

select * from btree_table where namepace<>10;

語句九:

select * from btree_table where namepace>10 or namepace<10;

四、生成awr報告

SQL> @?/RDBMS/ADMIN/awrrpt.sql (注:?是ORACLE_HOME)
輸入 report_type 的值:html     (注:確定報告的格式)
輸入 num_days 的值:1     (注:選擇快照的天數(shù))
輸入 begin_snap 的值:425     (注:起始快照)
輸入 end_snap 的值:427     (注:結(jié)束快照)
輸入 report_name 的值:d:\scmis-awr-2011-10-29.html     (注:報告生成的名稱和位置)

五、生成addm報告

SQL> @?/RDBMS/ADMIN/addmrpt.sql (注:?是ORACLE_HOME)

實驗四:oracle物理存儲優(yōu)化

一、行遷移的產(chǎn)生和清除

建一個表,用來做行遷移的實驗。

create table chain_table(id number, name varchar(4000));

往表中插入2000行數(shù)據(jù)

declare
    i number;
    begin
        for i in 1..2000 
        loop
        insert into chain_table values (i, rpad('abc', 100));
        end loop;
    commit;
end;
/

更新表,使得字符串都變成2000個字符

update chain_table set name=rpad(‘123’, 2000, '*');
commit;

強制回寫到數(shù)據(jù)庫中。并且清除緩存。

alter system checkpoint;
alter system flush buffer_cache;

查詢行鏈接,首次執(zhí)行,運行腳本utlchain.sql生成相關(guān)分析用表。

@?/rdbms/admin/utlchain.sql

開始分析行鏈接情況。

analyze table chain_table list chained rows;
select * from CHAINED_ROWS;

方法一:清除行鏈接, 最簡單的方法就是復制一下表,然后清除數(shù)據(jù),再復制進去。對于沒有外鍵限制的表,可以采取這種方式。

create table chain_table_tmp as select * from chain_table ;
truncate table chain_table
insert into chain_table select * from table_name_tmp

方法二:采取標準的處理方法,使用行移動的方法來解決。

alter table utlrp.sql enable row movement ; 
alter table chain_table shrink space compcat;
alter table chain_table shrink space; 

最后運行命令,重新編譯無效對象。

@?/rdbms/admin/utlrp.sql 

二、多種數(shù)據(jù)庫表的應用

  1. 表壓縮的應用

使用前面準備的bigtable復制一個一樣的表,使用的表壓縮技術(shù),然后進行存儲的比較。

create table bigtable_1 compress as select * from bigtable;

收集表的信息:

sql>execute dbms_stats.gather_table_stats(ownname=>'SYS', tabname=>'BIGTABLE');
sql>execute dbms_stats.gather_table_stats(ownname=>'SYS', tabname=>'BIGTABLE_1');

統(tǒng)計一下空間對比:

select table_name||'|'||blocks||'|'||num_rows 
from dba_tables 
where table_name like ‘BIGTABLE%’;

空間差異是多少?

  1. 聚簇表

構(gòu)建一個索引聚簇表,使用列owner varchar(30)作為聚簇列,復制視圖sys.all_tables和sys.all_views分別建立在這個聚簇上。

create cluster mycluster( owner varchar(30)) size 128;

create table mytables
(  
    owner varchar(30), 
    table_name varchar(100)
)cluster mycluster(owner);

create table myview
(
    owner varchar(30), 
    view_name(100)
)cluster mycluster(owner);

insert into mytables(owner, table_name)
select owner, table_name from sys.all_tables;

insert into myview(owner, view_name)
select owner, view_name from sys.all_views;

為了對比,我們建立兩個沒有通過聚簇關(guān)聯(lián)的表。并且建立索引,我們看看結(jié)果。

create table mytable_1 as select * from mytables;
create table myview_1 as select * from myview;
create index mytable_1_index on mytable_1(owner);
create index myview_1_index on myview_1(owner);

強制oracle寫磁盤,并且清空緩存。

alter system checkpoint;
alter system flush buffer_cache;

對比看看關(guān)聯(lián)結(jié)果,耗費的差異情況。

set autotrace traceonly
select * from myviews a , mytables b where a.owner=b.owner and a.owner=‘HR';
select * from myview_1 a , mytable_1 b where a.owner=b.owner and a.owner =‘HR’;
  1. IOT表

通過all_objects的OBJECT_ID, OBJECT_NAME建立一個IOT表。

create table iot_table
( 
    object_id number,
    object_name varchar(30),
    primary key(object_id)
) organization index;

insert into iot_table select object_id, object_name from all_objects;

另外,構(gòu)造一個一樣的普通表,對比一下看看。

create table short_table
( 
    object_id number,
    object_name varchar(30),
    primary key(object_id)
) as select * from iot_table;

寫磁盤,請緩存,比較一下看看。

alter system checkpoint;
alter system flush buffer_cache;

set autotrace on
select * from iot_table where object_id=23;
select * from short_table where object_id=23;

三、表分區(qū)機制與應用

1. 區(qū)間分區(qū)例子

建立一個表,按時間進行區(qū)間分區(qū)。

create table range_example
(    rang_key_column date NOT NULL,
     data  varchar(20)
)
partition by range(rang_key_column)
(   
    partition part_1 values less than (to_date('2010-01-01', 'yyyy-mm-dd')) compress,
    partition part_2 values less than (to_date('2011-01-01', 'yyyy-mm-dd')) compress,
    partition part_max values less than (maxvalue)
) ;

將最后一個分區(qū)再切分成兩個分區(qū)。

alter table range_example 
split partition part_max at (to_date('2012-01-01', 'yyyy-mm-dd')) 
into ( partition part_3, partition part_max);

2. 散列分區(qū)例子

建立一個表,以散列值進行分區(qū)

Create table hash_example
(    hash_key_column date NOT NULL,
     data  varchar(20)
)  Partition by hash(hash_key_column)
(   partition part_1,
    partition part_2 
); 

增加一個散列分區(qū)。

alter table hash_example add partion part_3;

3. 列表分區(qū)例子

根據(jù)列表內(nèi)容分成多個區(qū)。

create table list_example
(   state_name varchar(32),
    data varchar(20)
) partition by list(state_name)
(  partition part_1 values('guangdong‘, ‘guangxi’),
   partition part_2 values('shanghai'),
   partition part_default values(default)
)
alter table list_example split partition part_default values('beijing') into
(  partition part_3, partition part_default);

4. 間隔分區(qū)例子

create table audit_trail 
( 
    ts  timestamp, 
    data  varchar2(30) 
)partition by range(ts)  
interval(numtoyminterval(1, 'month')) 
store in (users, example) 
(partition p0 values less than(to_date('01-01-1900')));

當插入一行數(shù)據(jù)的時候,系統(tǒng)自動增加一個區(qū)間。

insert into audit_trail(ts, data) 
values ( to_timestamp(’27-feb-2010’, ‘dd-mon-yyyy’), ‘xx’);

5. 引用分區(qū)例子

建立一個分區(qū)父表。

create table orders 
(
    order#  number primary key, 
    order_date date, 
    data varchar2(30) 
) enable row movement partition by range( order_date)
(  
    partition part_2009 values less than(to_date(’01-01-2010’, ‘dd-mm-yyyy’)) ,
     partition part_2010 values less than(to_date(’01-01-2011’, ‘dd-mm-yyyy’))  
);

insert into orders values(1, to_date(’01-jun-2009’, ‘dd-mon-yyyy’), ‘xxx’);
insert into orders values(1, to_date(’01-jun-2010’, ‘dd-mon-yyyy’), ‘xxx’);

建立一個子表的引用分區(qū)。

create table order_line_items
( 
    order# number, 
    line# number, 
    data varchar2(30), 
    constraint c1_pk primary key(order#, line#), 
    Constraint c1_fk_p foreign key(order#) references orders
) 
    enable row movement partition by reference(c1_fk_p) ;

向分區(qū)子表插入數(shù)據(jù)。

insert into order_line_items values(1, 1, ‘yyy’);
insert into order_line_items values(2, 1, ‘yyy’);

引用分區(qū)的便利之處在于:當你將父表中的分區(qū)刪除掉,那么oracle也會自動級聯(lián)傳遞到子表中,同時刪除子表對應的分區(qū)。這樣避免了由于外鍵約束而不能刪除的問題。

alter table orders drop partition part_2009 update global indexes;

6.組合分區(qū)的例子

create table composite_example 
( 
    range_key_column  date,
      hash_key_column    int,
      data  varchar2(20)
) partition by range ( range_key_column)
    subpartition by hash( hash_key_column) subpartitions 2
    (  
        partition part_1  values less than (to_date('01/01/2008', 'dd/mm/yyyy'))
        ( 
            subpartition part_1_sub_1,  
            subpartition part_1_sub_2 
        ),
           partition part_2  values less than (to_date(’01/01/2008’, ‘dd/mm/yyyy’))
        ( 
            subpartition part_2_sub_1,  
            subpartition part_2_sub_2 
        )
    )
)

7.實驗

內(nèi)容:

  • 請將big_table表按照1-4類分區(qū)方法分別進行分區(qū)。寫出分區(qū)的語句,并說明分區(qū)的目的是什么。
  • 根據(jù)引用分區(qū)和組合分區(qū)的作用,將工作中遇到的大表進行分區(qū),寫出分區(qū)的語句,并說明分區(qū)的目的是什么。

四、索引分區(qū)

1. 局部索引分區(qū)

create table partitioned_table
( 
    a int, 
    b int, 
    data char(20)
) partition by range(a)  
(   partition part_1 values less than(2) tablespace p1,
    partition part_2 values less than(3) tablespace p2
);

局部前綴索引

create index local_prefixed on partitioned_table (a, b) local;

局部非前綴索引

create index local_nonprefixed on partitioned_table (b) local;

2. 全局索引分區(qū)

create table partitioned
(
    timestamp date, 
    id int
) partition by range(timestamp)  
(   
    partition part_1 values less than (to_date('01-01-2000', 'dd-mm-yyyy')),
    partition part_2 values less than (to_date('01-01-2001', 'dd-mm-yyyy')),
    partition part_3 values less than (to_date('01-01-2002', 'dd-mm-yyyy')),
    partition part_4 values less than (to_date('01-01-2003', 'dd-mm-yyyy')),
);

create index partitioned_index on partitioned(id) global partiton by range(id)
(   partition part_1 values less than (1000),
    partition part_2 values less than (maxvalue)
);

3. 滑動窗口和索引

建立一個分區(qū)及全局索引,用以做滑動窗口。

create table partitioned
( 
    timestamp date, 
    id int 
)partition by range (timestamp)
( 
    partition fy_2004 values less than (to_date('01-01-2005', 'dd-mm-yyyy')),
    partition fy_2005 values less than (to_date('01-01-2006', 'dd-mm-yyyy'))  
);

create index partitioned_idx_global on partitioned(timestamp) global; 

利用all_object 生成樣例數(shù)據(jù)。

insert into partitioned partition(fy_2004) 
select to_date('31-12-2004','dd-mm-yyyy')-mod(rownum,360), object_id 
from all_objects;

insert into partitioned partition(fy_2005) 
select to_date('31-12-2005','dd-mm-yyyy')-mod(rownum,360), object_id
from all_objects;

建立空表fy_2004,用于存放舊的數(shù)據(jù)。

create table fy_2004 (timestamp date, id int);
create index fy_2004_idx on  fy_2004(timestamp date);

建立表fy_2006,里面存放新的數(shù)據(jù)。下面進行數(shù)據(jù)的交換.

create table fy_2006(timestamp date, id int)
select to_date('31-12-2006', 'dd-mm-yyyy') – mod(rownum, 360), object_id
from all_objects;

create index fy_2006_idx on  fy_2006(timestamp date);

歸檔數(shù)據(jù)并清除舊分區(qū)

alter table partitioned 
exchange partition fy_2004 
with table fy_2004 
including indexes without validation update global indexes;

alter table partitioned drop partiton fy_2004 update global indexes

前面新數(shù)據(jù)已經(jīng)裝載,這里新增空白分區(qū)并交換數(shù)據(jù)和索引。

alter table partitioned 
    add partition fy_2006 less than ( to_date('01-01-2007', 'dd-mm-yyyy'));

alter table partitioned 
    exchange partition fy_2006
    with table fy_2006 
    including indexes without validation
    update global indexes ;

4. 案例題一

表分區(qū)在數(shù)據(jù)倉庫/決策系統(tǒng)中,分區(qū)是一個強大的工具,它一方面使你的數(shù)據(jù)存儲更加方便有效,同時通過合理的查詢使效率提升。在地市的實際工作中,我們常常使用表分區(qū)來解決一些存儲量巨大的歷史數(shù)據(jù)。
我們碰到了一個難題,我們有一個數(shù)據(jù)倉庫,數(shù)據(jù)量非常大,月均數(shù)據(jù)量超過2G。總體數(shù)據(jù)量已超過2T,數(shù)據(jù)量過高造成運行慢,備份能力不足。我們應該如何設計這個系統(tǒng)呢?

  • 地市的社會酬金系統(tǒng),每個月都要存儲計酬的原始數(shù)據(jù),計酬結(jié)果數(shù)據(jù)等信息。
  • 該數(shù)據(jù)庫每月新增的各類數(shù)據(jù)超過2G。并且數(shù)據(jù)要保留超過5年以上。
  • 每月計酬完畢以后,數(shù)據(jù)需要提供給社會渠道前臺系統(tǒng)查詢,了解清楚計酬過程,提升出酬透明度。

問題:

  • 數(shù)據(jù)庫應該怎么進行分區(qū)?
  • 數(shù)據(jù)量大,要保留時間長,在服務器存儲不足的情況下,如何節(jié)約存儲空間?
  • 如果這個數(shù)據(jù)庫是你來規(guī)劃,應該采取什么方式,以及日常應該如何維護?

5. 案例題二

某地市的觸點營銷平臺,每月上架大量的觸點數(shù)據(jù)。因為觸點數(shù)據(jù)使用周期比較長,客戶數(shù)量多,所以數(shù)據(jù)量較大。1月-4月數(shù)據(jù)量已經(jīng)超過上億條。開發(fā)人員按照上架時間對觸點數(shù)據(jù)進行分區(qū),并構(gòu)造全局索引,發(fā)現(xiàn)數(shù)據(jù)批量導入的速度仍然非常慢。

  • 為了導入的速度,他們采取的是先刪除索引,導入數(shù)據(jù),然后重建索引的辦法。
  • 由于表中數(shù)據(jù)量較大,導致凌晨生成全局索引時,耗時時間較長。
  • 歷史數(shù)據(jù)過多,導致表數(shù)據(jù)占用磁盤空間過大。

問題:

  • 數(shù)據(jù)庫數(shù)據(jù)裝載的方式有沒問題?這樣做的好處和壞處在哪里?
  • 觸點數(shù)據(jù)保留價值不高,應該如何進行處理?
  • 如果這個數(shù)據(jù)庫是你來規(guī)劃,應該采取什么方式,以及日常應該如何維護?

實驗五:oracle備份與恢復

一、flashback(誤操作恢復)

  • Flashback是oracle 9i以后引入的查詢,用于在需要時,依賴undo來查詢以前某個版本的數(shù)據(jù)。
  • Oracle 11g提供多種flashback的操作,用于恢復誤操作的數(shù)據(jù)。

flashback的特點:

flashback database 用于恢復數(shù)據(jù)庫到某一個時點
flashback table 用于恢復誤drop的表
flashback query 實現(xiàn)行級恢復
flashback table 恢復表到先前狀態(tài)

檢查是否啟用了flash recovery area

show parameter db_recovery_file

檢查是否啟用了自動歸檔模式

archive log list;

啟用自動歸檔模式

startup mount;
alter database archivelog;
alter database open

啟用flashback_on

alter database flashback on

查看scn與時間對應關(guān)系。

select scn, to_char(time_dp, 'yyyymmdd-hh24:mi:ss') 
from sys.smon_scn_time;

恢復到時間點或者scn

flashback database to 
timestamp to_timestamp('09-10-14 14:37:05′,'yy-mm-dd hh24:mi:ss');

flashback database to scn 1385839

誤刪除了一個表T

drop table gmcc.t;

閃回表T

flashback table gmcc.t to before drop;

誤刪除了一個表中的數(shù)據(jù)

delete from gmcc.t where x>1;
commit;

通過flashback仍然可以查詢之前的版本數(shù)據(jù), 然后通過插入或者覆蓋的方式來恢復表的行數(shù)據(jù)。

select * from gmcc.t 
as of timestamp to_timestamp('20150422 10:55:00', 'yyyymmdd hh24:mi:ss');

flash table

允許行移動

alter table gmcc.t enable row movement;

閃回表數(shù)據(jù)

flashback table gmcc.t 
to timestamp to_timestamp('20150422 10:55:00', 'yyyymmdd hh24:mi:ss');

為了避免恢復錯誤,可以查詢一下操作日志,確認恢復到的時間。

select sql_text from v_$sql 
where sql_text like 'delete%gmcc.t%' 
order by last_active_time asc;

二、Imp/exp(數(shù)據(jù)遷移與恢復)

由于Impdp/expdp是在服務器端的導入導出,所以主要使用的區(qū)別是指明服務端路徑。

create directory dpdata1 as '/home/oracle/dpdata1';
grant read, write on directory dpdata1 to gmcc;

導出的例子

$expdp gmcc/gmcc tables=mytable directory=dpdata1 dumpfile=mytable.dmp  \
job_name=MYTABLE_EXPORT

導入的例子

$impdp gmcc/gmcc tables=mytable directory=dpdata1 dumpfile=mytable.dmp  \
job_name=MYTABLE_EXPORT

三、Rman(標準的備份與恢復)

簡單實用

連接數(shù)據(jù)庫,進入rman

$ rman target / 

查看參數(shù)

RMAN> show all; 

查看備份情況

RMAN> list backup; 

查看數(shù)據(jù)庫的表空間和數(shù)據(jù)文件

RMAN> report schema; 

簡單的全備份,數(shù)據(jù)庫datafile和spfile都會保存

RMAN>  backup database format ‘e:/whole_%d_%U’ 

啟用autobackup,并備份controlfile

RMAN> configure controlfile autobackup on; 
RMAN> backup current controlfile

進入nomount的狀態(tài)后,恢復controlfile

RMAN>startup nomount; 
RMAN>restore controlfile from autobackup; 

進入mount的狀態(tài)后,恢復數(shù)據(jù)庫

RMAN>startup mount; 
RMAN>restore database; 

備份策略

bak_inc0:周日進行數(shù)據(jù)庫的全備份。

run {
        allocate channel ch1 type disk;
        backup as compressed backupset  incremental level 0
        format '/u01/oracle/bk/rmbk/incr0_%d_%U'
        tag 'day_incr0'
        database plus archivelog delete input;
        release channel ch1;
        }

bak_inc2:周一二,四五六,進行數(shù)據(jù)庫的增量備份。

run {
        allocate channel ch1 type disk;
        backup as compressed backupset  incremental level 2
        format '/u01/oracle/bk/rmbk/incr2_%d_%U'
        tag 'day_incr0'
        database plus archivelog delete input;
        release channel ch1;
        }

bak_inc1:周三進行數(shù)據(jù)庫的增量備份。

run {
        allocate channel ch1 type disk;
        backup as compressed backupset  incremental level 1
        format '/u01/oracle/bk/rmbk/incr1_%d_%U'
        tag 'day_incr0'
        database plus archivelog delete input;
        release channel ch1;
        }

腳本通過語句執(zhí)行

rman target / log=/u01/oracle/bk/log/bak_inc0.log \
append cmdfile = /u01/oracle/bk/scripts/bak_inc0.rcv

在linux系統(tǒng),通常是設置crontab來設置定期執(zhí)行,而在windows系統(tǒng)中設置計劃任務來執(zhí)行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容