ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Oracle数据库DBA日常Sql列表

Oracle数据库DBA日常Sql列表

原创 Linux操作系统 作者:ma_vicky 时间:2011-03-24 11:13:17 0 删除 编辑


3N3z!vgLRe9EhO3Ip0--监控索引是否使用

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_hashvalue=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;

--求会话等待的file_id/block_id

col event format a24

col p1text format a12

col p2text format a12

col p3text format a12

select sid,event,p1text, p1, p2text, p2, p3text, p3

from v$session_wait

where event not like '%SQL%' and event not like '%rdbms%' and event not like '%mon%' order by event;

select name,wait_time from v$latch l where exists (select 1 from (select sid,event,p1text, p1, p2text, p2, p3text, p3

from v$session_wait

where event not like '%SQL%' and event not like '%rdbms%' and event not like '%mon%'

) x where x.p1= l.latch#);

--求会话等待的对象

col owner format a18

col segment_name format a32

col segment_type format a32

select owner,segment_name,segment_type

from dba_extents

where file_id = &file_id and &block_id between block_id and block_id + blocks - 1;

--求buffer cache中的块信息

select o.OBJECT_TYPE, substr(o.OBJECT_NAME,1,10) objname , b.objd , b.status, count(b.objd)

from v$bh b, dba_objects o

where b.objd = o.data_object_id and o.owner = '&1' group by o.object_type, o.object_name,b.objd, b.status ;

--求日志文件的空间使用

select le.leseq current_log_sequence#, 100*cp.cpodr_bno/le.lesiz percentage_full

from x$kcccp cp,x$kccle le

where le.leseq =cp.cpodr_seq;

--求等待中的对象

select /*+rule */ s.sid, s.username, w.event, o.owner, o.segment_name, o.segment_type,

o.partition_name, w.seconds_in_wait seconds, w.state

from v$session_wait w, v$session s, dba_extents o

where w.event in (select name from v$event_name where parameter1 = 'file#'

and parameter2 = 'block#' and name not like 'control%')

and o.owner <> 'sys' and w.sid = s.sid and w.p1 = o.file_id and w.p2 >= o.block_id and w.p2 < o.block_id + o.blocks

--求当前事务的重做尺寸

select value

from v$mystat, v$statname

where v$mystat.statistic# = v$statname.statistic# and v$statname.name = 'redo size';

--唤醒smon去清除临时段

column pid new_value Smon

set termout off

select p.pid from sys.v_$bgprocess b,sys.v_$process p where b.name = 'SMON' and p.addr = b.paddr

/

set termout on

oradebug wakeup &Smon

undefine Smon

--求回退率

select b.value/(a.value + b.value),a.value,b.value from v$sysstat a,v$sysstat b

where a.statistic#=4 and b.statistic#=5;

--求DISK READ较多的SQL

select st.sql_text from v$sql s,v$sqltext st

where s.address=st.address and s.hashvalue=st.hash_value and s.disk_reads > 300;

--求DISK SORT严重的SQL

select sess.username, sql.sql_text, sort1.blocks

from v$session sess, v$sqlarea sql, v$sort_usage sort1

where sess.serial# = sort1.session_num

and sort1.sqladdr = sql.address

and sort1.sqlhash = sql.hash_value and sort1.blocks > 200;

--求对象的创建代码

column column_name format a36

column sql_text format a99

select dbms_metadata.get_ddl('TABLE','&1') from dual;

select dbms_metadata.get_ddl('INDEX','&1') from dual;

--求表的索引

set linesize 131

select a.index_name,a.column_name,b.status, b.index_type

from user_ind_columns a,user_indexes b

where a.index_name=b.index_name and a.table_name='&1';

求索引中行数较多的

select index_name,blevel,num_rows,CLUSTERING_FACTOR,status from user_indexes where num_rows > 10000 and blevel > 0

select table_name,index_name,blevel,num_rows,CLUSTERING_FACTOR,status from user_indexes where status <> 'VALID'

--求当前会话的SID,SERIAL#

select sid, serial# from v$session where audsid = SYS_CONTEXT('USERENV','SESSIONID');

--求表空间的未用空间

col mbytes format 9999.9999

select tablespace_name,sum(bytes)/1024/1024 mbytes from dba_free_space group by tablespace_name;

--求表中定义的触发器

select table_name,index_type,index_name,uniqueness from user_indexes where table_name='&1';

select trigger_name from user_triggers where table_name='&1';

--求未定义索引的表

select table_name from user_tables where table_name not in (select table_name from user_ind_columns);

--执行常用的过程

exec print_sql('select count(*) from tab');

exec show_space2('table_name');

--求free memory

select * from v$sgastat where name='free memory';

select a.name,sum(b.value) from v$statname a,v$sesstat b where a.statistic# = b.statistic# group by a.name;

查看一下谁在使用那个可以得回滚段,或者查看一下某个可以得用户在使用回滚段,

找出领回滚段不断增长的事务,再看看如何处理它,是否可以将它commit,再不行

就看看能否kill它,等等,查看当前正在使用的回滚段的用户信息和回滚段信息:

set linesize 121

SELECT r.name "ROLLBACK SEGMENT NAME ",l.sid "ORACLE PID",p.spid "SYSTEM PID ",s.username "ORACLE USERNAME"

FROM v$lock l, v$process p, v$rollname r, v$session s

WHERE l.sid = p.pid(+) AND s.sid=l.sid AND TRUNC(l.id1(+)/65536) = r.usn AND l.type(+) = 'TX' AND l.lmode(+) = 6 ORDER BY r.name;

--查看用户的回滚段的信息

select s.username, rn.name from v$session s, v$transaction t, v$rollstat r, v$rollname rn

where s.saddr = t.ses_addr and t.xidusn = r.usn and r.usn = rn.usn

--生成执行计划

explain plan set statement_id='a1' for &1;

--查看执行计划

select lpad(' ',2*(level-1))||operation operation,options,OBJECT_NAME,position from plan_table

start with id=0 and statement_id='a1' connect by prior id=parent_id and statement_id='a1'

执行计划ITPUB个人空间jns}|7P{
   1)根据SID,从v$sql中找到相应SQL的HASH_VALUE和ADDRESS ;ITPUB个人空间c1aY(uqF#u
   SELECT a.sql_text , a.address , a.hash_valueITPUB个人空间T?:OnS/E0Z
   FROM   v$sql a , v$session b
+D{C g U_?$h0   where  a.hash_value = b.sql_hash_valueITPUB个人空间L|8Omnn3@ w#A
   and    b.sid = &sid ;ITPUB个人空间ma1b+Tuog.P l
Alan Lee(160921) 22:58:07ITPUB个人空间#o-l~XdYh
 2)根据hash_value和address的值,从v$sql_plan中找到真实的执行计划。ITPUB个人空间z'T pJ;u,W y
   set line 200;ITPUB个人空间!j!l9ACc4F'm
   col oper format a100;
m)Uy |'r0   select lpad(oper,length(oper)+level*2,' ') oper,cost
(aYT,@J)g!@0   from   (ITPUB个人空间^+]g:a'S4mHY
           select object_name||':'||operation||' '||options as oper,cost,id,parent_idITPUB个人空间:rv;j-f??#}
           from   v$sql_plan
K5^f5?xe:T0           where  hash_value = &hash_valueITPUB个人空间 we0jo9rk;B0^
           and    address = '&address'
C'j.p t Lvp0          )
#f0Z ?x3e0JH?1d0   start with id=0
Q c$Tq5d RMh1f8d0   connect by prior id = parent_id;
({\ I Lq){9]&Y;\0Alan Lee(160921) 22:58:26
a#y$g(K2`0H| u0这2步,就可以找出实际正在跑的SQL使用的是什么执行计划

set autotrace traceonly statisticsITPUB个人空间B`6XslZv(q
set autotrace traceonly explain
V@BC2[;{`G0set autotrace traceonly on explain

--查看内存中存的使用

select decode(greatest(class,10),10,decode(class,1,'Data',2,'Sort',4,'Header',to_char(class)),'Rollback') "Class",

sum(decode(bitand(flag,1),1,0,1)) "Not Dirty",sum(decode(bitand(flag,1),1,1,0)) "Dirty",

sum(dirty_queue) "On Dirty",count(*) "Total"

from x$bh group by decode(greatest(class,10),10,decode(class,1,'Data',2,'Sort',4,'Header',to_char(class)),'Rollback');

--查看表空间状态

select tablespace_name,extent_management,segment_space_management from dba_tablespaces;

select table_name,freelists,freelist_groups from user_tables;

--查看系统请求情况

SELECT DECODE (name, 'summed dirty write queue length', value)/

DECODE (name, 'write requests', value) "Write Request Length"

FROM v$sysstat WHERE name IN ( 'summed dirty queue length', 'write requests') and value>0;

--计算data buffer命中率

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# = 40 and b.statistic# = 41 and c.statistic# = 42;

SELECT name, (1-(physical_reads/(db_block_gets+consistent_gets)))*100 H_RATIO FROM v$buffer_pool_statistics;

--查看内存使用情况

select least(max(b.value)/(1024*1024),sum(a.bytes)/(1024*1024)) shared_pool_used,

max(b.value)/(1024*1024) shared_pool_size,greatest(max(b.value)/(1024*1024),sum(a.bytes)/(1024*1024))-

(sum(a.bytes)/(1024*1024)) shared_pool_avail,((sum(a.bytes)/(1024*1024))/(max(b.value)/(1024*1024)))*100 avail_pool_pct

from v$sgastat a, v$parameter b where (a.pool='shared pool' and a.name not in ('free memory')) and b.name='shared_pool_size';

--查看用户使用内存情况

select username, sum(sharable_mem), sum(persistent_mem), sum(runtime_mem)

from sys.v_$sqlarea a, dba_users b

where a.parsing_user_id = b.user_id group by username;

--查看对象的缓存情况

select OWNER,NAMESPACE,TYPE,NAME,SHARABLE_MEM,LOADS,EXECUTIONS,LOCKS,PINS,KEPT

from v$db_object_cache where type not in ('NOT LOADED','NON-EXISTENT','VIEW','TABLE','SEQUENCE')

and executions>0 and loads>1 and kept='NO' order by owner,namespace,type,executions desc;

select type,count(*) from v$db_object_cache group by type;

--查看库缓存命中率

select namespace,gets, gethitratio*100 gethitratio,pins,pinhitratio*100 pinhitratio,RELOADS,INVALIDATIONS from v$librarycache

--查看某些用户的hash

select a.username, count(b.hash_value) total_hash,count(b.hash_value)-count(unique(b.hash_value)) same_hash,

(count(unique(b.hash_value))/count(b.hash_value))*100 u_hash_ratio

from dba_users a, v$sqlarea b where a.user_id=b.parsing_user_id group by a.username;

--查看字典命中率

select (sum(getmisses)/sum(gets)) ratio from v$rowcache;

--查看undo段的使用情况

SELECT d.segment_name,extents,optsize,shrinks,aveshrink,aveactive,d.status

FROM v$rollname n,v$rollstat s,dba_rollback_segs d

WHERE d.segment_id=n.usn(+) and d.segment_id=s.usn(+);

--无效的对象

select owner,object_type,object_name from dba_objects where status='INVALID';

select constraint_name,table_name from dba_constraints where status='INVALID';

--求出某个进程,并对它进行跟踪

select s.sid,s.serial# from v$session s,v$process p where s.paddr=p.addr and p.spid=&1;

exec dbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,true);

exec dbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,false);

--求出锁定的对象

select do.object_name,session_id,process,locked_mode

from v$locked_object lo, dba_objects do where lo.object_id=do.object_id;

--求当前session的跟踪文件

SELECT p1.value || '/' || p2.value || '_ora_' || p.spid || '.ora' filename

FROM v$process p, v$session s, v$parameter p1, v$parameter p2

WHERE p1.name = 'user_dump_dest' AND p2.name = 'instance_name'

AND p.addr = s.paddr AND s.audsid = USERENV('SESSIONID') AND p.background is null AND instr(p.program,'CJQ') = 0;

--求对象所在的文件及块号

select segment_name,header_file,header_block

from dba_segments where segment_name like '&1';

--求对象发生事务时回退段及块号

select a.segment_name,a.header_file,a.header_block

from dba_segments a,dba_rollback_segs b

where a.segment_name=b.segment_name and b.segment_id='&1'

--9i的在线重定义表

/*如果在线重定义的表没有主键需要创建主键*/

exec dbms_redefinition.can_redef_table('cybercafe','announcement');

create table anno2 as select * from announcement

exec dbms_redefinition.start_redef_table('cybercafe','announcement','anno2');

exec dbms_redefinition.sync_interim_table('cybercafe','announcement','anno2');

exec dbms_redefinition.finish_redef_table('cybercafe','announcement','anno2');

drop table anno2

exec dbms_redefinition.abort_redef_table('cybercafe','announcement','anno2');

--常用的logmnr脚本(cybercafe)

exec sys.dbms_logmnr_d.build(dictionary_filename =>'esal',dictionary_location =>'/home/oracle/logmnr');

exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_24050.dbf', ptions=>sys.dbms_logmnr.new);

exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22912.dbf', ptions=>sys.dbms_logmnr.addfile);

exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22913.dbf', ptions=>sys.dbms_logmnr.addfile);

exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22914.dbf', ptions=>sys.dbms_logmnr.addfile);

exec sys.dbms_logmnr.start_logmnr(dictfilename=>'/home/oracle/logmnr/esal.ora');

create table logmnr2 as select * from v$logmnr_contents;

死锁问题:1)查找死锁的进程:ITPUB个人空间(j0loW!A(z|-{i P'O w
sqlplus "/as sysdba"ITPUB个人空间\+{t-Z^4AA-z
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,l.ORACLE_USERNAME,
:NZO` JB*kU'A0l.OS_USER_NAME,l.PROCESS FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;


jT#\J!p3e;l9Aj02)kill掉这个死锁的进程:ITPUB个人空间"n/p'uQvj}
alter system kill session 'sid,serial#'; (其中sid=l.session_id)ITPUB个人空间*{*Y~Py&t_$_e2F
3)如果还不能解决,
#EUzt.kWJ/Yb0select pro.spid from v$session ses,v$process pro where ses.sid=XX and ses.paddr=pro.addr;ITPUB个人空间M/q TIV:u:z
其中sid用死锁的sid替换。
2wo!Bg7D0CUqT"V e0exit


I6S(IF|,W:a X0--与权限相关的字典

ALL_COL_PRIVS表示列上的授权,用户和PUBLIC是被授予者

ALL_COL_PRIVS_MADE表示列上的授权,用户是属主和被授予者

ALL_COL_RECD表示列上的授权,用户和PUBLIC是被授予者

ALL_TAB_PRIVS表示对象上的授权,用户是PUBLIC或被授予者或用户是属主

ALL_TAB_PRIVS_MADE表示对象上的权限,用户是属主或授予者

ALL_TAB_PRIVS_RECD表示对象上的权限,用户是PUBLIC或被授予者

DBA_COL_PRIVS数据库列上的所有授权

DBA_ROLE_PRIVS显示已授予用户或其他角色的角色

DBA_SYS_PRIVS已授予用户或角色的系统权限

DBA_TAB_PRIVS数据库对象上的所有权限

ROLE_ROLE_PRIVS显示已授予用户的角色

ROLE_SYS_PRIVS显示通过角色授予用户的系统权限

ROLE_TAB_PRIVS显示通过角色授予用户的对象权限

SESSION_PRIVS显示用户现在可利用的所有系统权限

USER_COL_PRIVS显示列上的权限,用户是属主、授予者或被授予者

USER_COL_PRIVS_MADE显示列上已授予的权限,用户是属主或授予者

USER_COL_PRIVS_RECD显示列上已授予的权限,用户是属主或被授予者

USER_ROLE_PRIVS显示已授予给用户的所有角色

USER_SYS_PRIVS显示已授予给用户的所有系统权限

USER_TAB_PRIVS显示已授予给用户的所有对象权限

USER_TAB_PRIVS_MADE显示已授予给其他用户的对象权限,用户是属主

USER_TAB_PRIVS_RECD显示已授予给其他用户的对象权限,用户是被授予者

--如何用dbms_stats分析表及模式?

exec dbms_stats.gather_schema_stats(ownname=>'&USER_NAME',estimate_percent=>dbms_stats.auto_sample_size,

method_opt => 'for all columns size auto',degree=> DBMS_STATS.DEFAULT_DEGREE);

exec dbms_stats.gather_schema_stats(ownname=>'&USER_NAME',estimate_percent=>dbms_stats.auto_sample_size,cascade=>true);

/*

FOR ALL [INDEXED | HIDDEN] COLUMNS [size_clause]

FOR COLUMNS [size clause] column|attribute [size_clause] [,column|attribute [size_clause]...],

where size_clause is defined as size_clause := SIZE {integer | REPEAT | AUTO | SKEWONLY}

integer--Number of histogram buckets. Must be in the range [1,254].

REPEAT--Collects histograms only on the columns that already have histograms.

AUTO--Oracle determines the columns to collect histograms based on data distribution and the workload of the columns.

SKEWONLY--Oracle determines the columns to collect histograms based on the data distribution of the columns

*/

 常用系统表,视图和作用
V s/J qXt3z:F6a Z:P0查看有关用户的信息:dba_usersITPUB个人空间ph:L nN:]lL
查看有关角色的信息:dba_roles,dba_role_privs,role_sys_privs
D&b"D KJ7T8h'{0查看有关系统权限的信息:dba_sys_privs
s3b.I_3Dk+Ev0查看当前数据库表空间状况:dba_tablespaces
6Cr,SDvG6k c0查看用户的系统权限:user_sys_privs
(@S7hj/? U0查看某个用户对另外一个用户授予的权限:user_tab_privs_madeITPUB个人空间s&l~w6G
查看某个用户对另外一个用户授予的列级权限:user_col_privs_made
0^"gR-Qq]0x{0查看某个用户接受的权限:user_tab_privs_recd
cj+aVn0查看某个用户接受的列级权限:user_col_privs_recdITPUB个人空间2cTwg[UU
查看有关用户的角色信息:user_role_privsITPUB个人空间'^sQ.z(DK&lH-W8D/U
查看有关授予某个角色的系统权限信息:role_sys_privsITPUB个人空间;t u!`4h9Ow8A/d
查看有关授予某个角色的对象权限信息:role_tab_privs
i h$}%wn2U0查看当前用户所拥有的表信息:user_tables
fh)v?M#n0查看当前用户有权限访问的表信息:all_tables
~X|;P F5O8?Cg w0查看当前用户所拥有的所有表的列信息:user_tab_columns
oDOy4@3LK)G3m;q0查看当前用户可以访问的表中的列信息:all_tab_columns
/I2L\ N+FtP0查看当前用户所拥有的所有约束信息:user_constraint
,k:WLQ^5IP$E,\._8B]0查看当前用户所拥有的所有约束和列的关系:user_cons_constraintITPUB个人空间p [%tY^;K
查看表中注释内容:user_tab_comments
8Bl;da2` \0查看表中列注释内容:user_col_commentsITPUB个人空间0^ _-m/~1JQh
提供练习的表:dualITPUB个人空间;F?4}:AOF
查看相关时区的名称和简称:v$timezone_names

V$OPTION:显示已安装的Oracle选项ITPUB个人空间n,L-wj1o0ZN-gx.c
select * from v$option;ITPUB个人空间6fm5_Um,X
取得Oracle版本的详细信息
)H F ~(e5_8Q!J0select * from v$version;
0Wee pr;x#i0取得初始化参数的详细信息
Z,@ { D:FZb0select name,value,description from v$parameter;
5Nq$q {-U0取得当前例程的详细信息ITPUB个人空间xD.[2If
select * from v$instance;


ise R;F'g0t*w5Ko01、用户

  查看当前用户的缺省表空间

  SQL>select username,default_tablespace from user_users;

  查看当前用户的角色

  SQL>select * from user_role_privs;

  查看当前用户的系统权限和表级权限

  SQL>select * from user_sys_privs;或
"k4A h(T v9UG0select  username,  default_tablespace,  temporary_tablespace, priv granted_role,  default_role  from dba_users u,      (select grantee,granted_role priv,default_role           from dba_role_privs          union all         select grantee,privilege  priv,''           from dba_sys_privs c       ) r where u.username = r.grantee order by username ;

  SQL>select * from user_tab_privs;

  显示当前会话所具有的权限

  SQL>select * from session_privs;

  显示指定用户所具有的系统权限

  SQL>select * from dba_sys_privs where grantee='GAME';

  2、表

  查看用户下所有的表

  SQL>select * from user_tables;

  查看名称包含log字符的表

  SQL>select object_name,object_id from user_objects

  where instr(object_name,'LOG')>0;

  查看某表的创建时间

  SQL>select object_name,created from user_objects where object_name=upper('&table_name');

  查看某表的大小

  SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

  where segment_name=upper('&table_name');

  查看放在ORACLE的内存区里的表

  SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;

  3、索引

  查看索引个数和类别

  SQL>select index_name,index_type,table_name from user_indexes order by table_name;

  查看索引被索引的字段

  SQL>select * from user_ind_columns where index_name=upper('&index_name');

  查看索引的大小

  SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments

  where segment_name=upper('&index_name');

  4、序列号

  查看序列号,last_number是当前值

  SQL>select * from user_sequences;

  5、视图

  查看视图的名称

  SQL>select view_name from user_views;

  查看创建视图的select语句

  SQL>set view_name,text_length from user_views;

  SQL>set long 2000; 说明:可以根据视图的text_length值设定set long 的大小

  SQL>select text from user_views where view_name=upper('&view_name');

  6、同义词

  查看同义词的名称

  SQL>select * from user_synonyms;

  7、约束条件

  查看某表的约束条件

  SQL>select constraint_name, constraint_type,search_condition, r_constraint_name

  from user_constraints where table_name = upper('&table_name');

  SQL>select c.constraint_name,c.constraint_type,cc.column_name

  from user_constraints c,user_cons_columns cc

  where c.owner = upper('&table_owner') and c.table_name = upper('&table_name')

  and c.owner = cc.owner and c.constraint_name = cc.constraint_name

  order by cc.position;

  8、存储函数和过程

  查看函数和过程的状态

  SQL>select object_name,status from user_objects where object_type='FUNCTION';

  SQL>select object_name,status from user_objects where object_type='PROCEDURE';

  查看函数和过程的源代码

  SQL>select text from all_source where wner=user and name=upper('&plsql_name');

1. 监控事例的等待
)Y&Nf9^I f0  select   event,ITPUB个人空间+{E'n-n&}jER8yVC\
    sum(decode(wait_Time,0,0,1)) "Prev", ITPUB个人空间r?#]Op7}G'S0lD
    sum(decode(wait_Time,0,1,0)) "Curr",
Mw6sp+a*ByiC|0    count(*) "Tot" ITPUB个人空间8^M7}:h-^ e h_B4x
       from  v$session_Wait ITPUB个人空间G:U+u0lfv(j6f:hu
       group by event 
,i&n9Z&e:OW%| t0       order by 4;ITPUB个人空间Ei$[z;t9f'L&{6j$j
  ITPUB个人空间\:j]3Ad\
2. 回滚段的争用情况ITPUB个人空间\ C+W&jGHD"T
      select   name, waits, gets, waits/gets "Ratio" ITPUB个人空间B^{9k[L
       from  v$rollstat a, v$rollname b ITPUB个人空间G.W~/IExT?'OrO
       where  a.usn = b.usn; 
e~(Y6d Wa*A`0  ITPUB个人空间3XG-cAC(S`(}.[;V
3. 监控表空间的 I/O 比例
#j3oh!]!Tt |D'V0      select   df.tablespace_name name,df.file_name "file",f.phyrds pyr,ITPUB个人空间W#^#m;C)}.E
        f.phyblkrd pbr,f.phywrts pyw, f.phyblkwrt pbw
`sc5E;m0       from  v$filestat f, dba_data_files df
&x:\[ j8W%nVWN+qb0       where  f.file# = df.file_id
A5@L"bE0       order by df.tablespace_name;
&JR{usY7jH]-~%w s U0  
v*Q \q5BP:? Q04. 监控文件系统的 I/O 比例
y5B-djI4}^4v0      select   substr(a.file#,1,2) "#", substr(a.name,1,30) "Name", ITPUB个人空间B#l)\+P(O;kp
        a.status, a.bytes, b.phyrds, b.phywrts ITPUB个人空间C0m.xZ[:tH
       from  v$datafile a, v$filestat b 
%SCe*dqDT0       where  a.file# = b.file#; ITPUB个人空间xn vPoQ { n+q
  
La6b @2\-Q05.在某个用户下找所有的索引
)J2c3l$T;n,v3u!`-fx.|4v6j0      select   user_indexes.table_name, ITPUB个人空间(pI']"E8TbWp
        user_indexes.index_name,ITPUB个人空间 D9G%I?t6\b
        uniqueness, 
} FCv7W;^ n'E5j0        column_nameITPUB个人空间7Ni i!Rx3u'I ll.M
       from  user_ind_columns, user_indexesITPUB个人空间2vZlG-j;U)c E
       where  user_ind_columns.index_name = user_indexes.index_name and ITPUB个人空间7X&W!~k tC.@9O!EF(B4xW
        user_ind_columns.table_name = user_indexes.table_name ITPUB个人空间 p7O:B*Vt I
       order by user_indexes.table_type, user_indexes.table_name,
0v%_6f2y#h#~!p u{&J0        user_indexes.index_name, column_position;ITPUB个人空间6cs R0]-CH7Rh
6. 监控 SGA 的命中率
jeZ9GT0      select   a.value + b.value "logical_reads", ITPUB个人空间)SU c h7|T+c,fH-j;j
        c.value "phys_reads",
&I g%T%EE0PB0        round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) "BUFFER HIT RATIO" ITPUB个人空间F6s\ O.o
       from  v$sysstat a, v$sysstat b, v$sysstat cITPUB个人空间!m ~'Wi&h&Y/W'H k^
       where  a.statistic# = 38 and b.statistic# = 39 and 
U2S9{xn8BO~,A:p0        c.statistic# = 40; 
h:\!l Ini0  ITPUB个人空间OYJtQ*CU
7. 监控 SGA 中字典缓冲区的命中率ITPUB个人空间 l\.w"UYy qA6D c
      select   parameter, gets,Getmisses , 
"B8WS)B%e0S&X0        getmisses/(gets+getmisses)*100 "miss ratio",
QI#c8S%l$N0        (1-(sum(getmisses)/ (sum(gets)+sum(getmisses))))*100 "Hit ratio"
x9^,e)EYG6xc8X^0       from  v$rowcache ITPUB个人空间(S/k_9L*BEt
       where  gets+getmisses <>0ITPUB个人空间#G l].?^Ll%Ux
       group by parameter, gets, getmisses; 
G.q.p^!f ^+qd0  
Z:iLux5Drv08. 监控 SGA 中共享缓存区的命中率,应该小于1%ITPUB个人空间&BC/^2R)m1|,xpK
      select   sum(pins) "Total Pins", sum(reloads) "Total Reloads",
1y MD&^bRO|0        sum(reloads)/sum(pins) *100 libcacheITPUB个人空间GZ3Q]&D e;z)iE
       from  v$librarycache;ITPUB个人空间"r P4tea M0h;~&l
      select   sum(pinhits-reloads)/sum(pins) "hit radio",ITPUB个人空间2y vM]4n*b
        sum(reloads)/sum(pins) "reload percent" 
+rrrK {U0       from  v$librarycache;ITPUB个人空间{RF#` D2xW
  ITPUB个人空间K Y`P0]C E
9. 显示所有数据库对象的类别和大小ITPUB个人空间9? ll]-jQI5E
      select   type,
`)I k+Bl&Lk7h7N%B6me g0        count(name) num_instances,ITPUB个人空间-ci:a9U K-a j/["x
        sum(source_size) source_size,ITPUB个人空间h5asi$NA
        sum(parsed_size) parsed_size,
W sl%k!uO_e0        sum(code_size) code_size,
rB6kl)F0        sum(error_size) error_size,
&GXt-~h;I%~0        sum(source_size) +sum(parsed_size) +sum(code_size) +sum(error_size) size_required 
g!d+O D&SL j I0       from  dba_object_size 
jA1q{#Q+_GRYqZ0       group by type ITPUB个人空间-g(H&?MD#G ^
       order by 1;ITPUB个人空间KT E vG\h
  
WW t y0g)u010. 监控 SGA 中重做日志缓存区的命中率,应该小于1%
/I { y F"S1O E0      SELECT   name, gets, misses, immediate_gets, immediate_misses,
*`0y3|cs'?0        Decode(gets,0,0,misses/gets*100) ratio1,
JryY?f0        Decode(immediate_gets+immediate_misses,0,0,ITPUB个人空间!l"rl3x'va*d
         immediate_misses/(immediate_gets+immediate_misses)*100) ratio2
L}x@3Zq O q0       FROM  v$latch ITPUB个人空间 _3|XPc't5@V
       WHERE  name IN ('redo allocation', 'redo copy'); ITPUB个人空间:G^lSw(hA y
  
3Gt O}f"L/n7XE011.  监控内存和硬盘的排序比率,最好使它小于 .10,增加 sort_area_size 
T;[\"kyo.R0      SELECT   name, value 
6yx3s]|Ex.z6s%S0       FROM  v$sysstat 
5r J"_:G*rF1z0       WHERE  name IN ('sorts (memory)', 'sorts (disk)'); 
{wd?]ti&c8lp0  
6c1`Z:}m3M^cT~O8c012. 监控当前数据库谁在运行什么SQL语句
D!T!O'S,h$Z"I0      SELECT   osuser, username, sql_text 
alhDe{uO0       from  v$session a, v$sqltext bITPUB个人空间 D,k3\Hj7k9T
       where  a.sql_address =b.address order by address, piece;
t~ps)hv/Kfr M l0  
FYx9h&v/N? _8q013. 监控字典缓冲区ITPUB个人空间]6f1hS9LNk}*j
      SELECT   (SUM(PINS - RELOADS)) / SUM(PINS) "LIB CACHE" ITPUB个人空间\d K@t1g
       FROM  V$LIBRARYCACHE;ITPUB个人空间5[E/x m+R
      SELECT   (SUM(GETS - GETMISSES - USAGE - FIXED)) / SUM(GETS) "ROW CACHE" 
!{2pnT6H L{!}0       FROM  V$ROWCACHE;
6]:Qw3tE?nsU(S0      SELECT   SUM(PINS) "EXECUTIONS", SUM(RELOADS) "CACHE MISSES WHILE EXECUTING" ITPUB个人空间 ?%d#Kyr$l#y h
       FROM  V$LIBRARYCACHE; 
1H` {@,Z\&E e^j2H0     后者除以前者,此比率小于1%,接近0%为好。ITPUB个人空间7^ Q:h wfW%bD4S
      SELECT   SUM(GETS) "DICTIONARY GETS",SUM(GETMISSES) "DICTIONARY CACHE GET MISSES"ITPUB个人空间q9}N*|3W&c
       FROM  V$ROWCACHE;
J5dNaXeH0  ITPUB个人空间zXL [+G k&cT0D
14. 找ORACLE字符集
bYl|CjN0      select * from sys.props$ where name='NLS_CHARACTERSET'; 
i0t m8Y&]:a0  
OAM?C.v'S015. 监控 MTS
f+m-v\ l0  select busy/(busy+idle) "shared servers busy" from v$dispatcher;ITPUB个人空间#A3n*B\"lZN,B
  此值大于0.5时,参数需加大
$D/b7]%qE8t0  select sum(wait)/sum(totalq) "dispatcher waits" from v$queue where type='dispatcher';
+Gl"@[X&sr1wS0  select count(*) from v$dispatcher;
"S}l!uM!Xp WLx F0  select servers_highwater from v$mts;ITPUB个人空间 b%re'D4{k/f+O lp
  servers_highwater接近mts_max_servers时,参数需加大ITPUB个人空间 p6L l*K^E*Q
  
*VUe&]4gA.YEl016. 碎片程度ITPUB个人空间RF d-I3r_N
      select   tablespace_name,count(tablespace_name) ITPUB个人空间N }[&L'l;W
       from  dba_free_space ITPUB个人空间F%ibv;S!|
       group by tablespace_name ITPUB个人空间}P?"{amT
       having count(tablespace_name)>10;
S4P-prJ5L7tqTi0  alter tablespace name coalesce;
1V5Y)g&A'MG0  alter table name deallocate unused;
#I7u{q-in0A3E0      create or replace view ts_blocks_v as
%_C*`W DV~0       select   tablespace_name,block_id,bytes,blocks,
Fi$w5`4]\ P+\]0         segment_name ITPUB个人空间#f4}lwd
        from  dba_free_space
OxE;}nrS0       union all
#e*to JZ&k0       select   tablespace_name,block_id,bytes,blocks,
G8~?+X'KP2s0         segment_name 
+d Z~U8[*HN#w0        from  dba_extents;
knR6P$^&\0      select * from ts_blocks_v;
RQ2r3[;g#v0      select   tablespace_name,sum(bytes),max(bytes),count(block_id) ITPUB个人空间t1C$G u(r6Ibk~
       from  dba_free_space ITPUB个人空间Tm(HED P;[3T
       group by tablespace_name;ITPUB个人空间(Wi+b5\&c w'}1AF
     查看碎片程度高的表
Z@;S(~vV]&a^0      SELECT   segment_name table_name , COUNT(*) extents
[.cg;j8Y$y#P\"G0       FROM  dba_segments ITPUB个人空间"C3c.c#V2B:^o+HpM5C
       WHERE  owner NOT IN ('SYS', 'SYSTEM') ITPUB个人空间7_D,~ klN
       GROUP BY segment_nameITPUB个人空间%e M'j"F4a_`
       HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM dba_segments GROUP BY segment_name);ITPUB个人空间{+u_J S
  
\&~9qmH x017. 表、索引的存储情况检查
f e&ID/J&[9Qy0      select   segment_name,sum(bytes),count(*) ext_quan ITPUB个人空间|&P Rpp.\)T1o3B I6Z
       from  dba_extents 
u G,}L/\:w0       where   tablespace_name='&tablespace_name' and ITPUB个人空间L]V;U8@;dE
        segment_type='TABLE' 
