首页 > 开发 > 综合 > 正文

一个DBA_Monitor.sql程序

2024-07-21 02:38:18
字体:
来源:转载
供稿:网友

  set echo on
  spool user_DBA_report.txt
  set pages 333 lin 96
  rem 这是一个DBA_Monitor.sql程序,目的是日常监测分析数据库之用。
  rem 使用时服务器并不一定要设置为timed_statistics=true。
  rem 我希望在你们这里可以发表一个月后,我收集意见后再修改为正式版。
  rem 看过的朋友一定提更改意见噢!
  rem 主要参考《Oracle8 DBA Handbook》,《Oracle8 Tuning》。
  rem
  rem
  rem ---------------------------------------------------
  rem 执行说明:本程序第一部分需要DBA权限,
  rem 第二部分针对实际用户,它们拥有表,索引,Source等。
  rem # cat> ding92sql
  rem sqlplus system/passwd@stamex <  rem @dba_monitor.sql
  rem connect stamexxx/passwd@stamex
  rem @getuser_objects.sql
  rem EOFa1
  rem exit
  rem 数据库性能调整包括三方面的内容OS,DB,App)
  rem OS=操作系统,分别从CPU,Memory,Disk,NetWork
  rem 查询数据表的:名称行数.变化列数InitailCacheTSpace
  rem 查询数据表索引的:名称表名列数类型Initial
  rem 查询数据表约束:名称表名相关性类型
  rem 查询SQL的频率和效率v_$sqlarea,跟踪到sqls表中
  rem 生成并定期执行:index1rebld.sql, index2rebld.sql
  rem ORACLE数据库情况统计分析程序, 2001.02--2001.05, Ding Jugang
  rem 1、 参数dbwr_io_slave等三个从属进程可以分别设置为 40:12:6
  rem 2、 参数PRocess 一般是实际进程数据的1.5倍, 是为限制进程总数.
  rem process过大将降低系统的性能,可适当考虑降低该数值到600
  rem 3、 当LOG FILE SWITCH时出现等待时,建议加大REDO LOG FILE,一般是30分钟
  rem 一次切换。目前是128MB,配合4MB LOG_BUFFER已经可以了(8M也没用).
  rem 4、 在整个系统较繁忙时检测SHARED_POOL(一般情况下应该空余1/4)
  rem 5、 SGA应该小于整个物理内存的一半,太大会导致内存换页出现(PI/PO)
  rem 6、 MTS对于网站应用是理想选择,但在过分繁忙的客户端压力下,MTS会自动失败
  rem 而重启专用进程(例如过多的php连接),估计是应用类型不兼容。
  rem 7、 命令instat,vmstat,top,w 能从OS级评估系统负荷。
  rem 监测下面语句的执行结果的变化率,能得出数据库级的硬盘读写流量。
  rem select count(FILE#),sum(PHYRDS),sum(PHYWRTS),sum(PHYBLKRD) from sys.V_$filestat;
  rem================================================================
  rem 创建SQL跟踪表SQLS
  rem create table SQLS as select * from sys.v_$sqlarea where disk_reads>100;
  rem 择机(有性能怀疑时)执行跟踪, 并执行随后的查询:
  rem insert into sqls select * from sys.v_$sqlarea where disk_reads>10 and executions<10;
  rem 1)最高频率的SQL
  rem select disk_reads,executions,rows_processed,sql_text from SQLS
  rem where executions> 99 ;
  rem 2)查询性能最差的SQL:
  rem select disk_reads,executions,rows_processed,first_load_time,sql_text from SQLS
  rem order by first_load_time;
  select disk_reads,executions,rows_processed,first_load_time,sql_text
  from sys.v_$sqlarea where disk_reads>10 and executions <10 order by first_load_time;
  rem ========================================================================
  rem 567890123456789_1234567890123456789_1234567890123456789_1234567890123456
  rem ==RowCache,LibraryCache 依靠于Shared_pool,参看sys.v_$sgastat ===
  rem 此二者当达到95%,现已经达到99%
  select (sum(pins - reloads))/ sum(pins) "lib cache" from sys.v_$librarycache;
  select (sum(gets-getmisses-usage-fixed)) / sum(gets) from sys.v_$rowcache;
  rem ==================== SGA ==============================================
  rem ======= sys.v_$sgastat,SGA中具体说明 =====================
  rem 剩余共享池: 保留Free Memory 大于25%
  column name format A46
  column value format 999999,999,999
  select * from sys.v_$sgastat where rownum<5;
  rem =================== SYS ===============================================
  rem ========= sys.v_$SYSstat 具体列表,下面是几个指标的算法=======
  rem 数据缓冲命中率:1- 40#/(39#+38#) = 99.8% > 95%
  rem 内存排序成功率:1- 162#/(161#) = 99.4% > 92%
  rem 脏缓冲区平均长度(oracle8i已废除之): 41#/42#=0.06  rem 应用效率:全表扫描<1%, 140#long/(139#short+140#long)
  column class format 99999
  column value format 999999,999,999
  select * from sys.v_$sysstat
  where STATISTIC# in (38,39,40,41,42,43,139,140,141,106,161,162,163);
  rem 日志缓存要满足空间请求极小,每日300,还可更小:
  rem LOG_BUFFER=4MB,还可再大。

  rem select * from sys.v_$sysstat where name like 'redo%';
  rem
  rem ==================回滚段1==============================================
  rem 回滚段有效率:waits/gets<1% 即: 无等待命中率NoWait_Hit_Ratio接近于1
  rem 回滚段数据量在4---100个,同样规格大小,尽可能稳定不变。
  rem alter TABLESPACE RBS DEFAULT STORAGE
  rem (INITIAL 1M NEXT 1M MINEXTENTS 8 PCTINCREASE 0);
  rem CREATE PUBLIC ROLLBACK SEGMENT RB21 TABLESPACE RBS;
  rem ALTER ROLLBACK SEGMENT RB21 storage( minextents 4 optimal 8M);
  rem CREATE PUBLIC ROLLBACK SEGMENT RB55 TABLESPACE RBS2 storage(minextents 8 optimal 8M);
  column Ratios? format 99.9999
  select count(*), sum(waits)/sum(gets) from sys.v_$rollstat;
  rem rssize>=最优保留值optimal,shrinks是动态收缩次数,每小时2次是答应的。
  rem 例如,统计信息:rssize=8M,extents=8, waits和shrinks 小于天天2次
  select usn,extents,gets,writes,rssize,waits,shrinks
  from sys.v_$rollstat;
  rem =======****** 回滚段2 ****===========
  rem 这里是回滚段的汇总统计,分析其扩展段之定义。其命中率统计见v_$roolstat
  rem dba_rollback_segs 下面验证一致性。
  select owner,initial_extent,NEXT_EXTENT,Min_EXTENTS,MAX_EXTENTS,count(*),
  TABLESPACE_NAME,STATUS
  from dba_rollback_segs
  group by owner,initial_extent,NEXT_EXTENT,min_extents,
  MAX_EXTENTS,TABLESPACE_NAME,STATUS;
  rem dba_rollback_segs --> detail 定义
  column SEGMENT_name format a6
  column TABLESPACE_NAME format a12
  select SEGMENT_name,owner,initial_extent,NEXT_EXTENT,Min_EXTENTS,MAX_EXTENTS,
  TABLESPACE_NAME,STATUS from dba_rollback_segs;
  rem ======================== 闩 ===========================================
  rem 闩-Oracle内部锁,无等待命中率NoWait_Hit_Ratio接近于1
  
  column name format A30
  select name, immediate_gets "Imme_gets", immediate_misses "Imme_Mis",
  round(immediate_gets/(immediate_gets+immediate_misses),3) "nowait_hit_ratio"
  from sys.v_$latch where immediate_gets+immediate_misses != 0 order by name;
  rem 闩-Oracle内部锁,命中率HITRATIO(即misses/gets)接近于0
  rem sleeps, immediate_gets "Imme_gets", 为了易于阅览,不要换行
  column Mis/Get? format 99.999;
  select name, gets, misses, misses/gets "Mis/Get?",
  immediate_misses "Imme_Mis"
  from sys.v_$latch where gets > 0 order by name;
  rem ================== 系统等待 ===========================================
  rem 系统等待累计次数和累计时间
  column class format A30
  select * from sys.v_$waitstat ;
  column event format A28
  select * from sys.v_$system_event order by TOTAL_WAITS;
  rem ==============================================================
  rem ================ v_$session_wait ======================================
  rem 会话正在等待的累计时间seconds
  select event,sum(SECONDS_IN_WAIT),count(*)
  from sys.v_$session_wait group by event ;
  rem rdbms ipc message 3648 6
  rem slave wait 31501 41 太大?
  rem===================================================================rem 会话等待的累计次数和累计时间
  column event format A32
  select event,type, sum(total_waits) "Waits",
  sum(time_waited) "SumTime",count(*) "Count"
  from sys.v_$session s, sys.v_$session_event e
  where s.sid = e.sid
  group by event,type
  order by type ;

  rem =================================================================
  rem 这里是日志文件和控制

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表