数据库状态检查综合脚本

----查询物理文件是否有offline

  1. select name, bytes, status from v$datafile where status = 'OFFLINE';
复制代码
----查询失效对象
  1. SELECT owner, object_name, object_type
  2.   FROM dba_objects
  3. WHERE status = 'INVALID'
  4.    and owner not in ('SYS', 'SYSTEM');
复制代码
----查看redo信息
  1. select * from v$log;
复制代码
----查看redo文件状态
  1. select * from v$logfile;
复制代码
----查看disable的约束
  1. SELECT owner, constraint_name, table_name, constraint_type, status
  2.   FROM dba_constraints
  3. WHERE status = 'DISABLED'
  4.    AND constraint_type in ('P', 'U', 'C', 'O', 'R', 'V')
  5.    and owner not in ('SYS', 'SYSTEM');
复制代码
--查看pga使用
  1. select s.username username,s.serial# serial,se.sid,n.name,
  2.        max(se.value) maxmem
  3. from v$sesstat se,
  4.      v$statname n,
  5.      v$session s
  6. where n.statistic# = se.statistic#
  7. and s.sid=se.sid
  8. and s.username is not null
  9. group by n.name,se.sid,s.username,s.serial#
  10. order by 2;
复制代码
--share pool中未使用绑定变量的语句
  1. select d.plan_hash_value plan_hash_value,
  2.        d.execnt          execnt,
  3.        a.hash_value      hash_value,
  4.        a.sql_text        sql_text
  5.   from v$sqltext a,
  6.        (select plan_hash_value, hash_value, execnt
  7.           from (select c.plan_hash_value,
  8.                        b.hash_value,
  9.                        c.execnt,
  10.                        rank() over(partition by c.plan_hash_value order by b.hash_value) as hashrank
  11.                   from v$sql b,
  12.                        (select count(*) as execnt, plan_hash_value
  13.                           from v$sql
  14.                          where plan_hash_value <> 0
  15.                          group by plan_hash_value
  16.                         having count(*) > 10
  17.                          order by count(*) desc) c
  18.                  where b.plan_hash_value = c.plan_hash_value
  19.                  group by c.plan_hash_value, b.hash_value, c.execnt)
  20.          where hashrank <= 3) d
  21. where a.hash_value = d.hash_value
  22. order by d.execnt desc, a.hash_value, a.piece;
复制代码
--查看全部扫描及全索引扫描次数
  1. select name, value
  2.   from v$sysstat
  3. where name in
  4.        ('table scans (short tables)', 'table scans (long tables)',
  5.         'index fast full scans (full)',
  6.         'index fast full scans (rowid ranges)',
  7.         'index fast full scans (direct read)', 'table scans (rowid ranges)',
  8.         'table scans (cache partitions)', 'table scans (direct read)',
  9.        'table scan blocks gotten');
复制代码
--查看归档频率
  1. SELECT
  2. TO_CHAR(first_time,'MM/DD') DAY
  3. --, TO_CHAR(first_time,'YYYY/MM/DD') DAY2
  4. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'00',1,0)) H00
  5. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'01',1,0)) H01
  6. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'02',1,0)) H02
  7. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'03',1,0)) H03
  8. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'04',1,0)) H04
  9. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'05',1,0)) H05
  10. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'06',1,0)) H06
  11. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'07',1,0)) H07
  12. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'08',1,0)) H08
  13. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'09',1,0)) H09
  14. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'10',1,0)) H10
  15. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'11',1,0)) H11
  16. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'12',1,0)) H12
  17. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'13',1,0)) H13
  18. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'14',1,0)) H14
  19. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'15',1,0)) H15
  20. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'16',1,0)) H16
  21. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'17',1,0)) H17
  22. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'18',1,0)) H18
  23. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'19',1,0)) H19
  24. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'20',1,0)) H20
  25. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'21',1,0)) H21
  26. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'22',1,0)) H22
  27. , SUM(DECODE(TO_CHAR(first_time, 'HH24'),'23',1,0)) H23
  28. , COUNT(*)||'('||trim(to_char(sum(blocks*block_size)/1024/1024/1024,'99,999.9'))||'G)' TOTAL
  29. FROM
  30. (select max(blocks) blocks,max(block_size) block_size,max(first_time) first_time
  31. from
  32. v$archived_log a
  33. where COMPLETION_TIME > sysdate - &day
  34. and dest_id = 1
  35. group by sequence#
  36. )
  37. group by TO_CHAR(first_time,'MM/DD'), TO_CHAR(first_time,'YYYY/MM/DD')
  38. order by TO_CHAR(first_time,'YYYY/MM/DD') desc;