lW.U[3\/za3Y+fr0       group by tablespace_name,segment_name;
LbcM R_j7]/t0      select   segment_name,count(*) ITPUB个人空间&E-FD @ hI-~ba{
       from  dba_extents 
jv/w-P#Rd1N"M#`0       where  segment_type='INDEX' and wner='&owner'
7wZ~6oohf S7v0       group by segment_name;
_7r4uX[ {8?0  
E_Jy}018、找使用CPU多的用户session
2S/l:@i~8S$Fe0     12是cpu used by this sessionITPUB个人空间.uK$@ ^F/G's.B
      select   a.sid,spid,status,substr(a.program,1,40) prog,ITPUB个人空间K8r)uj;I c@,\
        a.terminal,osuser,value/60/100 valueITPUB个人空间g7U6C:J9xZ
       from  v$session a,v$process b,v$sesstat c
aZ1v^y_&b0       where  c.statistic#=12 and ITPUB个人空间Z5]Lmkx cB'a
        c.sid=a.sid and ITPUB个人空间1g'j5J [7f G"~7\)l[,Q
        a.paddr=b.addr 
#a _7xb.s0SY z B0       order by value desc;
*s3n[/C%N0VeF/Lf0  
R)a*d TwVMw6yl,s])z019.对可疑/性能不好的Server Process来进行Trace.,可以用tkprof来分析Trace的结果.比较方便.使用Unix的KSH. 
W&o6x_}Ff8c0   (1) start_trc: ITPUB个人空间IjFh6_
  #!/bin/ksh ITPUB个人空间0d R!]0hF
  if (( $# != 1 )) 
$a;{3Sd}a} @i0  then 
[4Aa|"R5C'YK/x0  echo Usuage: start_trc pid ITPUB个人空间 q n A:w/d.t6n
  fi 
c.a`FAaF0  sid_serial=$(print " 
'b7})ANS]0  connect / as sysdba; 
]%~ RQm6S%vnB Yi0  set heading off; ITPUB个人空间 mF`*S[,B m|]
  set feedback off; ITPUB个人空间t&mv\Jgo4ew
  select a.sid,a.serial# from v\$session a,v\$process b where a.paddr=b.addr and b.spid=$1; ITPUB个人空间i4T-TT#i6y6bf.w.t
  exit; 
/e{t}3{X#Y4[!u0  " | sqlplus -s /nolog | grep -v 'Connected' | sed -e 's/\([0-9]\{1,\}\)/\1,/' -e '/^$/d' ) ITPUB个人空间 FZ(B;\*w:Y
  if [[ -z $sid_serial ]] ITPUB个人空间GU#LPR.v9E
  then 
v+F+BDmty e0  print "Seems that this process $1 is not an Oracle process!" 
}N }V+W8R0  exit 1 
.B%Z O e3PAm B"NlD_0  fi ITPUB个人空间SR*G2{ g\][E
  print " 
n]2k6pfm0  connect / as sysdba; ITPUB个人空间+G$i-I ? R'w#w
  execute dbms_system.set_sql_trace_in_session($sid_serial,true); 
