合 如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据
Tags: OracleASHDBA_HIST_ACTIVE_SESS_HISTORY
前言部分
导读和注意事项
各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,\~O(∩_∩)O\~:
① 如何导出ASH数据--利用exp导出基表的数据(重点)
② 12c的expdp参数VIEWS_AS_TABLES选项
③ expdp工具不能导出哪些对象?
本文简介
众所周知,视图只是一个查询数据的窗口,其不存储数据,所以在使用exp等工具导出的时候只能导出其定义,而不能导出视图的查询结果数据。在Oracle 12c中,可以采用expdp中的一个新增参数VIEWS_AS_TABLES来将视图作为表来导出,非常实用,不过对于一些特殊的表仍然不能采用expdp导出,例如SYS和SYSTEM下的一些表,AUD\$表不能使用expdp来导出。
另外,对于一些安全类很高的系统是不允许随意创建表,也不允许使用PLSQL Developer等客户端的工具,那么若是查询DBA_HIST_ACTIVE_SESS_HISTORY等视图的时候就非常不方便了,这个时候我们可以将该视图的内容导出来,然后导入到我们自己的测试库中就可以随意的进行分析了。那么,如何来导出这些数据的内容呢?本文将详细介绍这些内容。
如何导出ash数据?
根据前边的分析,我们知道视图的查询结果数据不能直接导出,那么我们可以导出这个视图的基表数据:
1 2 3 4 5 6 7 8 9 | SELECT D.NAME, D.TYPE, D.REFERENCED_NAME, D.REFERENCED_TYPE FROM DBA_DEPENDENCIES D WHERE D.NAME IN ('DBA_HIST_ACTIVE_SESS_HISTORY', 'DBA_HIST_PLAN_OPERATION_NAME', 'DBA_HIST_PLAN_OPTION_NAME', 'DBA_HIST_SQLCOMMAND_NAME', 'DBA_HIST_TOPLEVELCALL_NAME') AND D.TYPE = 'VIEW' ORDER BY D.NAME, D.REFERENCED_NAME; |
主要涉及的表是图中方框里的去掉X\$表后的7个表,其中最主要的还是WRH\$_ACTIVE_SESSION_HISTORY表,该表是一个分区表,导出的时候可以按照时间进行导出。其它表都是很小的表,可以全量导出。
下面尝试使用exp和expdp来导出。
expdp导出sys用户下的表报错ORA-39165 和ORA-39166
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [oracle@orcltest ~]$ more /tmp/expdp_ash_lhr_01.par query=SYS.WRH$_ACTIVE_SESSION_HISTORY:"WHERE SAMPLE_TIME BETWEEN TO_DATE('2016-12-02 08:30:00', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('2016-12-08 23:38:00', 'YYYY-MM-DD HH24:MI:SS')" [oracle@orcltest ~]$ [oracle@orcltest ~]$ expdp \'/ AS SYSDBA\' directory=DATA_PUMP_DIR tables='SYS.WRH$_ACTIVE_SESSION_HISTORY','SYS.WRM$_SNAPSHOT','SYS.WRH$_EVENT_NAME','SYS.WRH$_SQLCOMMAND_NAME','SYS.WRH$_PLAN_OPERATION_NAME','SYS.WRH$_PLAN_OPTION_NAME','SYS.WRH$_TOPLEVELCALL_NAME' dumpfile=expdp_ash_lhr_01.dmp parfile=/tmp/expdp_ash_lhr_01.par EXCLUDE=STATISTICS VERSION=11.2.0.1 REUSE_DUMPFILES=Y Export: Release 11.2.0.3.0 - Production on Fri Dec 16 16:49:52 2016 Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, Automatic Storage Management, OLAP, Data Mining and Real Application Testing options Starting "SYS"."SYS_EXPORT_TABLE_03": "/******** AS SYSDBA" directory=DATA_PUMP_DIR tables=SYS.WRH$_ACTIVE_SESSION_HISTORY,SYS.WRM$_SNAPSHOT,SYS.WRH$_EVENT_NAME,SYS.WRH$_SQLCOMMAND_NAME,SYS.WRH$_PLAN_OPERATION_NAME,SYS.WRH$_PLAN_OPTION_NAME,SYS.WRH$_TOPLEVELCALL_NAME dumpfile=expdp_ash_lhr_01.dmp parfile=/tmp/expdp_ash_lhr_01.par EXCLUDE=STATISTICS VERSION=11.2.0.1 REUSE_DUMPFILES=Y Estimate in progress using BLOCKS method... Total estimation using BLOCKS method: 0 KB ORA-39166: Object SYS.WRH$_ACTIVE_SESSION_HISTORY was not found. ORA-39166: Object SYS.WRM$_SNAPSHOT was not found. ORA-39166: Object SYS.WRH$_EVENT_NAME was not found. ORA-39166: Object SYS.WRH$_SQLCOMMAND_NAME was not found. ORA-39166: Object SYS.WRH$_PLAN_OPERATION_NAME was not found. ORA-39166: Object SYS.WRH$_PLAN_OPTION_NAME was not found. ORA-39166: Object SYS.WRH$_TOPLEVELCALL_NAME was not found. ORA-31655: no data or metadata objects selected for job Job "SYS"."SYS_EXPORT_TABLE_03" completed with 8 error(s) at 16:49:53 |
查询MOS:
DataPump Export (EXPDP) Fails With Error ORA-39165: Schema SYS Was Not Found (文档 ID 553402.1)
该文章给出了如下答案:
- There is a restriction on dataPump export. It cannot export schemas like SYS, ORDSYS, EXFSYS, MDSYS, DMSYS, CTXSYS, ORDPLUGINS, LBACSYS, XDB, SI_INFORMTN_SCHEMA, DIP, DBSNMP and WMSYS in any mode.
- The Utilities Guide indicates the restriction only on full export mode, but the restriction actually applies to all modes.
而:MOS:Why Can an Object Not Be Exported? Expdp of SYSTEM User's Table Returns ORA-39166 or ORA-31655 (文档 ID 2114233.1)列出来了哪些对象不能导出:
Objects (tables, views, schemas, etc) which fall under either of below conditions are not exported with expdp because they are regarded as system maintained objects.
Object is listed in ku_noexp_view.
This view is a union of ku_noexp_tab and noexp\$ tables.
Objects that are listed in this view are not exported.
Object is ORACLE_MAINTAINED='Y' in ALL_OBJECTS (and DBA_OBJECTS).----针对12c
在视图sys.Ku_Noexp_View中或DBA_OBJECTS的ORACLE_MAINTAINED列为Y的对象不能导出。
SELECT * FROM sys.Ku_Noexp_View d WHERE d.name LIKE '%WRH%' ;
SELECT * FROM DBA_OBJECTS d WHERE d.ORACLE_MAINTAINED='Y' AND D.object_name LIKE 'WR%';
解决该报错的方法是:
1. 使用exp 导出
2.ctas的方法在不受限制的schema下创建表,然后导出该新建的表
3. use the DBMS_AUDIT_MGMT package of Audit Vault to manage and purge audit data (see Note 731908.1). This allows for the facility to move the AUD\$ table out of the SYSTEM tablespace, which can negate the need to export the table.
注意:This issue also applies to other SYS owned auditing tables such as FGA_LOG\$
采用exp导出ASH数据
方法1:ctas建表导出,有的客户不让建表
1 2 3 4 5 6 7 8 9 10 11 | CREATE TABLE ASH_TEMP_20161219 NOLOGGING AS SELECT * FROM DBA_HIST_ACTIVE_SESS_HISTORY D WHERE D.SAMPLE_TIME BETWEEN TO_DATE('2016-12-01 02:00:00', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('2016-12-17 06:00:00', 'YYYY-MM-DD HH24:MI:SS') ; exp \'/ AS SYSDBA\' tables=ASH_TEMP_20161219 file=/tmp/exp_ASH_TEMP_20161219.dmp log=/tmp/ASH_TEMP_20161117.log buffer=41943040 imp lhr/lhr tables=ASH_TEMP_20161219 file=/tmp/ASH_TEMP_20161219.dmp log=/tmp/imp_ASH_TEMP_20161117.log buffer=41943040 |
实验过程: