监视数据库性能的几个sql

来源:互联网 发布:mac mysql 客户端 编辑:程序博客网 时间:2024/04/29 12:16

  1. 监控事例的等待
  select event,sum(decode(wait_Time,0,0,1)) "Prev",
  sum(decode(wait_Time,0,1,0)) "Curr",count(*) "Tot"
  from v$session_Wait
  group by event order by 4;


 2. 回滚段的争用情况
  select name, waits, gets, waits/gets "Ratio"
  from v$rollstat a, v$rollname b
  where a.usn = b.usn;


 3. 监控表空间的 I/O 比例
  select df.tablespace_name name,df.file_name "file",f.phyrds pyr,
  f.phyblkrd pbr,f.phywrts pyw, f.phyblkwrt pbw
  from v$filestat f, dba_data_files df
  where f.file# = df.file_id
  order by df.tablespace_name;


 4. 监控文件系统的 I/O 比例
  select substr(a.file#,1,2) "#", substr(a.name,1,30) "Name",
  a.status, a.bytes, b.phyrds, b.phywrts
  from v$datafile a, v$filestat b
  where a.file# = b.file#;


 5.在某个用户下找所有的索引
  select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name
  from user_ind_columns, user_indexes
  where user_ind_columns.index_name = user_indexes.index_name
  and user_ind_columns.table_name = user_indexes.table_name
  order by user_indexes.table_type, user_indexes.table_name,
  user_indexes.index_name, column_position;


 6. 监控 SGA 的命中率
  select a.value + b.value "logical_reads", c.value "phys_reads",
  round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) "BUFFER HIT RATIO"
  from v$sysstat a, v$sysstat b, v$sysstat c
  where a.statistic# = 38 and b.statistic# = 39
  and c.statistic# = 40;


 7. 监控 SGA 中字典缓冲区的命中率
  select parameter, gets,Getmisses , getmisses/(gets+getmisses)*100 "miss ratio",
  (1-(sum(getmisses)/ (sum(gets)+sum(getmisses))))*100 "Hit ratio"
  from v$rowcache
  where gets+getmisses <>0
  group by parameter, gets, getmisses;


 8. 监控 SGA 中共享缓存区的命中率,应该小于1%
  select sum(pins) "Total Pins", sum(reloads) "Total Reloads",
  sum(reloads)/sum(pins) *100 libcache
  from v$librarycache;
  select sum(pinhits-reloads)/sum(pins) "hit radio",sum(reloads)/sum(pins) "reload percent"
  from v$librarycache;


   9.表空间碎片整理
      1,指定pctincrease不为0,则系统自动相邻自由盘区空间合并
      2,如果pctincrease为0,则系统不会自动实现相邻自由盘区空间合并,
        但是可以使用oracle命令 alter tablespace xxxx coalesce,强制
        相邻自由盘区空间合并.但是该命令不能合并被数据盘区分割的自由
        盘区.
      3,如果想彻底的合并所有的自由空间,只有对该表空间施行导出导入.以
        重新分配数据盘区
  
   10,重组表(使用move 命令可以减少行迁移,优化性能)
       可以删除旧表,复制数据,创建新表   
       alter table xxx move
       tablespace new_tbs
       storage (initial xxM next xM pctincrease 0)
       pctfree 0 pctused 50
       initrans 2 nologging;