bn:~2}*z]H XAL0  exit; ITPUB个人空间8}:atZ:yu|:Nz3Z
  " | sqlplus -s /nolog 
R5J1U,Y'rH0  ITPUB个人空间t*fpR|.PD$Zk
  (2) stop_trc: 
7p\:|Gp)e F-h3e0  #!/bin/ksh 
HA%im7b-C#y;u0  if (( $# != 1 )) 
4zR4xpwt ?0  then ITPUB个人空间jfzO@%j4v.A` g
  echo Usuage: stop_trc pid ITPUB个人空间7] OT ^+G4Fx6o4b G
  fi 
H{:bw\+{~0  sid_serial=$(print " ITPUB个人空间}(^;E:`N zn
  connect / as sysdba; ITPUB个人空间4g4]_!XP%d2H
  set heading off; 
TR+@E UZ5e)@d9ka0  set feedback off; 
YJF3Q]R:z7W7j&Vd0  select a.sid,a.serial# from v\$session a,v\$process b where a.paddr=b.addr and b.spid=$1; ITPUB个人空间Y*|W%d5|8R
  exit; ITPUB个人空间J$I1CN4[+W sCi
  " | sqlplus -s /nolog | grep -v 'Connected' | sed -e 's/\([0-9]\{1,\}\)/\1,/' -e '/^$/d' ) ITPUB个人空间1rta%H4b6W
  if [[ -z $sid_serial ]] 
5m6t-?\0S*^0  then 
*Rctv*F s e'^g {0  print "Seems that this process $1 is not an Oracle process!" 
rCNlJ7AJ&gfs0  exit 1 ITPUB个人空间#WI1T9FZb
  fi ITPUB个人空间q3jp*I:B
  print " 
)D Ab5Q6y0  connect / as sysdba; ITPUB个人空间9t;i F_!q jD8g
  execute dbms_system.set_sql_trace_in_session($sid_serial,false); ITPUB个人空间;Tc&jX0_+x.o,H`
  exit; ITPUB个人空间4o6n#YE+^
  " | sqlplus -s /nolog
f^'dS$Fv4C\j0  ITPUB个人空间Q!B7F0k'j!S ^ iY
20.查看Lock 
i8y7U'_6f1O\D0  SELECT   sn.username, m.sid, m.type, 
sVn3o)Qv1D Y0    DECODE(m.lmode, 0, 'None', 
`K$j*o UI0     1, 'Null', 
4{w,daD#f0     2, 'Row Share', 
j7n&d&R-Z[/N0     3, 'Row Excl.', ITPUB个人空间%Vus1Q+x\+w}
     4, 'Share', 
#@r `OV@ }$V0     5, 'S/Row Excl.', 
+b oW1Rb:h C0     6, 'Exclusive', 
$Gy,PE H&`0     lmode, ltrim(to_char(lmode,'990'))) lmode, 
t&W!zN:YQ0    DECODE(m.request,0, 'None', 
J RX(W h8W0     1, 'Null', 
3hbW*TiSD1}q y0     2, 'Row Share', 
|?.^B;{S0     3, 'Row Excl.', ITPUB个人空间 x8E7B\ TW@
     4, 'Share', 
B?"|g(Z[*Y0     5, 'S/Row Excl.', ITPUB个人空间vU0BS5dM(t
     6, 'Exclusive', ITPUB个人空间7E4XExU+?4?6Q
     request, ltrim(to_char(m.request,'990'))) request, 
'J$\J Iv4_0    m.id1, m.id2 ITPUB个人空间G*i3I)X x ]@;\#O
   FROM  v$session sn, v$lock m ITPUB个人空间-N'C*C(B(_%H}
   WHERE  (sn.sid = m.sid AND m.request != 0) OR ITPUB个人空间%w!keZi!~8uHd7{
    ( sn.sid = m.sid AND 
s{.?"hQo6gwz0     m.request = 0 AND ITPUB个人空间 x5i Q3B2wH
     lmode != 4 AND 
!F$}UkN s0     (id1, id2) IN (ITPUB个人空间Xc,y(`K
       SELECT   s.id1, s.id2 ITPUB个人空间-}:u_ ?W D Eg
        FROM  v$lock s 
