場合:1.適用於OLAP數據倉庫應用 2.數據遷移 比exp/imp速度快,不只遷移數據同時遷移元數據 sql
原理:只導出表空間的元數據(即結構信息),導入目標數據庫,把表空間設置爲只讀,把對應的數據文件複製到目標系統的目標目錄,掛載上導入的表空間,在把表空間設置爲讀寫。 數據庫
官方文檔:Administrator’s Guide-> 8 Managing Tablespace -> Transport Tablespace Between databases 安全
SQL Reference -> CREATE DIRECTORY session
清理環境 併發
drop user tsport cascade; oracle
drop tablespace tsport including contents and datafiles; app
1.初始化實驗環境 ide
sqlplus / as sysdba; 函數
create tablespace tsport datafile '/oracle/oracle/oradata/JCH1/tsport01.dbf' size 20m autoextend off; 性能
create user tsport identified by tsport default tablespace tsport;
grant connect,resource to tsport;
conn tsport/tsport
create table t1 (a int) tablespace tsport;
insert into t1 values (100);
commit;
create index idx_t1 on t1 (a) tablespace tsport;
select index_name,table_name,tablespace_name from user_indexes where table_name='T1';
conn / as sysdba
select tablespace_name,segment_name,segment_type from dba_segments where tablespace_name='TSPORT'; 查看TSPORT表空間有哪些對象
2.檢查TSPORT表空間是否違反約束條件
exec dbms_tts.transport_set_check('TSPORT',true);
例如表在A表空間,索引在B表空間,若是隻傳輸A的話,就會違反自包含約束條件,表上的索引就會失效,傳輸不完整,解決方法:同時傳輸A和B兩個表空間。
select * from transport_set_violations; 查看違反約束列表,沒有記錄爲正常
3.設置TSPORT爲只讀表空間,即把全部數據文件都設置成只讀狀態。
alter tablespace tsport read only;
4.使用exp或expdp的transport_tablespace=y參數導出表空間的元數據 即結構信息
exp -help 打開幫助文檔能夠查看其選項說明
exp userid=\'/ as sysdba\' transport_tablespace=y tablespaces=tsport file=/oracle/oracle/exp_tsport.dmp
使用SYS用戶只導出tsport表空間的元數據(結構信息),而不是真實數據,所以容量比較小
scp exp_tsport.dmp ocm2:~ 傳輸到第二臺機器上
數據泵導出方式選作
【expdp -help
create directory dir_home as '/home/oracle';
grant read,write on directory dir_home to public;
expdp system/oracle directory=dir_home dumpfile=expdp_tsport.dmp transport_tablespaces=tsport transport_full_check=y
】
5.拷貝數據文件(表空間真正的數據)只有數據文件爲只讀狀態才能夠複製,並且不用停庫
scp tsport01.dbf ocm2:/u01/app/oracle/oradata/JCH2/
6.imp或impdp的傳輸表空間導入
JCH2庫的準備工做
create user tsport identified by tsport;
grant connect,resource to tsport;
使用imp導入方式 默認以追加方式插入表空間元數據+數據文件
imp userid=\'/ as sysdba\' file=/oracle/exp_tsport.dmp fromuser=tsport touser=tsport transport_tablespace=y tablespaces=tsport datafiles=/u01/app/oracle/oradata/JCH2/tsport01.dbf
【使用impdp導入方法 選作】
create directory dir_home as '/home/oracle';
grant read,write on directory dir_home to public;
impdp system/oracle directory=dir_home dumpfile=expdp_tsport.dmp remap_schema=(tsport:tsport) 對象從一個schema加載到另外一個schema
transport_datafiles=/u01/app/oracle/oradata/LEO2/tsport01.dbf 導入哪一個數據文件
檢查表空間是否導入成功
col tablespace_name for a15
col segment_name for a15
col segment_type for a15
select tablespace_name,segment_name,segment_type from dba_segments where tablespace_name='TSPORT';
conn tsport/tsport
select * from t1;
7.將JCH1實例和JCH2實例表空間調整爲可讀寫狀態
sqlplus sys/oracle@JCH112 as sysdba
conn / as sysdba
select tablespace_name,status from dba_tablespaces;
alter tablespace tsport read write;
select tablespace_name,status from dba_tablespaces;
sqlplus sys/oracle@JCH111 as sysdba
select tablespace_name,status from dba_tablespaces;
alter tablespace tsport read write;
select tablespace_name,status from dba_tablespaces;
官方文檔:
Administrator’s Guide –> 17 Managing Partitioned Tables and Indexs
Data Warehousing Guide -> 5 Partitioning in Data Warehouses
場合:數據量很大,要求檢索範圍小,效率高
優勢:DBA管理靈活性高,基於分區刪除、插入
缺點:跨分區檢索效率很低,但可建立一個全局索引global來改善性能
全局索引global:默認刪除分區,則全局索引失效,一個分區表只有一個全局索引
本地索引local:一個分區一個索引,有幾個分區就有幾個索引
要求:咱們建立一個分區表,共有4個分區,每一個分區獨立使用一個表空間
使用非標準塊,塊大小16k
1.設置非標準塊
JCH1
alter system set db_16k_cache_size=80M; 設置非標準塊16K緩衝區,用於存放非標準塊
show parameter db_16k_cache_size
做用:用於減小物理I/O讀寫次數,本來讀100次能夠完成的數據,如今讀50次就完成了
2.建立4個表空間,一個分區對應一個表空間
create user jch1 identified by jch1;
grant dba to jch1
drop table leo1.t2_part;
drop index leo1.idx_t2_part;
drop tablespace part1 including contents and datafiles;
drop tablespace part2 including contents and datafiles;
drop tablespace part3 including contents and datafiles;
drop tablespace part4 including contents and datafiles;
create tablespace part1 datafile '/oracle/oracle/oradata/JCH1/disk1/part1_01.dbf' size 50M
extent management local
blocksize 16k;
create tablespace part2 datafile '/oracle/oracle/oradata/JCH1/disk2/part2_01.dbf' size 50M
extent management local blocksize 16k;
create tablespace part3 datafile '/oracle/oracle/oradata/JCH1/disk3/part3_01.dbf' size 50M
extent management local blocksize 16k;
create tablespace part4 datafile '/oracle/oracle/oradata/JCH1/disk4/part4_01.dbf' size 50M
extent management local blocksize 16k;
select * from v$tablespace;
2.構造分區表數據
conn tsport/tsport
drop table t2 purge;
create table t2 (itemid number(10),name varchar2(10),itemdate date);
create index idx_t2 on t2(itemid);
insert into t2 values (1,'apple1',to_date('2000-02-01','yyyy-mm-dd'));
insert into t2 values (2,'apple2',to_date('2000-03-01','yyyy-mm-dd'));
insert into t2 values (3,'apple3',to_date('2002-04-01','yyyy-mm-dd'));
insert into t2 values (4,'apple4',to_date('2002-05-01','yyyy-mm-dd'));
insert into t2 values (5,'apple5',to_date('2002-06-01','yyyy-mm-dd'));
insert into t2 values (6,'apple6',to_date('2010-07-01','yyyy-mm-dd'));
insert into t2 values (7,'apple7',to_date('2010-08-01','yyyy-mm-dd'));
insert into t2 values (8,'apple8',to_date('2012-09-01','yyyy-mm-dd'));
insert into t2 values (9,'apple9',to_date('2012-10-01','yyyy-mm-dd'));
insert into t2 values (10,'apple10',to_date('2013-11-01','yyyy-mm-dd'));
commit;
select * from t2; 插入10條記錄,顯示出來
3.JCH1用戶下建立分區表
conn jch1/jch1
CREATE TABLE t2_part
PARTITION BY RANGE (itemdate)
( PARTITION p1 VALUES LESS THAN (to_date('2002-01-01','yyyy-mm-dd'))
TABLESPACE part1,
PARTITION p2 VALUES LESS THAN (to_date('2010-01-01','yyyy-mm-dd'))
TABLESPACE part2,
PARTITION p3 VALUES LESS THAN (to_date('2012-01-01','yyyy-mm-dd'))
TABLESPACE part3,
PARTITION p4 VALUES LESS THAN (to_date('2013-01-01','yyyy-mm-dd'))
TABLESPACE part4,
PARTITION other VALUES LESS THAN (maxvalue)
TABLESPACE part4)
as select * from tsport.t2;
查看分區表數據
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
select * from jch1.t2_part;
查看某一個分區
select * from jch1.t2_part partition (p1);
4.建立hash全局分區索引
hash分區索引:均勻打散後存放數據,拿一列做hash打散,均勻分佈在4個分區上,每一個分區在不一樣表空間上的記錄數都差很少,併發讀併發寫
官方文檔:SQL Reference -> CREATE TABLE and CREATE INDEX
conn jch1/jch1
注:把全局分區索引(按照name列作hash打散)均勻分紅4份,每份都保存在LEOINDEX表空間裏
create unique index idx_t2_part on t2_part (name,itemid)global partition by hash (name) partitions 4 tablespace JCHINDEX parallel 4;
select index_name,index_type,table_name from user_indexes where table_name='T2_PART';
截斷一個分區,須要保證全局索引可用
1.默認狀況下,增長、刪除分區>全局索引失效
2.truncate 操做會影響全局索引 delete 操做不會影響全局索引
alter table t2_part truncate partition p1 update global indexes;
select * from t2_part partition (p1);
【update global indexes 這個關鍵字能夠在操做以後重建全局索引】
檢查全局索引是否有效
select index_name,status,partitioned from dba_indexes where table_name='T2_PART';
場合:能夠查出誰 何時 使用什麼語句 刪除的表
Oracle 細粒度審計是安全領域的一個分支,它能夠追溯數據庫的歷史操做,從而保證全部的操做都是安全可靠可控,FGA是基於包來實現的
官方文檔:Security Guide -> 12 Configuring and Administering Auditing -> Fine-Grained Auditing
PL/SQL Packages and Types Reference –> 40 DBMS_FGA
要求:使用FGA技術對錶進行審計
1.清理環境
只有管理員才能夠刪除審計
DBMS_FGA.DROP_POLICY(
object_schema VARCHAR2, --用戶名(若是爲空默認當前登錄用戶)
object_name VARCHAR2, --待審計的對象名(表名字)
policy_name VARCHAR2 ); --審計名(必須是惟一值)
execute DBMS_FGA.DROP_POLICY(object_schema=>'leo1',object_name=>'t',policy_name=>'audit_t');
conn jch1/jch1
drop table t purge; 刪除待審計的表
2.建立待審計的T表
conn jch1/jch1
create table t (x number(10),y varchar2(20)); 建立待審計的表t
3.建立審計策略
conn / as sysdba
begin
dbms_fga.add_policy (
object_schema => 'jch1', 審計誰
object_name => 't', 審計誰的表
policy_name => 'audit_t', 審計策略的名字
audit_condition => 'x >= 100', 觸發審計的條件 x>=100
audit_column => 'x', 審計表中的哪一個列‘x,y’
enable => TRUE, 審計馬上生效
statement_types => 'INSERT,UPDATE,DELETE,SELECT'); 觸發審計的語句對這些語句都啓動審計
end;
/
4.查詢確認FGA策略是否生效
col object_schema for a20
col object_name for a15
col policy_name for a13
col enabled for a3
select object_schema,object_name,policy_name,enabled from dba_audit_policies;
5.插入測試記錄符合觸發審計的條件
conn jch1/jch1
insert into t values (10,'first');
insert into t values (100,'leo');
insert into t values (200,'leonarding');
insert into t values (300,'andy');
insert into t values (400,'anlan');
insert into t values (500,'tigerfish');
insert into t values (600,'666666666');
select * from t; 查看審計表的內容
6.查看審計結果,默認會把審計結果放在SYS.FGA_LOG$基表中
注:審計會對INSERT,UPDATE,DELETE,SELECT這四種語句都作檢查,而且記錄誰 操做的哪一個表 執行的什麼語句
col OBJ$SCHEMA for a10;
col OBJ$NAME for a10;
col POLICYNAME for a15;
col LSQLTEXT for a50;
select OBJ$SCHEMA,OBJ$NAME,POLICYNAME,LSQLTEXT from SYS.FGA_LOG$;
set lines 200
col sql_text for a55;
col object_schema for a10;
col object_name for a10;
col policy_name for a15;
select object_schema,object_name,policy_name,sql_text from dba_common_audit_trail;
顯示全部審計結果
場合:監控表中無用索引刪除之
官方文檔:Administrator’s Guide -> 16 Managing Indexes -> Monitoring Index Usage
conn jch1/jch1
drop table t4;
create table t4 as select * from dba_objects;
create index idx_t4 on t4(object_id);
開啓JCH1下idx_t4索引的監控
alter index jch1.idx_t4 monitoring usage;
中止JCH1下idx_t4索引的監控
alter index jch1.idx_t4 nomonitoring usage;
select * from t4 where object_id=5000;
查看v$object_usage視圖得到索引被使用狀況
set linesize 400 設置環境
col index_name for a10
col table_name for a10
col start_monitoring for a20
col end_monitoring for a20
select * from v$object_usage;
說明: monitoring字段爲YES 表示此索引已經被監控,NO未被監控
used字段爲YES 表示此索引已經被使用,NO未被使用
start_monitoring與end_monitoring 表示上次監控區間
建立具備ROWID及時間戳類型字段的表並插入數據
官方文檔:SQL Reference -> 2 Basic Elements of Oracle SQL -> Datatypes ->搜索「ROWID」和「TIMESTAMP」 WITH LOCAL TIME ZONE Datatype
1.建立JCH_R表並初始化數據
conn jch1/jch1
構造數據環境
create table jch_text
(
text1 varchar2(10),
text2 varchar2(10),
text3 date,
text4 varchar2(50)
);
插入含有‘Hmilyjch’關鍵字的記錄
insert into jch_text values ('jch1','name',sysdate,'Hmilyjch');
insert into jch_text values ('jch2','name',sysdate,'HmilyjchHmilyjch');
insert into jch_text values ('jch3','name',sysdate,'HmilyjchHmilyjchHmilyjch');
insert into jch_text values ('jch4','name',sysdate,'HmilyjchHmilyjchHmilyjchHmilyjch');
commit;
select * from jch_text;
create table jch1.jch_r (text rowid,insert_time timestamp with local time zone) tablespace users;
rowid 字段類型
timestamp with local time zone 時間戳和本地時區字段類型
2.向JCH_R表插入記錄
在jch_text表中檢索記錄,若是1條記錄中有包含3個或者以上的「Hmilyjch」關鍵字,就把這條記錄rowid和時間戳插入leonarding_r表
insert into jch1.jch_r (text,insert_time) select rowid,current_timestamp from jch1.jch_text
where length(text4)>=3*10;
注:current_timestamp函數:返回根據時區轉換過的「日期」和「時間」,返回的秒是系統的
sysdate 函數:返回操做系統的日期和時間
length 字符串長度函數:取字段長度
commit;
select * from jch1.jch_r;
drop table jch1.jch_r;
場景:當誤刪除時如何恢復數據
官方文檔:Application Developer’s Guide - Fundamentals -> 10 Developing Flashback Application -> Using Flashback Query (SELECT … AS OF)
1.Flashback Query閃回查詢數據
原理:閃回查詢使用的是undo表空間裏存放的前映像
構造環境
drop table t5 purge;
create table t5 (x int);
insert into t5 values(1);
insert into t5 values(2);
insert into t5 values(3);
commit;
select * from t5;
2.爲構造後續的閃回查詢查詢當前的時間和scn號
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
查詢當前系統時間
select sysdate from dual;
查詢當前系統SCN號
select dbms_flashback.get_system_change_number from dual; 473966
3.刪除數據
delete from t5 where x=1;
commit;
select * from t5;
4.兩種方法建立視圖構造閃回查詢刪除以前的數據
1)第一種方法:使用時間戳來構造閃回查詢視圖
create view v_t5_timestamp as select * from t5
as of timestamp to_timestamp('2014-01-24 03:27:11','yyyy-mm-dd hh24:mi:ss');
2)第二種方法:使用SCN構造閃回查詢視圖
create view v_t5_scn as select * from t5 as of scn 473966;
注:scn 比 timestamp 更精確
查詢視圖閃回內容
select * from v_t5_timestamp;
select * from v_t5_scn;
drop view v_t5_timestamp;
drop view v_t5_scn;
到此,兩種構造視圖的方法都順利的得到了閃回查詢的數據
2.一張表被反覆屢次刪除,要求恢復到指定的數據版本
原理:清空回收站
purge recyclebin;
create table t6 (x int);
insert into t6 values (1);
commit;
select * from t6;
drop table t6;
create table t6 (x int);
insert into t6 values (1);
insert into t6 values (2);
commit;
select * from t6;
drop table t6;
查詢回收站數據字典
SQL> col OBJECT_NAME for a40;
SQL> col ORIGINAL_NAME for a20;
SQL> col TYPE for a10;
SQL> select object_name,original_name,type from recyclebin;
show recyclebin
得到t6表被drop的兩個版本中哪一個是咱們須要恢復的對象,恢復有1條記錄的t6表
select * from "BIN$8LXMoaS95DTgQKjAb5kW+A==$0";
select * from "BIN$8LXMoaS85DTgQKjAb5kW+A==$0";
閃回指定的版本->閃回同時重命名
flashback table "BIN$8LXMoaS95DTgQKjAb5kW+A==$0" to before drop rename to t6_new;
drop table t6_new;