数据库进阶:数据库管理员日常工作中必备的sql列表

发表于:2008-05-06来源:作者:点击数: 标签:sqlSQLSql日常工作数据库管理员
数据库 管理员日常工作中必备的 sql 列表: --监控索引是否使用 alter index index_name monitoring usage; alter index index_name nomonitoring usage; select * from v$object_usage where index_name = index_name; --求数据文件的I/O分布 select df.nam
 数据库管理员日常工作中必备的sql列表:

  --监控索引是否使用

  alter index &index_name monitoring usage;
  alter index &index_name nomonitoring usage;
  select * from v$object_usage where index_name = &index_name;

  --求数据文件的I/O分布

  select df.name,phyrds,phywrts,phyblkrd,phyblkwrt,singleblkrds,readtim,writetim
  from v$filestat fs,v$dbfile df
  where fs.file#=df.file# order by df.name;

  --求某个隐藏参数的值

  col ksppinm format a54
  col ksppstvl format a54
  select ksppinm, ksppstvl
  from x$ksppi pi, x$ksppcv cv
  where cv.indx=pi.indx and pi.ksppinm like '\_%' escape '\' and pi.ksppinm like '%meer%';

  --求系统中较大的latch

  select name,sum(gets),sum(misses),sum(sleeps),sum(wait_time)
  from v$latch_children
  group by name having sum(gets) > 50 order by 2;

  --求归档日志的切换频率(生产系统可能时间会很长)

  select start_recid,start_time,end_recid,end_time,minutes from (select test.*, rownum as rn
  from (select b.recid start_recid,to_char(b.first_time,'yyyy-mm-dd hh24:mi:ss') start_time,
  a.recid end_recid,to_char(a.first_time,'yyyy-mm-dd hh24:mi:ss') end_time,round(((a.first_time-b.first_time)*24)*60,2) minutes
  from v$log_history a,v$log_history b where a.recid=b.recid+1 and b.first_time > sysdate - 1
  order by a.first_time desc) test) y where y.rn < 30

  --求回滚段正在处理的事务

  select a.name,b.xacts,c.sid,c.serial#,d.sql_text
  from v$rollname a,v$rollstat b,v$session c,v$sqltext d,v$transaction e
  where a.usn=b.usn and b.usn=e.xidusn and c.taddr=e.addr
  and c.sql_address=d.address and c.sql_hash_value=d.hash_value order by a.name,c.sid,d.piece;

  --求出无效的对象

  select 'alter procedure '||object_name||' compile;'
  from dba_objects
  where status='INVALID' and wner='&' and object_type in ('PACKAGE','PACKAGE BODY');
  /
  select owner,object_name,object_type,status from dba_objects where status='INVALID';

  --求process/session的状态

  select p.pid,p.spid,s.program,s.sid,s.serial#
  from v$process p,v$session s where s.paddr=p.addr;

  --求当前session的状态

  select sn.name,ms.value
  from v$mystat ms,v$statname sn
  where ms.statistic#=sn.statistic# and ms.value > 0;

  --求表的索引信息

  select ui.table_name,ui.index_name
  from user_indexes ui,user_ind_columns uic
  where ui.table_name=uic.table_name and ui.index_name=uic.index_name
  and ui.table_name like '&table_name%' and uic.column_name='&column_name';

  --显示表的外键信息

  col search_condition format a54
  select table_name,constraint_name
  from user_constraints
  where constraint_type ='R' and constraint_name in (select constraint_name from user_cons_columns where column_name='&1');
  select rpad(child.table_name,25,' ') child_tablename,
  rpad(cp.column_name,17,' ') referring_column,rpad(parent.table_name,25,' ') parent_tablename,
  rpad(pc.column_name,15,' ') referred_column,rpad(child.constraint_name,25,' ') constraint_name
  from user_constraints child,user_constraints parent,
  user_cons_columns cp,user_cons_columns pc
  where child.constraint_type = 'R' and child.r_constraint_name = parent.constraint_name and
  child.constraint_name = cp.constraint_name and parent.constraint_name = pc.constraint_name and
  cp.position = pc.position and child.table_name ='&table_name'
  order by child.owner,child.table_name,child.constraint_name,cp.position;

  --显示表的分区及子分区(user_tab_subpartitions)

  col table_name format a16
  col partition_name format a16
  col high_value format a81
  select table_name,partition_name,HIGH_VALUE from user_tab_partitions where table_name='&table_name'

  --使用dbms_xplan生成一个执行计划

  explain plan set statement_id = '&sql_id' for &sql;
  select * from table(dbms_xplan.display);

  --求某个事务的重做信息(bytes)

  select s.name,m.value
  from v$mystat m,v$statname s
  where m.statistic#=s.statistic# and s.name like '%redo size%';

  --求cache中缓存超过其5%的对象

  select o.owner,o.object_type,o.object_name,count(b.objd)
  from v$bh b,dba_objects o
  where b.objd = o.object_id
  group by o.owner,o.object_type,o.object_name
  having count(b.objd) > (select to_number(value)*0.05 from v$parameter where name = 'db_block_buffers');

  --求谁阻塞了某个session(10g)

  select sid, username, event, blocking_session,
  seconds_in_wait, wait_time
  from v$session where state in ('WAITING') and wait_class != 'Idle';

  --求session的OS进程ID

  col program format a54
  select p.spid "OS Thread", b.name "Name-User", s.program
  from v$process p, v$session s, v$bgprocess b
  where p.addr = s.paddr and p.addr = b.paddr
  UNION ALL
  select p.spid "OS Thread", s.username "Name-User", s.program
  from v$process p, v$session s where p.addr = s.paddr and s.username is not null;

  --查会话的阻塞

  col user_name format a32
  select /*+ rule */ lpad(' ',decode(l.xidusn ,0,3,0))||l.oracle_username user_name, o.owner,o.object_name,s.sid,s.serial#
  from v$locked_object l,dba_objects o,v$session s
  where l.object_id=o.object_id and l.session_id=s.sid order by o.object_id,xidusn desc ;
  col username format a15
  col lock_level format a8
  col owner format a18
  col object_name format a32
  select /*+ rule */ s.username, decode(l.type,'tm','table lock', 'tx','row lock', null) lock_level, o.owner,o.object_name,s.sid,s.serial#
  from v$session s,v$lock l,dba_objects o
  where l.sid = s.sid and l.id1 = o.object_id(+) and s.username is not null ;

  --求等待的事件及会话信息/求会话的等待及会话信息

  select se.sid,s.username,se.event,se.total_waits,se.time_waited,se.average_wait
  from v$session s,v$session_event se
  where s.username is not null and se.sid=s.sid and s.status='ACTIVE' and se.event not like '%SQL*Net%' order by s.username;
  select s.sid,s.username,sw.event,sw.wait_time,sw.state,sw.seconds_in_wait
  from v$session s,v$session_wait sw
  where s.username is not null and sw.sid=s.sid and sw.event not like '%SQL*Net%' order by s.username;

原文转自:http://www.ltesting.net