Q~*p$`/I6]{A?"pO0        WHERE  request != 0 AND 
!? q/LD Zy:ju!Ev0         s.id1 = m.id1 AND 
r0J)e&BrG0         s.id2 = m.id2
:J8H!en^:RW5s&z!T2Da0       ) 
4u6QHU or#r0    ) ITPUB个人空间4|:Ui d7x
   ORDER BY id1, id2, m.request; ITPUB个人空间ce/JM#E s!k|C:k P
  
|CT,@*^;ulPs0   select   l.sid,s.serial#,s.username,s.terminal, 
%ec1Y)aK~L,s0     decode(l.type,'RW','RW - Row Wait Enqueue', 
8m7myx"{y0      'TM','TM - DML Enqueue', ITPUB个人空间hpB(X v$X,GA
      'TX','TX - Trans Enqueue', ITPUB个人空间/ow7A.K%^!TS.W~*T
      'UL','UL - User',l.type||'System') res, ITPUB个人空间h^7@1NC#q R'\
     substr(t.name,1,10) tab,u.name owner, 
e8r pC|9S0     l.id1,l.id2, ITPUB个人空间%Wb\.Vk5^J
     decode(l.lmode,1,'No Lock', 
p7{ gz1~0      2,'Row Share', 
%JY})ho$q(^|5[0      3,'Row Exclusive', 
yH_3C!C!M0      4,'Share', 
:`.ULz4mZ%T0      5,'Shr Row Excl', 
1P4]5V |G Y0      6,'Exclusive',null) lmode, 
G:fFTB1^"GA0     decode(l.request,1,'No Lock', ITPUB个人空间 Md,pTEg-X
      2,'Row Share', ITPUB个人空间8G8G)JEc iS
      3,'Row Excl', 
