一键获取数据库整体信息脚本
将脚本内容放spooldb.sql中,在sqlplus中执行,相关信息会自动生成5个文件,其中addm是最近一小时文件,ash是最近半小时文件,而awr文件是最近一小时和最近7天的两个文件。

SET markup html ON spool ON pre off entmap offset term off
set heading on
set verify off
set feedback offset linesize 2000
set pagesize 30000
set long 999999999
set longchunksize 999999column index_name format a30
column table_name format a30
column num_rows format 999999999
column index_type format a24
column num_rows format 999999999
column status format a8
column clustering_factor format 999999999
column degree format a10
column blevel format 9
column distinct_keys format 9999999999
column leaf_blocks format   9999999
column last_analyzed    format a10
column column_name format a25
column column_position format 9
column temporary format a2
column partitioned format a5
column partitioning_type format a7
column partition_count format 999
column program  format a30
column spid  format a6
column pid  format 99999
column sid  format 99999
column serial# format 99999
column username  format a12
column osuser    format a12
column logon_time format  date
column event    format a32
column JOB_NAME        format a30
column PROGRAM_NAME    format a32
column STATE           format a10
column window_name           format a30
column repeat_interval       format a60
column machine format a30
column program format a30
column osuser format a15
column username format a15
column event format a50
column seconds format a10
column sqltext format a100SET markup html off
column dbid new_value spool_dbid
column inst_num new_value spool_inst_num
select dbid from v$database where rownum = 1;
select instance_number as inst_num from v$instance where rownum = 1;
column spoolfile_name new_value spoolfile
select 'spool_'||(select name from v$database where rownum=1) ||'_'|| (select instance_number from v$instance where rownum=1)||'_'||to_char(sysdate,'yy-mm-dd_hh24.mi') as spoolfile_name from dual;
spool &&spoolfile..htmlSET markup html off
set serveroutput on;
exec dbms_output.enable(9999999999);
exec dbms_output.put_line('<html>');
exec dbms_output.put_line('<style>');
exec dbms_output.put_line('th {font:bold 8pt Arial,Helvetica,Geneva,sans-serif; color:White; background:#0066CC;padding-left:4px; padding-right:4px;padding-bottom:2px}');
exec dbms_output.put_line('</style>');
exec dbms_output.put_line('<head>');
exec dbms_output.put_line('</head>');
exec dbms_output.put_line('<body>');SET markup html onprompt <p>VERSION
select * from v$version;
select * from dba_registry_history;prompt <p>Last startup time, version, and whether RAC
select * from(select name db_name from v$database),(select instance_name from v$instance),(select archiver from v$instance),(select snap_interval awr_interval, retention awr_retention FROM DBA_HIST_WR_CONTROL),(select flashback_on from v$database),(select parallel from v$instance),(select startup_time from v$instance),(select decode(name,null,'NOT ASM','ASM') IS_ASM from (select null name from dual union all (select name IS_ASM from v$datafile where name like '+%' and rownum = 1))),(select max(end_time) rman_lastcompleted from v$rman_status where status = 'COMPLETED' and object_type like 'DB FULL')
;prompt <p>CPU or wait for the longest in 30 minutes
select t.*, s.sid, s.serial#, s.machine, s.program, s.osuserfrom (select c.USERNAME,a.event,to_char(a.cnt) as seconds,a.sql_id--,dbms_lob.substr(b.sql_fulltext,50,1) sqltextfrom (select rownum rn, t.*from (select decode(s.session_state,'WAITING',s.event,'Cpu + Wait For Cpu') Event,s.sql_id,s.user_id,count(*) CNTfrom v$active_session_history swhere sample_time > sysdate - 30 / 1440group by s.user_id,decode(s.session_state,'WAITING',s.event,'Cpu + Wait For Cpu'),s.sql_idorder by CNT desc) twhere rownum < 20) a,v$sqlarea b,dba_users cwhere a.sql_id = b.sql_idand a.user_id = c.user_idorder by CNT desc) t,v$session s
where t.sql_id = s.sql_id(+)
;prompt <p>Recent load status (based on AWR snapshot
select s.snap_date,decode(s.redosize, null, '--shutdown or end--', s.currtime) "TIME",to_char(round(s.seconds/60,2)) "elapse(min)",round(t.db_time / 1000000 / 60, 2) "DB time(min)",s.redosize redo,round(s.redosize / s.seconds, 2) "redo/s",s.logicalreads logical,round(s.logicalreads / s.seconds, 2) "logical/s",physicalreads physical,round(s.physicalreads / s.seconds, 2) "phy/s",s.executes execs,round(s.executes / s.seconds, 2) "execs/s",s.parse,round(s.parse / s.seconds, 2) "parse/s",s.hardparse,round(s.hardparse / s.seconds, 2) "hardparse/s",s.transactions trans,round(s.transactions / s.seconds, 2) "trans/s"from (select curr_redo - last_redo redosize,curr_logicalreads - last_logicalreads logicalreads,curr_physicalreads - last_physicalreads physicalreads,curr_executes - last_executes executes,curr_parse - last_parse parse,curr_hardparse - last_hardparse hardparse,curr_transactions - last_transactions transactions,round(((currtime + 0) - (lasttime + 0)) * 3600 * 24, 0) seconds,to_char(currtime, 'yy/mm/dd') snap_date,to_char(currtime, 'hh24:mi') currtime,currsnap_id endsnap_id,to_char(startup_time, 'yyyy-mm-dd hh24:mi:ss') startup_timefrom (select a.redo last_redo,a.logicalreads last_logicalreads,a.physicalreads last_physicalreads,a.executes last_executes,a.parse last_parse,a.hardparse last_hardparse,a.transactions last_transactions,lead(a.redo, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_redo,lead(a.logicalreads, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_logicalreads,lead(a.physicalreads, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_physicalreads,lead(a.executes, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_executes,lead(a.parse, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_parse,lead(a.hardparse, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_hardparse,lead(a.transactions, 1, null) over(partition by b.startup_time order by b.end_interval_time) curr_transactions,b.end_interval_time lasttime,lead(b.end_interval_time, 1, null) over(partition by b.startup_time order by b.end_interval_time) currtime,lead(b.snap_id, 1, null) over(partition by b.startup_time order by b.end_interval_time) currsnap_id,b.startup_timefrom (select snap_id,dbid,instance_number,sum(decode(stat_name, 'redo size', value, 0)) redo,sum(decode(stat_name,'session logical reads',value,0)) logicalreads,sum(decode(stat_name,'physical reads',value,0)) physicalreads,sum(decode(stat_name, 'execute count', value, 0)) executes,sum(decode(stat_name,'parse count (total)',value,0)) parse,sum(decode(stat_name,'parse count (hard)',value,0)) hardparse,sum(decode(stat_name,'user rollbacks',value,'user commits',value,0)) transactionsfrom dba_hist_sysstatwhere stat_name in('redo size','session logical reads','physical reads','execute count','user rollbacks','user commits','parse count (hard)','parse count (total)')group by snap_id, dbid, instance_number) a,dba_hist_snapshot bwhere a.snap_id = b.snap_idand a.dbid = b.dbidand a.instance_number = b.instance_numberand a.dbid = &&spool_dbidand a.instance_number = &&spool_inst_numorder by end_interval_time)) s,(select lead(a.value, 1, null) over(partition by b.startup_time order by b.end_interval_time) - a.value db_time,lead(b.snap_id, 1, null) over(partition by b.startup_time order by b.end_interval_time) endsnap_idfrom dba_hist_sys_time_model a, dba_hist_snapshot bwhere a.snap_id = b.snap_idand a.dbid = b.dbidand a.instance_number = b.instance_numberand a.stat_name = 'DB time'and a.dbid = &&spool_dbidand a.instance_number = &&spool_inst_num) twhere s.endsnap_id = t.endsnap_id
order by s.snap_date desc ,time asc
;prompt <p>逻辑读最多
select *from (select sql_id,s.EXECUTIONS,s.LAST_LOAD_TIME,s.FIRST_LOAD_TIME,s.DISK_READS,s.BUFFER_GETSfrom v$sql swhere s.buffer_gets > 300order by buffer_gets desc)
where rownum <= 10
;prompt <p>Logical read most
select * from (select sql_id,s.EXECUTIONS,s.LAST_LOAD_TIME,s.FIRST_LOAD_TIME,s.DISK_READS,s.BUFFER_GETS,s.PARSE_CALLSfrom v$sql s
where s.disk_reads > 300
order by disk_reads desc)
where rownum<=10
;prompt <p>Maximum number of executions
select *from (select sql_id,s.EXECUTIONS,s.LAST_LOAD_TIME,s.FIRST_LOAD_TIME,s.DISK_READS,s.BUFFER_GETS,s.PARSE_CALLSfrom v$sql sorder by s.EXECUTIONS desc)
where rownum <= 10
;prompt <p>SQL parsing most
select *from (select sql_id,s.EXECUTIONS,s.LAST_LOAD_TIME,s.FIRST_LOAD_TIME,s.DISK_READS,s.BUFFER_GETS,s.PARSE_CALLSfrom v$sql sorder by s.PARSE_CALLS desc)
where rownum <= 10
;prompt <p>The disk is sorted most
select sess.username, sql.address, sort1.blocksfrom v$session sess, v$sqlarea sql, v$sort_usage sort1
where sess.serial# = sort1.session_numand sort1.sqladdr = sql.addressand sort1.sqlhash = sql.hash_valueand sort1.blocks > 200
order by sort1.blocks desc
;prompt <p>Commits more than 10,000 session
select t1.sid, t1.value, t2.namefrom v$sesstat t1, v$statname t2where t2.name like '%user commits%'and t1.STATISTIC# = t2.STATISTIC#and value >= 10000order by value desc
;prompt <p>SQL with a length of more than 100
SELECT SQL_ID, COUNT(*) line_countFROM V$SQLTEXTGROUP BY SQL_ID
HAVING COUNT(*) >= 100ORDER BY COUNT(*) DESC
;prompt <p>Query shared memory share
select count(*),round(sum(sharable_mem)/1024/1024,2)from v$db_object_cache a
;prompt <p>Table with parallelism
select t.owner, t.table_name, degreefrom dba_tables t
where  trim(t.degree) <>'1'and trim(t.degree)<>'0'and owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and owner not like 'FLOWS%'and owner not like 'WK%'
;prompt <p>Index with parallelism
select t.owner, t.table_name, index_name, degree, statusfrom dba_indexes t
where  trim(t.degree) <>'1'and trim(t.degree)<>'0'and owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and owner not like 'FLOWS%'and owner not like 'WK%'
;prompt <p>Invalid index
select t.index_name,t.table_name,blevel,t.num_rows,t.leaf_blocks,t.distinct_keysfrom dba_indexes twhere status = 'UNUSABLE'and  table_owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  table_owner not like 'FLOWS%'
;
select t2.owner,t1.blevel,t1.leaf_blocks,t1.INDEX_NAME,t2.table_name,t1.PARTITION_NAME,t1.STATUSfrom dba_ind_partitions t1, dba_indexes t2
where t1.index_name = t2.index_nameand t1.STATUS = 'UNUSABLE'
;prompt <p>Invalid object
select t.owner,t.object_type,t.object_namefrom dba_objects twhere STATUS='INVALID'
order by 1, 2
;prompt <p>Bitmap index and function index, reverse key index
select t.owner,t.table_name,t.index_name,t.index_type,t.status,t.blevel,t.leaf_blocksfrom dba_indexes twhere index_type in ('BITMAP', 'FUNCTION-BASED NORMAL', 'NORMAL/REV')and owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and owner not like 'FLOWS%'and owner not like 'WK%'
;prompt <p>The combination of index columns is more than four
select table_owner,table_name, index_name, count(*)from dba_ind_columns
where  table_owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and table_owner not like 'FLOWS%'and table_owner not like 'WK%'group by table_owner,table_name, index_name
having count(*) >= 4order by count(*) desc
;prompt <p>Indexed more than 5 numbers
select owner,table_name, count(*) cntfrom dba_indexes
where  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and owner not like 'FLOWS%'and owner not like 'WK%'group by owner,table_name
having count(*) >= 5
order by cnt desc
;prompt <p>Never indexed a large table
select segment_name,bytes / 1024 / 1024 / 1024 "GB",blocks,tablespace_namefrom dba_segmentswhere segment_type = 'TABLE'and segment_name not in (select table_name from dba_indexes)and bytes / 1024 / 1024 / 1024 >= 0.5order by GB desc
;
select segment_name, sum(bytes) / 1024 / 1024 / 1024 "GB", sum(blocks)from dba_segmentswhere segment_type = 'TABLE PARTITION'and segment_name not in (select table_name from dba_indexes)group by segment_name
having sum(bytes) / 1024 / 1024 / 1024 >= 0.5order by GB desc
;prompt <p>There is a cross between the combined index of the table and the single column indexselect table_name, trunc(count(distinct(column_name)) / count(*),2) cross_idx_ratefrom dba_ind_columnswhere table_owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and table_owner not like 'FLOWS%'and table_owner not like 'WK%'group by table_name
having count(distinct(column_name)) / count(*) < 1
order by cross_idx_rate desc
;prompt <p>Object is built on the system tablespace
select * from (
select owner, segment_name, tablespace_name, count(*) numfrom dba_segmentswhere tablespace_name in('SYSTEM','SYSAUX')
group by owner, segment_name, tablespace_name)
where  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB','ORDSYS','DBSNMP','OUTLN','TSMSYS')and owner not like 'FLOWS%'and owner not like 'WK%'
;prompt <p>Check whether the statistics are collected
select t.job_name,t.program_name,t.state,t.enabledfrom dba_scheduler_jobs t
where job_name = 'GATHER_STATS_JOB'
;
select client_name,statusfrom dba_autotask_client
;
select window_next_time,autotask_statusfrom DBA_AUTOTASK_WINDOW_CLIENTS
;prompt <p>Check the object that was not collected or collected for a long time
select owner, count(*)from dba_tab_statistics t
where (t.last_analyzed is null or t.last_analyzed < sysdate - 100)and table_name not like 'BIN$%'
group by owner
order by owner
;prompt <p>Temporary table for collecting statistical information
select owner, table_name, t.last_analyzed, t.num_rows, t.blocksfrom dba_tables t
where t.temporary = 'Y'and last_analyzed is not null
;prompt <p>Log switching frequency analysis
select *from (select thread#, sequence#, to_char(first_time, 'MM/DD/RR HH24:MI:SS')from v$log_historyorder by first_time desc)where rownum <= 50
;prompt <p>The amount of log switch every day in the last 10 days
SELECT SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'),1,5) Day,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'00',1,0)) H00,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'01',1,0)) H01,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'02',1,0)) H02,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'03',1,0)) H03,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'04',1,0)) H04,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'05',1,0)) H05,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'06',1,0)) H06,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'07',1,0)) H07,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'08',1,0)) H08,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'09',1,0)) H09,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'10',1,0)) H10,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'11',1,0)) H11,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'12',1,0)) H12,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'13',1,0)) H13,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'14',1,0)) H14,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'15',1,0)) H15,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'16',1,0)) H16,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'17',1,0)) H17,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'18',1,0)) H18,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'19',1,0)) H19,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'20',1,0)) H20,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'21',1,0)) H21,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'22',1,0)) H22 ,SUM(DECODE(SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH24:MI:SS'),10,2),'23',1,0)) H23,COUNT(*) TOTAL
FROM v$log_history  awhere first_time>=to_char(sysdate-11)
GROUP BY SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'),1,5)
ORDER BY SUBSTR(TO_CHAR(first_time, 'MM/DD/RR HH:MI:SS'),1,5) DESC
;prompt <p>Log group size
select group#,bytes,statusfrom v$log
;prompt <p>show recovery_file_dest usageselect substr(name, 1, 30) name,space_limit as quota,space_used as used,space_reclaimable as reclaimable,number_of_files as filesfrom v$recovery_file_dest
;
select * from V$FLASH_RECOVERY_AREA_USAGE
;prompt <p>Check if the sequence is less than 20
select sequence_owner,count(*) CNT,sum(case when t.cache_size <= 20 then 1 else 0 end ) CNT_LESS_20,sum(case when t.cache_size > 20 then 1 else 0 end ) CNT_MORE_20from dba_sequences tgroup by sequence_owner
;prompt <p>Table space usage
set markup html off
prompt <p>
declaretype NUMBER_ARRAY is table of number(15) index by varchar2(30);ts_free_mb NUMBER_ARRAY;cursor c1 isselect tablespace_name, sum(free_mb) + sum(expired_mb) free_mbfrom (SELECT tablespace_name,round(sum(nvl(bytes, 0)) / 1024 / 1024, 2) free_mb,0 expired_mbFROM dba_free_spaceGROUP BY tablespace_nameunion allselect tablespace_name,0 free_mb,round(sum(nvl(bytes, 0)) / 1024 / 1024, 2) expired_mbfrom dba_undo_extents dwhere tablespace_name =(select valuefrom v$parameterwhere name = 'undo_tablespace')and status = 'EXPIRED'group by tablespace_name)group by tablespace_name;cursor c2 isSELECT /*+ rule */tablespace_name, round(sum(bytes) / 1024 / 1024, 2) total_mbFROM dba_data_fileswhere tablespace_name not in(select tablespace_namefrom dba_data_fileswhere upper(AUTOEXTENSIBLE) = 'YES')GROUP BY tablespace_name;ts_name  varchar2(30);ts_total number(15);ts_used  number(15);ts_free  number(15);ts_rate  varchar2(5);
beginfor rec1 in c1 loopts_free_mb(rec1.tablespace_name) := rec1.free_mb;end loop;dbms_output.put_line('<table border="1" width="90%" align="center" summary="Script output">');dbms_output.put_line('<tr><th>ts_name</th><th>ts_total</th><th>ts_used</th><th>ts_free</th><th>ts_rate</th></tr>');for rec2 in c2 loopts_name  := null;ts_total := null;ts_used  := null;ts_free  := null;ts_rate  := null;ts_name  := rec2.tablespace_name;ts_total := rec2.total_mb;ts_free  := nvl(ts_free_mb(ts_name), 0);ts_used  := nvl(ts_total - ts_free, 0);ts_rate  := to_char(round((ts_total - ts_free) / ts_total * 100, 2),'fm990.99');dbms_output.put_line('<tr><td>' || ts_name || '</td><td>' || ts_total ||'</td><td>' || ts_used || '</td><td>' || ts_free ||'</td><td>' || ts_rate || '</td></tr>');end loop;dbms_output.put_line('</table>');
end;
/
prompt <p>
set markup html onprompt <p>The size of the entire database
select owner, round(sum(bytes) / 1024 / 1024 / 1024, 2) "GB"from dba_segmentsgroup by ownerorder by 2 desc
;prompt <p>Object size TOP10
select *from (select owner,segment_name,segment_type,round(sum(bytes) / 1024 / 1024) object_sizefrom DBA_segmentsgroup by owner, segment_name, segment_typeorder by object_size desc)
where rownum <= 10
;prompt <p>Recycle Bin situation (size and quantity)
select *from (select SUM(BYTES) / 1024 / 1024 / 1024 as recyb_sizefrom DBA_SEGMENTSWHERE SEGMENT_NAME LIKE 'BIN$%') a,(select count(*) as recyb_cnt from dba_recyclebin)
;prompt <p>Check who took up the undo tablespaceSELECT r.name "roll_segment_name", rssize/1024/1024/1024 "RSSize(G)",s.sid,s.serial#,s.username,s.status,s.sql_hash_value,s.SQL_ADDRESS,s.MACHINE,s.MODULE,substr(s.program, 1, 78) program,r.usn,hwmsize/1024/1024/1024, shrinks ,xacts
FROM sys.v_$session s,sys.v_$transaction t,sys.v_$rollname r, v$rollstat rs
WHERE t.addr = s.taddr and t.xidusn = r.usn and r.usn=rs.USN
Order by rssize desc
;prompt <p>Check who took up the temp tablespaceselect sql.sql_id,t.Blocks * 16 / 1024 / 1024,s.USERNAME,s.SCHEMANAME,t.tablespace,t.segtype,t.extents,s.PROGRAM,s.OSUSER,s.TERMINAL,s.sid,s.SERIAL#from v$sort_usage t, v$session s , v$sql sql
where t.SESSION_ADDR = s.SADDRand t.SQLADDR=sql.ADDRESSand t.SQLHASH=sql.HASH_VALUE
;prompt <p>Observe the rollback segment, the temporary segment and the general segment No is automatically expanded
select t2.contents, t1.*from (select file_name, tablespace_name, bytes, maxbytes, autoextensiblefrom dba_temp_filesunion allselect file_name, tablespace_name, bytes, maxbytes, autoextensiblefrom dba_data_files) t1,dba_tablespaces t2where t1.tablespace_name = t2.tablespace_name
;prompt <p>Table size of more than 10GB did not build the partition table
select owner,segment_name,segment_type,round(sum(bytes) / 1024 / 1024 / 1024,2) object_sizefrom dba_segments
where segment_type = 'TABLE'and bytes > 10*1024*1024*1024
group by owner, segment_name, segment_type
order by object_size desc
;prompt <p>The top 10 partitions are the most
select *from (select table_owner, table_name, count(*) cntfrom dba_tab_partitionsgroup by table_owner, table_nameorder by cnt desc)
where rownum <= 10
;prompt <p>Partitioned uneven table
select *from (select table_owner,table_name,max(num_rows) max_num_rows,trunc(avg(num_rows), 0) avg_num_rows,sum(num_rows) sum_num_rows,casewhen sum(num_rows) = 0 then0elsetrunc(max(num_rows) / trunc(avg(num_rows), 0), 2)end rate,count(*) part_countfrom dba_tab_partitionsgroup by table_owner, table_name)where rate > 5;prompt <p>A table with a column larger than 100 or less than 2
select *from (select owner, table_name, count(*) col_countfrom dba_tab_colsgroup by owner, table_name)where col_count > 100or col_count <= 2and  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  owner not like 'FLOWS%'
;prompt <p>The table property is NOLOGGING
select owner, table_name, tablespace_name, loggingfrom dba_tableswhere logging = 'NO'and  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  owner not like 'FLOWS%';prompt <p>Table properties, containing COMPRESSION
select owner, table_name, tablespace_name, COMPRESSIONfrom dba_tableswhere COMPRESSION = 'ENABLED'and  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  owner not like 'FLOWS%'
;prompt <p>The index property contains COMPRESSION
select owner, index_name, table_name, COMPRESSIONfrom dba_indexeswhere COMPRESSION = 'ENABLED'and  owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  owner not like 'FLOWS%'
;prompt <p>Trigger
select OWNER, TRIGGER_NAME, TABLE_NAME, STATUSfrom dba_triggerswhere owner not in ('SYSTEM','SYSMAN','SYS','CTXSYS','MDSYS','OLAPSYS','WMSYS','EXFSYS','LBACSYS','WKSYS','XDB')and  owner not like 'FLOWS%';prompt <p>The foreign key is not indexed
select *from (select pk.owner PK_OWNER,pk.constraint_name PK_NAME,pk.table_name PK_TABLE_NAME,fk.owner FK_OWNER,fk.constraint_name FK_NAME,fk.table_name FK_TABLE_NAME,fk.delete_rule FK_DELETE_RULE,ind_col.INDEX_NAME FK_INDEX_NAME,ind.index_type FK_INDEX_TYPE,con_col.COLUMN_NAME FK_INDEX_COLUMN_NAME,con_col.POSITION FK_INDEX_COLUMN_POSITION,decode(ind_col.INDEX_NAME,NULL,NULL,NVL(x_ind.status, 'VALID')) IS_IND_VALID,decode(ind_col.INDEX_NAME,NULL,NULL,NVL(x_ind_part.status, 'VALID')) IS_IND_PART_VALIDfrom (select * from dba_constraints where constraint_type = 'R') fk,(select * from dba_constraints where constraint_type = 'P') pk,dba_cons_columns con_col,dba_ind_columns ind_col,dba_indexes ind,(select index_owner, index_name, statusfrom dba_ind_partitionswhere status <> 'VALID') x_ind_part,(select owner as index_owner, index_name, statusfrom dba_indexeswhere status <> 'VALID') x_indwhere fk.r_constraint_name = pk.constraint_nameand pk.owner = fk.ownerand fk.owner = con_col.ownerand fk.table_name = con_col.table_nameand fk.constraint_name = con_col.CONSTRAINT_NAMEand con_col.owner = ind_col.TABLE_OWNER(+)and con_col.TABLE_NAME = ind_col.TABLE_NAME(+)and con_col.COLUMN_NAME = ind_col.COLUMN_NAME(+)and ind_col.INDEX_OWNER = ind.owner(+)and ind_col.INDEX_NAME = ind.index_name(+)and ind_col.INDEX_OWNER = x_ind.index_owner(+)and ind_col.INDEX_NAME = x_ind.index_name(+)and ind_col.INDEX_OWNER = x_ind_part.index_owner(+)and ind_col.INDEX_NAME = x_ind_part.index_name(+))where FK_INDEX_NAME is nullorder by FK_OWNER ASC
;/* Bad performance
prompt <p>Hot block (summary)
SELECT *+ rule *e.owner, e.segment_name, e.segment_type, sum(b.tch) tchFROM dba_extents e,(SELECT *FROM (SELECT addr, ts#, file#, dbarfil, dbablk, tchFROM x$bhORDER BY tch DESC)WHERE ROWNUM <= 10) bWHERE e.relative_fno = b.dbarfilAND e.block_id <= b.dbablkAND e.block_id + e.blocks > b.dbablkgroup by e.owner, e.segment_name, e.segment_type
order by tch desc
;
prompt <p>Hot block (unfolded, not summary)
SELECT *+ rule *distinct e.owner, e.segment_name, e.segment_type, dbablk,b.tchFROM dba_extents e,(SELECT *FROM (SELECT addr, ts#, file#, dbarfil, dbablk, tchFROM x$bhORDER BY tch DESC)WHERE ROWNUM <= 10) bWHERE e.relative_fno = b.dbarfilAND e.block_id <= b.dbablkAND e.block_id + e.blocks > b.dbablk
order by tch desc
;
*/prompt <p>Appendix: check the parameter settings of session_cached_cursors and increase the parameter value if the usage rate is 100%
SELECT 'session_cached_cursors' PARAMETER,  LPAD(VALUE, 5) VALUE,  DECODE(VALUE, 0, ' n/a', TO_CHAR(100 * USED / VALUE, '990') || '%') USAGE  FROM (SELECT MAX(S.VALUE) USED  FROM V$STATNAME N, V$SESSTAT S  WHERE N.NAME = 'session cursor cache count'  AND S.STATISTIC# = N.STATISTIC#),  (SELECT VALUE FROM V$PARAMETER WHERE NAME = 'session_cached_cursors')  UNION ALL
SELECT 'open_cursors',  LPAD(VALUE, 5),  TO_CHAR(100 * USED / VALUE, '990') || '%'  FROM (SELECT MAX(SUM(S.VALUE)) USED  FROM V$STATNAME N, V$SESSTAT S  WHERE N.NAME IN  ('opened cursors current', 'session cursor cache count')  AND S.STATISTIC# = N.STATISTIC#  GROUP BY S.SID),  (SELECT VALUE FROM V$PARAMETER WHERE NAME = 'open_cursors');  prompt <p>Appendix: all parameters of Oracle for reference
show parameterset markup html off
exec dbms_output.put_line('</body>');
exec dbms_output.put_line('</html>');spool off/* Obtain awr、addm、ash */
--HTML tags are not used below
SET markup html off spool ON pre off entmap offset trim on
set trimspool on
set heading off--select dbid、instance_number
column dbid new_value awr_dbid
column instance_number new_value awr_inst_num
select dbid from v$database;
select instance_number from v$instance;--Ash report in half an hour
column ashbegintime new_value ashbegin_str
column ashendtime new_value ashend_str
select to_char(sysdate-3/144,'yyyymmddhh24miss') as ashbegintime, to_char(sysdate,'yyyymmddhh24miss') as ashendtime from dual;column ashfile_name new_value ashfile
select 'ashrpt_' || to_char(&&awr_inst_num) || '_' || to_char(&&ashbegin_str) || '_' || to_char(&&ashend_str) ashfile_name from dual;
spool &&ashfile..html
select * from table(dbms_workload_repository.ash_report_html(to_char(&&awr_dbid),to_char(&&awr_inst_num),to_date(to_char(&&ashbegin_str),'yyyymmddhh24miss'),to_date(to_char(&&ashend_str),'yyyymmddhh24miss')));
spool off;--Create AWR snapshot
column begin_snap new_value awr_begin_snap
column end_snap new_value awr_end_snap
select max(snap_id) begin_snapfrom dba_hist_snapshotwhere snap_id < (select max(snap_id) from dba_hist_snapshot);
select max(snap_id) end_snap from dba_hist_snapshot;
declaresnap_maxtime date;snap_mintime date;
beginselect max(end_interval_time) + 0into snap_maxtimefrom dba_hist_snapshotwhere snap_id = to_number(&&awr_end_snap);select max(end_interval_time) + 0into snap_mintimefrom dba_hist_snapshotwhere snap_id = to_number(&&awr_begin_snap);if sysdate - snap_maxtime > 10/1445 thendbms_workload_repository.create_snapshot();end if;
end;
/--The latest two AWR reports between snap_id
column begin_snap new_value awr_begin_snap
column end_snap new_value awr_end_snap
select max(snap_id) begin_snapfrom dba_hist_snapshotwhere snap_id < (select max(snap_id) from dba_hist_snapshot);
select max(snap_id) end_snap from dba_hist_snapshot;
column awrfile_name new_value awrfile
select 'awrrpt_' || to_char(&&awr_inst_num) || '_' || to_char(&&awr_begin_snap) || '_' || to_char(&&awr_end_snap) awrfile_name from dual;spool &&awrfile..html
select output from table(dbms_workload_repository.awr_report_html(&&awr_dbid,&&awr_inst_num,&&awr_begin_snap,&&awr_end_snap));
spool off;--The longest AWR report available (all analyses since a week)
column begin_snap new_value awr_begin_snap
column end_snap new_value awr_end_snap
select a.begin_snap, a.end_snapfrom (select startup_time, min(snap_id) begin_snap, max(snap_id) end_snapfrom dba_hist_snapshotgroup by startup_time) a,(select max(startup_time) startup_time from dba_hist_snapshot) bwhere a.startup_time = b.startup_timeand rownum = 1;
column awrfile_name new_value awrfile
select 'awrrpt_' || to_char(&&awr_inst_num) || '_' || to_char(&&awr_begin_snap) || '_' || to_char(&&awr_end_snap) ||'_all' awrfile_name from dual;spool &&awrfile..html
select output from table(dbms_workload_repository.awr_report_html(&&awr_dbid,&&awr_inst_num,&&awr_begin_snap,&&awr_end_snap));
spool off;--Latest ADDM Report
column addmfile_name new_value addmfile
select 'addmrpt_' || to_char(&&awr_inst_num) || '_' || to_char(&&awr_begin_snap) || '_' || to_char(&&awr_end_snap) addmfile_name from dual;
set serveroutput on
spool &&addmfile..txt
declareid          number;name        varchar2(200) := '';descr       varchar2(500) := '';addmrpt     clob;v_ErrorCode number;
BEGINname := '&&addmfile';begindbms_advisor.create_task('ADDM', id, name, descr, null);dbms_advisor.set_task_parameter(name, 'START_SNAPSHOT', &&awr_begin_snap);dbms_advisor.set_task_parameter(name, 'END_SNAPSHOT', &&awr_end_snap);dbms_advisor.set_task_parameter(name, 'INSTANCE', &&awr_inst_num);dbms_advisor.set_task_parameter(name, 'DB_ID', &&awr_dbid);dbms_advisor.execute_task(name);exceptionwhen others thennull;end;select dbms_advisor.get_task_report(name, 'TEXT', 'TYPICAL')into addmrptfrom sys.dual;dbms_output.enable(20000000000);for i in 1 .. (DBMS_LOB.GETLENGTH(addmrpt) / 2000 + 1) loopdbms_output.put_line(substr(addmrpt, 1900 * (i - 1) + 1, 1900));end loop;dbms_output.put_line('');begindbms_advisor.delete_task(name);exceptionwhen others thennull;end;
end;
/
spool off;exit;

转载于:https://www.cnblogs.com/duwamish/p/10954643.html

一键获取数据库整体信息脚本相关推荐

  1. sql 获取数据库字段信息_使用DBATools获取SQL数据库详细信息

    sql 获取数据库字段信息 In the series of articles on DBATools, (see TOC at the bottom) we are exploring useful ...

  2. 查询Master下的系统表和系统视图获取数据库的信息和简单的渗透测试

    在SQL中可以通过查询Master下的系统表(sys)和系统视图(information_schema)获取数据库的信息.SQL2000和SQL2005的结构略有不同. 系统表结构参考系统表详细说明. ...

  3. 浅析ado.net获取数据库元数据信息

    写这个文章源于早先对ADO.Net获取数据库元数据上的认识,去年我在阅读ADO.Net Core Reference的时候曾经注意过DataSet的FillSchema的这个方法.这方面,在我之前的随 ...

  4. C#获取数据库表信息,列信息

    获取表的信息: conn.Open();string[] restrictions = new string[4];restrictions[1] = "dbo"; DataTab ...

  5. 使用OLE方式获取数据库架构信息

    一般连接SQL数据库的连接字串: data source=[SERVERNAME];user id=[USERNAME];password=[PASSWORD];database=[DATABASEN ...

  6. 检测硬件的批处理命令,检测硬件bat,一键获取电脑硬件信息

    警告:运行BAT源码是一种危险的动作,如果你不熟悉,请不要尝试! 批处理语言: 简体中文 授权方式: 免费软件 运行环境: Windows平台 检测硬件批处理命令.一键获取.直接双击就可以查看 @ec ...

  7. mysql 一键获取数据库表结构

    作用: 1.获取当前数据库所有的表信息 2.获取当前表结构信息 查询数据库中的表: -- 查询数据库中的表 SELECT * FROM information_schema.`TABLES` wher ...

  8. jdbc mysql查询整行信息_JDBC获取数据库各种信息

    当通过一个JDBC连接上一个数据的时候,我们就可以通过JDBC获取到这个数据的各种信息,比如数据库的名称.模式.表结构(及其字段).视图.函数.过程.访问权限等等一系列的信息.当然,如果你原意,你还可 ...

  9. java数据库编程(13) 获取数据库更多信息

    使用 DatabaseMetaData可以获得关于数据库的很多信息,比如支持的列,两张表之间的外键约束,查看主键约束等等. 这里的代码也不用怎么讲解了,如果前面的都懂的话,这些代码是可以看懂的. im ...

最新文章

  1. 算法设计思想(1)— 穷举法
  2. java实现高斯赛德尔算法解线性方程组
  3. 高端服务器CPU详细图解
  4. Java语法糖之foreach
  5. 透明轮播原生JavaScript实现
  6. xshell1分钟就会自动断_手术室自动门不能正常控制开关门维修案例
  7. php 系统模版_原生 PHP 模板系统:Plates
  8. mysql系统搭建互备DB(双主)记录
  9. 奇偶链表的分割(C++)
  10. mybatis配置全局变量
  11. css连续的纯数字或字母强制换行
  12. 任务寄存器TR:GDT、LDT、IDT、TR、TSS之间的关系
  13. vSphere 7 With K8s系列-1~9 (微信公众号需要收费)
  14. Windows 查看端口使用情况
  15. Linux TTY 串口 struct termios结构体参数
  16. mac 谷歌浏览器 跨域访问
  17. 全国大学生GIS应用技能大赛(开发试题参考)
  18. Scrum立会报告+燃尽图(十月二十八日总第十九次)
  19. Android 项目必备(十四)--> 开发者选项
  20. 结合《穹顶之下》看中、美宽带提速

热门文章

  1. C++ 操作符new和delete
  2. 用xargs处理带空格文件名
  3. ASP.NET备份恢复SqlServer数据库
  4. 一个开源小项目,如何使用「分类网络」实现排球追踪
  5. TMM|车辆重识别的一些实践
  6. 腾讯开源视频动作检测算法DBG,打破两项世界纪录!
  7. 图鸭科技重金揽才-深度学习优化研究员
  8. python字符串结合操作符的使用
  9. Python进阶|聊聊异常处理
  10. 年薪十万的王者荣耀,LOL游戏模型师的工作是这样的|附50G资料