复制代码
--查看IO情况
  1. select substr(to_char(sn.snap_time, 'yyyy-mm-dd HH24:MI:SS'), 0),
  2.        round(((new_db_block_gets.value - old_db_block_gets.value) *
  3.              (select value from v$parameter where NAME = 'db_block_size')) / 1024 / 1024 / 1024,
  4.              2) "db block gets (GB)",
  5.        round(((new_consistent_gets.value - old_consistent_gets.value) *
  6.              (select value from v$parameter where NAME = 'db_block_size')) / 1024 / 1024 / 1024,
  7.              2) "consistent gets (GB)",
  8.        round(((new_physical_reads.value - old_physical_reads.value) *
  9.              (select value from v$parameter where NAME = 'db_block_size')) / 1024 / 1024 / 1024,
  10.              2) "physical reads (GB)"
  11.   from perfstat.stats$sysstat  old_db_block_gets,
  12.        perfstat.stats$sysstat  new_db_block_gets,
  13.        perfstat.stats$sysstat  old_consistent_gets,
  14.        perfstat.stats$sysstat  new_consistent_gets,
  15.        perfstat.stats$sysstat  old_physical_reads,
  16.        perfstat.stats$sysstat  new_physical_reads,
  17.        perfstat.stats$snapshot sn
  18. where new_db_block_gets.snap_id = sn.snap_id
  19.    and new_consistent_gets.snap_id = sn.snap_id
  20.    and new_physical_reads.snap_id = sn.snap_id
  21.    and old_db_block_gets.snap_id = sn.snap_id - 1
  22.    and old_consistent_gets.snap_id = sn.snap_id - 1
  23.    and old_physical_reads.snap_id = sn.snap_id - 1
  24.    and old_db_block_gets.statistic# = 40
  25.    and new_db_block_gets.statistic# = 40
  26.    and old_consistent_gets.statistic# = 41
  27.    and new_consistent_gets.statistic# = 41
  28.    and old_physical_reads.statistic# = 42
  29.    and new_physical_reads.statistic# = 42
  30.    and (new_db_block_gets.value - old_db_block_gets.value) > 0
  31.    and (new_consistent_gets.value - old_consistent_gets.value) > 0
  32.    and (new_physical_reads.value - old_physical_reads.value) > 0
  33. order by substr(to_char(sn.snap_time, 'yyyy-mm-dd HH24:MI:SS'), 0) desc;
复制代码
--查看锁对象
  1. select p.spid,
  2.        c.object_name,
  3.        c.subobject_name,
  4.        b.session_id,
  5.        b.oracle_username,
  6.        b.os_user_name
  7.   from v$process p, v$session a, v$locked_object b, all_objects c
  8. where p.addr = a.paddr
  9.    and a.process = b.process
  10.    and c.object_id = b.object_id 
  11. and object_name !='OBJ--查看session IO
  12. [code]select b.username, a.*
  13.   from v$sess_io a, v$session b
  14. where a.sid in
  15.        (select x.sid
  16.           from v$session x
  17.          where x.status = 'ACTIVE'
  18.            and x.PADDR not in (select paddr from v$bgprocess))
  19.    and a.sid = b.sid
  20.    and username is not null
  21.    and username <> 'SYS';