Fln0V/I h)Rw0      4,'Share', 
7L,tn2C7ma#rdU*F(}0      5,'Shr Row Excl', ITPUB个人空间*Nv~5E-U9e,X`%s
      6,'Exclusive',null) request 
L`td2?1Q0    from  v$lock l, v$session s, ITPUB个人空间t$z5c&Tb'Z D
     sys.user$ u,sys.obj$ t 
D8V}Ef ]0    where  l.sid = s.sid and 
;J_$t-vpX&l0     s.type != 'BACKGROUND' and ITPUB个人空间lq2Zyp#u!hwl,Wz
     t.obj# = l.id1 and 
;r-Xkh g|3eu;X5q`0     u.user# = t.owner#;ITPUB个人空间$B@u%X3\#P-X;T A
監控登入登出的用戶:ITPUB个人空间%Kd'R&ea
創建如下的兩張表:ITPUB个人空间&e%| Q*{Hp
create table login_log                        --        登入登出信息表ITPUB个人空间ITT"GY
(ITPUB个人空间k9[X M&h6Xm7L&m!Cbk
    session_id int not null,        --        sessionid
2s$]LHv&n,f&S0    login_on_time  date,                --        登入時間       
Z9U]&\:bk0    login_off_time  date,                --        登出時間       ITPUB个人空间h:C$kgL_+[ m H
    user_in_db        varchar2(30),        --        登入的db userITPUB个人空间Bi W#Q#Q|9h8s
    machine    varchar2(20),           --        機器名
(V*yOq0k G0    ip_address varchar2(20),        --        ip地址
1IP egq'N7~C0    run_program varchar2(20)    --        以何程序登入ITPUB个人空间'o]:vnX:`s
);

create table allow_user                        --        網域用戶表ITPUB个人空间H0C/BI%R NP9NZ
(ITPUB个人空间(`;?:r)m3_|M*ke\
    ip_address varchar2(20),                --        ip地址
Zs#I$Y+@+@T&t0    login_user_name nvarchar2(20)   --        操作者姓名
.|%F"u8F6Q {0);

創建如下的兩個觸發器:
f-f"iZ+P0create or replace trigger login_on_info                --        紀錄登入信息的觸發器ITPUB个人空间*MIe m$G Z
after logon on database
.fRtu@"vr0BeginITPUB个人空间sV-S-D b2[!tIJ
    insert into login_log(session_id,login_on_time,login_off_time,user_in_db,machine,ip_address,run_program)ITPUB个人空间3c!S"[im(qmg*i
    select AUDSID,sysdate,null,sys.login_user,machine,SYS_CONTEXT('USERENV','IP_ADDRE

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25530136/viewspace-690330/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2011-03-14

  • 博文量
    7
  • 访问量
    5257