复制代码
--查看session top3
  1. select *
  2.   from (select row_number() over(partition by a.STATISTIC# order by a.value desc) size_rank,
  3.                a.sid,
  4.                b.name,
  5.                a.value
  6.           from v$sesstat a,v$statname b,v$session c
  7.           where c.STATUS='ACTIVE'
  8.           and c.USERNAME is not null
  9.           and a.sid=c.sid
  10.           and a.STATISTIC#=b.STATISTIC#
  11.          order by a.value desc)
  12. where size_rank <= 3
  13. and name like '%&NAME%'
  14. and sid like '%&SID%'
  15. order by name, size_rank;
复制代码
--查看undo的占用
  1. select s.username,s.SID, u.name, r.RSSIZE, r.WRITES, r.HWMSIZE
  2.   from v$transaction t, v$rollstat r, v$rollname u, v$session s
  3. where s.taddr = t.addr
  4.    and t.xidusn = r.usn
  5.    and r.usn = u.usn
  6. order by r.rssize desc;
复制代码
--查看会话堵塞
  1. select c.username,a.sid,c.serial#,' is blocking ',d.username,b.sid,d.serial#
  2. from 
  3. (select sid,id1,id2 from v$lock where block =1) a,
  4. (select sid,id1,id2 from v$lock where request > 0) b,
  5. (select sid,serial#,username from v$session ) c,
  6. (select sid,serial#,username from v$session ) d
  7. where a.id1=b.id1
  8. and a.id2=b.id2
  9. and a.sid=c.sid
  10. and b.sid=d.sid;
复制代码
--查看正在操作的排序
  1. select a.tablespace,
  2.        a.SEGFILE#,
  3.        a.segblk#,
  4.        a.blocks,
  5.        b.sid,
  6.        b.serial#,
  7.        b.username,
  8.        b.osuser,
  9.        b.status
  10.   from v$sort_usage a, v$session b
  11. where a.session_addr = b.saddr;
复制代码
--查询系统进程对应的SQL
  1. select /*+ordered  */
  2. sql_text,
  3. spid,
  4. v$session.program,
  5. machine,
  6. process,
  7. sid,
  8. v$session.SERIAL#
  9.   from v$process, v$session, v$sql
  10. where v$sql.address = v$session.sql_address
  11.    and v$sql.hash_value = v$session.sql_hash_value
  12.    and v$session.paddr = v$process.addr
  13.    and v$process.spid = &spid;
复制代码
--查看高水位线
  1. select TABLE_NAME,HWM,AVG_USED_BLOCKS,                                                                           
  2. GREATEST(ROUND(100 * (NVL(HWM - AVG_USED_BLOCKS,0)/GREATEST(NVL(HWM,1),1) ), 2), 0) WASTE_PER,sysdate analyze_dt 
  3. from                                                                                                             
  4.      (SELECT A.BLOCKS - B.EMPTY_BLOCKS - 1 HWM,table_name,                                                       
  5.        DECODE( ROUND((B.AVG_ROW_LEN * NUM_ROWS * (1 + (PCT_FREE/100)))/8192, 0),                                 
  6.                                 0, 1,                                                                            
  7.                                 ROUND((B.AVG_ROW_LEN * NUM_ROWS * (1 + (PCT_FREE/100)))/8192, 0)                 
  8.                            ) + 2 AVG_USED_BLOCKS                                                                 
  9.         FROM USER_SEGMENTS A,                                                                                    
  10.              USER_TABLES B                                                                                       
  11.         WHERE SEGMENT_NAME = TABLE_NAME                                                                          
  12.          and TABLE_NAME in ('XXX')                                                                               
  13.          and SEGMENT_TYPE = 'TABLE'                                                                              
  14.       );                                                                                                         
复制代码
--监控索引的使用
  1. CREATE OR REPLACE PROCEDURE start_monitor_index
  2. IS
  3. Cursor cur1 is
  4. select OWNER,INDEX_NAME from dba_indexes di where di.owner like '%BJ';

  5. p_rows cur1%ROWTYPE;
  6. p_str varchar2(200);

  7. begin
  8. open cur1;
  9. loop
  10. fetch cur1 into p_rows;
  11. exit when cur1%NOTFOUND;
  12. p_str:='alter index '||p_rows.owner||'.'||p_rows.index_name||' MONITORING USAGE';
  13. execute immediate p_str;
  14. --dbms_output.put_line(p_str);
  15. end loop;
  16. close cur1;
  17. dbms_output.put_line('finish');
  18. COMMIT;
  19. END;

  20. CREATE OR REPLACE PROCEDURE stop_monitor_index
  21. IS
  22. Cursor cur1 is
  23. select OWNER,INDEX_NAME from dba_indexes di where di.owner like '%BJ';

  24. p_rows cur1%ROWTYPE;
  25. p_str varchar2(200);

  26. begin
  27. open cur1;
  28. loop
  29. fetch cur1 into p_rows;
  30. exit when cur1%NOTFOUND;
  31. p_str:='alter index '||p_rows.owner||'.'||p_rows.index_name||' NOMONITORING USAGE';
  32. execute immediate p_str;
  33. --dbms_output.put_line(p_str);
  34. end loop;
  35. close cur1;
  36. dbms_output.put_line('finish');
  37. COMMIT;
  38. END;
  39. /
  40. create or replace view v$object_usage
  41. (index_name, table_name, monitoring, used, start_monitoring, end_monitoring)
  42. as
  43. select io.name, t.name,
  44.        decode(bitand(i.flags, 65536), 0, 'NO', 'YES'),
  45.        decode(bitand(ou.flags, 1), 0, 'NO', 'YES'), 
  46.        ou.start_monitoring,
  47.        ou.end_monitoring
  48. from sys.obj$ io, sys.obj$ t, sys.ind$ i, sys.object_usage ou
  49. where io.owner# = userenv('SCHEMAID')
  50.   and i.obj# = ou.obj#
  51.   and io.obj# = ou.obj#
  52.   and t.obj# = i.bo#;
复制代码
--索引自动重建脚本
  1. REM =============================================================
  2. REM
  3. REM                         rebuild_indx.sql
  4. REM
  5. REM  Copyright (c) Oracle Software, 1998 - 2000
  6. REM 
  7. REM  Author  : Jurgen Schelfhout
  8. REM
  9. REM  The sample program in this article is provided for educational
  10. REM  purposes only and is NOT supported by Oracle Support Services.
  11. REM  It has been tested internally, however, and works as documented.
  12. REM  We do not guarantee that it will work for you, so be sure to test
  13. REM  it in your environment before relying on it.
  14. REM
  15. REM  This script will analyze all the indexes for a given schema
  16. REM  or for a subset of schema's. After this the dynamic view
  17. REM  index_stats is consulted to see if an index is a good
  18. REM  candidate for a rebuild or for a bitmap index.
  19. REM
  20. REM  Database Version : 7.3.X and above.
  21. REM  
  22. REM  NOTE:  If running this on 10g, you must exclude the 
  23. REM  objects in the Recycle Bin
  24. REM        cursor c_indx is
  25. REM          select owner, table_name, index_name
  26. REM            from dba_indexes
  27. REM           where owner like upper('&schema')
  28. REM             and table_name not like 'BIN$%'
  29. REM             and owner not in ('SYS','SYSTEM');
  30. REM
  31. REM  Additional References for Recycle Bin functionality:
  32. REM  Note.265254.1 Flashback Table feature in Oracle Database 10g
  33. REM  Note.265253.1 10g Recyclebin Features And How To Disable it(_recyclebin)
  34. REM
  35. REM =============================================================

  36. prompt
  37. ACCEPT spoolfile CHAR prompt 'Output-file : ';
  38. ACCEPT schema CHAR prompt 'Schema name (% allowed) : '; 
  39. prompt
  40. prompt
  41. prompt Rebuild the index when :
  42. prompt   - deleted entries represent 20% or more of the current entries
  43. prompt   - the index depth is more then 4 levels.
  44. prompt Possible candidate for bitmap index :
  45. prompt   - when distinctiveness is more than 99%
  46. prompt
  47. spool &spoolfile

  48. set serveroutput on
  49. set verify off
  50. declare
  51. c_name        INTEGER;
  52. ignore        INTEGER;
  53. height        index_stats.height%TYPE := 0;
  54. lf_rows       index_stats.lf_rows%TYPE := 0;
  55. del_lf_rows   index_stats.del_lf_rows%TYPE := 0;
  56. distinct_keys index_stats.distinct_keys%TYPE := 0;
  57. cursor c_indx is
  58.   select owner, table_name, index_name
  59.   from dba_indexes
  60.   where owner like upper('&schema')
  61.     and owner not in ('SYS','SYSTEM');
  62. cursor c_indx is
  63.        select owner, table_name, index_name
  64.           from dba_indexes
  65.        where owner like upper('&schema')
  66.       and table_name not like 'BIN$%'
  67.         and owner not in ('SYS','SYSTEM');



  68. begin
  69. dbms_output.enable (1000000);
  70. dbms_output.put_line ('Owner           Index Name                              % Deleted Entries Blevel Distinctiveness');
  71. dbms_output.put_line ('--------------- --------------------------------------- ----------------- ------ ---------------');

  72. c_name := DBMS_SQL.OPEN_CURSOR;
  73. for r_indx in c_indx loop
  74.   DBMS_SQL.PARSE(c_name,'analyze index ' || r_indx.owner || '.' || 
  75.                  r_indx.index_name || ' validate structure',DBMS_SQL.NATIVE);
  76.   ignore := DBMS_SQL.EXECUTE(c_name);

  77.   select HEIGHT, decode (LF_ROWS,0,1,LF_ROWS), DEL_LF_ROWS, 
  78.          decode (DISTINCT_KEYS,0,1,DISTINCT_KEYS) 
  79.          into height, lf_rows, del_lf_rows, distinct_keys
  80.   from index_stats;
  81. --
  82. -- Index is considered as candidate for rebuild when :
  83. --   - when deleted entries represent 20% or more of the current entries
  84. --   - when the index depth is more then 4 levels.(height starts counting from 1 so > 5)
  85. -- Index is (possible) candidate for a bitmap index when :
  86. --   - distinctiveness is more than 99%
  87. --
  88.   if ( height > 5 ) OR ( (del_lf_rows/lf_rows) > 0.2 ) then
  89.     dbms_output.put_line (rpad(r_indx.owner,16,' ') || rpad(r_indx.index_name,40,' ') || 
  90.                           lpad(round((del_lf_rows/lf_rows)*100,3),17,' ') || 
  91.                           lpad(height-1,7,' ') || lpad(round((lf_rows-distinct_keys)*100/lf_rows,3),16,' '));
  92.   end if;

  93. end loop;
  94. DBMS_SQL.CLOSE_CURSOR(c_name);
  95. end;
  96. /

  97. spool off
  98. set verify on
复制代码
--自动修复行链接
  1. CREATE OR REPLACE PROCEDURE repair_chained_rows(f_owner in varchar) IS
  2.   Cursor cur1 is
  3.     select table_name from dba_tables dt where dt.owner = f_owner;

  4.   --  type c_type is ref cursor;
  5.   --  cur2 c_type;

  6.   p_rows cur1%ROWTYPE;
  7.   p_str  varchar2(200);
  8.   p_str2 varchar2(200);
  9.   p_str3 varchar2(200);
  10.   p_str4 varchar2(200);
  11.   p_str5 varchar2(200);
  12.   x      number(10) default 1;

  13. begin
  14.   open cur1;
  15.   loop
  16.     fetch cur1
  17.       into p_rows;
  18.     exit when cur1%NOTFOUND;
  19.     p_str := 'analyze table ' || f_owner || '.' || p_rows.table_name ||
  20.              ' list chained rows'; ---分析表
  21.     execute immediate p_str; ---执行分析
  22.     dbms_output.put_line(p_str);
  23.   
  24.     p_str2 := '
  25. select * from ' || f_owner || '.' || p_rows.table_name ||
  26.               '  where rowid in (select HEAD_ROWID from chained_rows)';
  27.     ---查找行链接的记录
  28.     execute immediate p_str2;
  29.   
  30.     if SQL%NOTFOUND then
  31.       dbms_output.put_line('NO CHAINED ROWS');
  32.       p_str3 := 'create table ACR_TEMP' || x || ' as ' || p_str2;
  33.       p_str4 := 'delete from ' || f_owner || '.' || p_rows.table_name ||
  34.                 ' where rowid in (select HEAD_ROWID from chained_rows)';
  35.       p_str5 := 'insert into ' || f_owner || '.' || p_rows.table_name ||
  36.                 ' select * from ACR_TEMP' || x;
  37.       dbms_output.put_line(p_str3);
  38.       dbms_output.put_line(p_str4);
  39.       dbms_output.put_line(p_str5);
  40.       x := x + 1;
  41.     else
  42.       p_str3 := 'create table ACR_TEMP' || x || ' as ' || p_str2;
  43.       p_str4 := 'delete from ' || f_owner || '.' || p_rows.table_name ||
  44.                 ' where (rowid in select HEAD_ROWID from chained_rows)';
  45.       p_str5 := 'insert into ' || f_owner || '.' || p_rows.table_name ||
  46.                 ' select * from ACR_TEMP' || x;
  47.       dbms_output.put_line('HAVE SOME CHAINED ROWS');
  48.       x := x + 1;
  49.     end if;
  50.   
  51.   end loop;
  52.   close cur1;
  53.   COMMIT;
  54. END;
  55. /
复制代码
--查看session IO
  1. select b.username, a.*
  2.   from v$sess_io a, v$session b
  3. where a.sid in
  4.        (select x.sid
  5.           from v$session x
  6.          where x.status = 'ACTIVE'
  7.            and x.PADDR not in (select paddr from v$bgprocess))
  8.    and a.sid = b.sid
  9.    and username is not null
  10.    and username &lt;&gt; 'SYS';
复制代码
--查看表空间使用情况
  1. SELECT /*+ NO_MERGE(D) NO_MERGE(A) NO_MERGE(F) NO_MERGE(U) NO_MERGE(O) */ D.TABLESPACE_NAME "表空间名", D.BLOCK_SIZE/1024 "块大小(KB)",D.INITIAL_EXTENT/1024 "初始分配大小(KB)",
  2.        ROUND(NVL(A.BYTES /1024 /1024,0) ,2) "大小(MB)",
  3.        ROUND(DECODE(D.CONTENTS, 'UNDO', NVL(U.BYTES, 0) / 1024 / 1024,NVL(A.BYTES - NVL(F.BYTES, 0), 0) / 1024 / 1024) ,2) "占用量(MB)",
  4.        TO_CHAR(ROUND(DECODE(D.CONTENTS, 'UNDO', NVL(U.BYTES / A.BYTES * 100, 0),NVL((A.BYTES - NVL(F.BYTES, 0)) / A.BYTES * 100, 0)) ,2),'999.99')||'%' "占用率(MB)",
  5.        ROUND(DECODE(D.CONTENTS, 'UNDO', NVL(A.BYTES - NVL(U.BYTES, 0), 0) / 1024 / 1024,NVL(F.BYTES, 0) / 1024 / 1024) ,2) "空闲空间(MB)", 
  6.        D.STATUS "状态",A.AUTOEXTENSIBLE "是否自动扩展",D.LOGGING "是否记录日志", A.COUNT "数据文件", D.CONTENTS "类型",
  7.        D.EXTENT_MANAGEMENT "区管理", D.SEGMENT_SPACE_MANAGEMENT "段管理"
  8.   FROM SYS.DBA_TABLESPACES D,
  9.        (SELECT TABLESPACE_NAME, SUM(BYTES) BYTES, COUNT(FILE_ID) COUNT,
  10.                CASE WHEN SUM(DECODE(AUTOEXTENSIBLE,'YES',10000,'NO',1,AUTOEXTENSIBLE))>=10000 THEN 'YES' ELSE 'NO' END||CASE WHEN MOD(SUM(DECODE(AUTOEXTENSIBLE,'YES',10000,'NO',1,AUTOEXTENSIBLE)),10000)>0 THEN CHR(13)||CHR(38)||CHR(13)||'NO' ELSE '' END AS AUTOEXTENSIBLE 
  11.           FROM DBA_DATA_FILES GROUP BY TABLESPACE_NAME) A,
  12.        (SELECT TABLESPACE_NAME, SUM(BYTES) BYTES FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME) F,
  13.        (SELECT TABLESPACE_NAME, SUM(BYTES) BYTES
  14.          FROM DBA_UNDO_EXTENTS
  15.          WHERE STATUS IN ('UNEXPIRED', 'UNKNOWN')
  16.          GROUP BY TABLESPACE_NAME) U
  17.   WHERE D.TABLESPACE_NAME = A.TABLESPACE_NAME(+) AND D.TABLESPACE_NAME = F.TABLESPACE_NAME(+) 
  18.     AND D.TABLESPACE_NAME = U.TABLESPACE_NAME(+) AND NOT (D.EXTENT_MANAGEMENT = 'LOCAL' AND D.CONTENTS = 'TEMPORARY') 
  19.   --AND D.TABLESPACE_NAME LIKE :1
  20. UNION ALL
  21. SELECT /*+ NO_MERGE(D) NO_MERGE(A) NO_MERGE(T) */ D.TABLESPACE_NAME "表空间名", D.BLOCK_SIZE/1024 "块大小(KB)",D.INITIAL_EXTENT/1024 "初始分配大小(KB)",
  22.        ROUND(NVL(A.BYTES /1024 /1024,0) ,2) "大小(MB)",
  23.        ROUND(NVL(T.BYTES, 0) / 1024 / 1024 ,2) "占用量(MB)", 
  24.        TO_CHAR(ROUND(NVL(T.BYTES / A.BYTES * 100, 0) ,2),'999.99')||'%' "占用率(MB)",
  25.        ROUND((NVL(A.BYTES, 0) / 1024 / 1024 - NVL(T.BYTES, 0) / 1024 / 1024) ,2) "空闲空间(MB)",
  26.        D.STATUS "状态",A.AUTOEXTENSIBLE "是否自动扩展",D.LOGGING "是否记录日志", A.COUNT "数据文件", D.CONTENTS "类型",
  27.        D.EXTENT_MANAGEMENT "区管理", D.SEGMENT_SPACE_MANAGEMENT "段管理"
  28.   FROM SYS.DBA_TABLESPACES D,
  29.        (SELECT TABLESPACE_NAME, SUM(BYTES) BYTES, COUNT(FILE_ID) COUNT,
  30.                CASE WHEN SUM(DECODE(AUTOEXTENSIBLE,'YES',10000,'NO',1,AUTOEXTENSIBLE))>=10000 THEN 'YES' ELSE 'NO' END||CASE WHEN MOD(SUM(DECODE(AUTOEXTENSIBLE,'YES',10000,'NO',1,AUTOEXTENSIBLE)),10000)>0 THEN CHR(13)||CHR(38)||CHR(13)||'NO' ELSE '' END AS AUTOEXTENSIBLE 
  31.           FROM DBA_TEMP_FILES GROUP BY TABLESPACE_NAME) A,
  32.        (SELECT SS.TABLESPACE_NAME, SUM((SS.USED_BLOCKS * TS.BLOCKSIZE)) BYTES
  33.          FROM GV$SORT_SEGMENT SS, SYS.TS$ TS
  34.          WHERE SS.TABLESPACE_NAME = TS.NAME
  35.          GROUP BY SS.TABLESPACE_NAME) T
  36.   WHERE D.TABLESPACE_NAME = A.TABLESPACE_NAME(+) AND D.TABLESPACE_NAME = T.TABLESPACE_NAME(+) 
  37.     AND D.EXTENT_MANAGEMENT = 'LOCAL'
  38.     AND D.CONTENTS = 'TEMPORARY' 
  39.   --AND D.TABLESPACE_NAME LIKE :2
  40. ORDER BY 6 DESC;
复制代码
--查看session情况
  1. select /*+rule*/      a.sid,
  2.                       a.username as "用户名",
  3.                       j.EVENT as "等待事件",
  4.                       /*j.P1TEXT,
  5.                       j.P1,
  6.                       j.P2TEXT,
  7.                       j.p2,
  8.                       j.p3text,
  9.                       j.p3,
  10.                       j.STATE,*/              
  11.                       a.osuser as "OS用户名",
  12.                       a.machine "OS机器名",
  13.                       a.program "OS程序名",
  14.                       a.module "模块名",
  15.                       a.action "动作名",
  16.                       i.name as "操作命令",
  17.                       a.type "用户类型",
  18.                       a.logon_time "登陆时间",
  19.                       g.ospid "OS进程号",
  20.                       c.logical_reads "逻辑读",
  21.                       c.physical_reads "物理读",
  22.                       c.io_write "写IO",
  23.                       d.cputimes "CPU执行时间",
  24.                       e.memsize "共享内存占用",
  25.                       f.redosize "REDO大小",
  26.                       'select sql_text from v$sqltext where address=''' || b.address ||
  27.                       ''' and hash_value=''' || b.hash_value || ''' order by piece' sql_text,
  28.                       'select sql_text from v$sqltext where address=''' || k.address ||
  29.                       ''' and hash_value=''' || k.hash_value || ''' order by piece' prev_sql_text
  30.                  from (select sid,
  31.                               username,
  32.                               osuser,
  33.                               machine,
  34.                               program,
  35.                               module,
  36.                               action,
  37.                               command,
  38.                               type,
  39.                               logon_time,
  40.                               paddr,
  41.                               taddr,
  42.                               lockwait,
  43.                               sql_address,
  44.                               sql_hash_value,
  45.                               prev_sql_addr,
  46.                               prev_hash_value
  47.                          from v$session
  48.                         where status = 'ACTIVE'
  49.                           and username is not null
  50.                           and username <> 'SYS'
  51.                           and type <> 'BACKGROUND') a,
  52.                       AUDIT_ACTIONS i,
  53.                       v$session_wait j,
  54.                       (select address, hash_value, sql_text from v$sqltext where piece = 0) b,
  55.                       (select address, hash_value, sql_text from v$sqltext where piece = 0) k,
  56.                       (select sid,
  57.                               round((block_gets + consistent_gets) * 8 / 1024) logical_reads,
  58.                               round(physical_reads * 8 / 1024) physical_reads,
  59.                               round((block_changes + consistent_changes) * 8 / 1024) io_write
  60.                          from v$sess_io) c,
  61.                       (select sid, value / 100 cputimes
  62.                          from v$sesstat
  63.                         where statistic# = 11) d,
  64.                       (select sid, sum(value) memsize
  65.                          from v$sesstat
  66.                         where statistic# in (15, 20)
  67.                         group by sid) e,
  68.                       (select sid,
  69.                               sum((decode(statistic#, 115, value, 0) +
  70.                                   decode(statistic#, 117, value, 0)) /
  71.                                   (decode(statistic#, 120, value, 0) + 16)) redosize
  72.                          from v$sesstat
  73.                         group by sid) f,
  74.                       (select addr, spid ospid from v$process) g
  75.                 where (a.sql_address = b.address(+) and a.sql_hash_value = b.hash_value(+))
  76.                   and (a.prev_sql_addr = k.address(+) and a.prev_hash_value = k.hash_value(+))
  77.                   and (a.sid = c.sid(+))
  78.                   and (a.sid = d.sid(+))
  79.                   and (a.sid = e.sid(+))
  80.                   and (a.sid = f.sid(+))
  81.                   and (a.paddr = g.addr(+))
  82.                   and a.command = i.action
  83.                   and (a.sid = j.sid(+))
  84.                   order by c.logical_reads desc nulls last;



**********本博客所有内容均为原创,如有转载请注明作者和出处!!!**********
Name:    guoyJoe

QQ:        252803295

Email:    oracledba_cn@hotmail.com

Blog:      http://blog.csdn.net/guoyJoe

ITPUB:   http://www.itpub.net/space-uid-28460966.html

OCM:     http://education.oracle.com/education/otn/YGuo.HTM
 _____________________________________________________________
加群验证问题:哪些SGA结构是必需的,哪些是可选的?否则拒绝申请!!!

答案在:http://blog.csdn.net/guoyjoe/article/details/8624392

Oracle@Paradise  总群:127149411

Oracle@Paradise No.1群:177089463(已满)

Oracle@Paradise No.2群:121341761

Oracle@Paradise No.3群:140856036


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值