ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Oracle DBA 常用的SQL日志

Oracle DBA 常用的SQL日志

原创 Linux操作系统 作者:ORA-Hansey 时间:2011-03-20 00:01:08 0 删除 编辑
--监控索引是否使用

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'

执行计划
~+OE:?p&E)c ?0   1)根据SID,从v$sql中找到相应SQL的HASH_VALUE和ADDRESS ;ITPUB个人空间ag aE(|Zr7D l
   SELECT a.sql_text , a.address , a.hash_value
/j+x eq`kA,B0   FROM   v$sql a , v$session bITPUB个人空间,O8vr;B8h(Ax
   where  a.hash_value = b.sql_hash_value
o:D4}m A1m ln4y*^0   and    b.sid = &sid ;
*m|5Z C a \Zj+lI0Alan Lee(160921) 22:58:07
AYv,]?m8U8SR0 2)根据hash_value和address的值,从v$sql_plan中找到真实的执行计划。ITPUB个人空间ow*wy7J#v
   set line 200;ITPUB个人空间:S5r%IR8lHP6?
   col oper format a100;ITPUB个人空间6J/S8L.G a:c#@bMo,v
   select lpad(oper,length(oper)+level*2,' ') oper,costITPUB个人空间 hwH]Y@e3f@
   from   (
IO yk:z~0           select object_name||':'||operation||' '||options as oper,cost,id,parent_idITPUB个人空间X-f\&DH'G_
           from   v$sql_plan
eX}U\fO9kK0           where  hash_value = &hash_value
tn7p&Z&x'}C_-v0           and    address = '&address'
%c)g%@ B$B1]}V.W4{K0          )
yQ q} @o/Fq0   start with id=0ITPUB个人空间Rr;x:o3d9u
   connect by prior id = parent_id;ITPUB个人空间5V%^ Y FAA3V3Q
Alan Lee(160921) 22:58:26
h3T KZi+z!F0这2步,就可以找出实际正在跑的SQL使用的是什么执行计划

set autotrace traceonly statisticsITPUB个人空间d&E(? g6N'? a X
set autotrace traceonly explainITPUB个人空间7k+} `)G-`j@
set 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个人空间 XR OS%e)ez
sqlplus "/as sysdba"ITPUB个人空间:V q^`&Y7~*U6z
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,l.ORACLE_USERNAME,ITPUB个人空间)_ `e?'S9N
l.OS_USER_NAME,l.PROCESS FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;


7l4ld L:~02)kill掉这个死锁的进程:ITPUB个人空间 M bc7bLcTd^\
alter system kill session 'sid,serial#'; (其中sid=l.session_id)
I*O INS j9vMx-n03)如果还不能解决,ITPUB个人空间l u#g3ff)or(T
select pro.spid from v$session ses,v$process pro where ses.sid=XX and ses.paddr=pro.addr;ITPUB个人空间n;o!vM'}#]x#N p.Y
其中sid用死锁的sid替换。ITPUB个人空间O5^Yvs3x1f
exit


S{c:[1u`@m0--与权限相关的字典

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

*/

 常用系统表,视图和作用
b"G3}k*m!_e`0查看有关用户的信息:dba_users
p2jJ%?Pq u+{0查看有关角色的信息:dba_roles,dba_role_privs,role_sys_privs
s C&d!I`ZM0查看有关系统权限的信息:dba_sys_privsITPUB个人空间Uv%A-ii M
查看当前数据库表空间状况:dba_tablespacesITPUB个人空间PXj~;W]kDB${
查看用户的系统权限:user_sys_privsITPUB个人空间0zr{:F ?6Sc
查看某个用户对另外一个用户授予的权限:user_tab_privs_madeITPUB个人空间a z,P6s$N,B)Y h
查看某个用户对另外一个用户授予的列级权限:user_col_privs_made
IT)OJ&\Fb0查看某个用户接受的权限:user_tab_privs_recd
,bqJ8o zA8v/A^Q0查看某个用户接受的列级权限:user_col_privs_recd
'J6j*R*Njo2z$Hs0查看有关用户的角色信息:user_role_privsITPUB个人空间4T4yZ"Q1xR#S'\
查看有关授予某个角色的系统权限信息:role_sys_privs
R y,I(k Huu!BX } j5S0查看有关授予某个角色的对象权限信息:role_tab_privs
6~G'BLe2x4\,m0查看当前用户所拥有的表信息:user_tables
%i3ZmejX$G0查看当前用户有权限访问的表信息:all_tablesITPUB个人空间.imh[W(C0V1B
查看当前用户所拥有的所有表的列信息:user_tab_columnsITPUB个人空间M R}:s.g2d_?
查看当前用户可以访问的表中的列信息:all_tab_columns
y:TAA"@%jWTyh0查看当前用户所拥有的所有约束信息:user_constraintITPUB个人空间TWltjl#Wn
查看当前用户所拥有的所有约束和列的关系:user_cons_constraint
?7mPv(P$G0查看表中注释内容:user_tab_commentsITPUB个人空间B%x k4eu^|
查看表中列注释内容:user_col_commentsITPUB个人空间e(BU/Rp}d
提供练习的表:dual
a#k.h%Q1f|eBICf%sn0查看相关时区的名称和简称:v$timezone_names

V$OPTION:显示已安装的Oracle选项ITPUB个人空间`4Ma%L/lc.b)Y
select * from v$option;ITPUB个人空间CyUTn;nZW[
取得Oracle版本的详细信息ITPUB个人空间$? thb6MK
select * from v$version;ITPUB个人空间{ j%t |&O;~/l1d'k
取得初始化参数的详细信息ITPUB个人空间/` H@L0O)H
select name,value,description from v$parameter;ITPUB个人空间t`Nq{|
取得当前例程的详细信息ITPUB个人空间)Y}7v]X.^
select * from v$instance;

ITPUB个人空间 T0l"qxKit sC
1、用户

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

  SQL>select username,default_tablespace from user_users;

  查看当前用户的角色

  SQL>select * from user_role_privs;

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

  SQL>select * from user_sys_privs;或ITPUB个人空间6[R d1M8~N%y8b
select  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. 监控事例的等待
f(@L0Isg~0  select   event,ITPUB个人空间vX&[$^{F l
    sum(decode(wait_Time,0,0,1)) "Prev", 
6i-fx\5ty$R0    sum(decode(wait_Time,0,1,0)) "Curr",ITPUB个人空间j+^UjZ]|5{ I.o
    count(*) "Tot" ITPUB个人空间D;nw ]`.F:j+b-|
       from  v$session_Wait ITPUB个人空间0DH&m-R)C XFAQ
       group by event 
rS&wf-x&Q0       order by 4;
pf-a+Q!g2DM%D3m0  
ie{-K&};L02. 回滚段的争用情况
)qY_!}3iD\g}0      select   name, waits, gets, waits/gets "Ratio" ITPUB个人空间`XCJe
       from  v$rollstat a, v$rollname b ITPUB个人空间l Ofz5fb;H
       where  a.usn = b.usn; 
3K8uW jy:R0  
&@O6T3M0K$q:_,tI:W:~-j03. 监控表空间的 I/O 比例
_~8v%V2g0      select   df.tablespace_name name,df.file_name "file",f.phyrds pyr,
$jyqs?w1D#[0        f.phyblkrd pbr,f.phywrts pyw, f.phyblkwrt pbwITPUB个人空间Jj6M,`:W W'd _)e
       from  v$filestat f, dba_data_files dfITPUB个人空间fd-_,S%fWY7{ ^X({
       where  f.file# = df.file_idITPUB个人空间gJX7@:R1w9~y"P
       order by df.tablespace_name;ITPUB个人空间TZ ? zNL]Q4A0|
  
pl\8we04. 监控文件系统的 I/O 比例ITPUB个人空间'vJ$rL-^*Hy:\6k
      select   substr(a.file#,1,2) "#", substr(a.name,1,30) "Name", 
w%n&e.B4EShA N1F0        a.status, a.bytes, b.phyrds, b.phywrts 
%U%g;A"n+e K,V0       from  v$datafile a, v$filestat b 
'Z w_6kfu0       where  a.file# = b.file#; 
? z&ZCh0  ITPUB个人空间U/D&o \i_u
5.在某个用户下找所有的索引ITPUB个人空间YY/A)Mf3L
      select   user_indexes.table_name, 
+@@x r Ad4I"lA0        user_indexes.index_name,
,j4`;|$[l0        uniqueness, ITPUB个人空间 r+E2_ KN8nA%UQ}?bW
        column_name
a5c1X\i[i,q0       from  user_ind_columns, user_indexesITPUB个人空间~'H,X&S \"U E
       where  user_ind_columns.index_name = user_indexes.index_name and ITPUB个人空间X4e7U.?9Z
        user_ind_columns.table_name = user_indexes.table_name ITPUB个人空间s @`+{,D"Y!oJ&d
       order by user_indexes.table_type, user_indexes.table_name,ITPUB个人空间&mO5MF,t{o)P3S
        user_indexes.index_name, column_position;ITPUB个人空间-UA:B"f.pv
6. 监控 SGA 的命中率ITPUB个人空间;iK0z]2} N D&g
      select   a.value + b.value "logical_reads", ITPUB个人空间Nr)~^:Mq0Q
        c.value "phys_reads",
o7g/|4z.g~0        round(100 * ((a.value+b.value)-c.value) / (a.value+b.value)) "BUFFER HIT RATIO" ITPUB个人空间A)x&DOJ(u
       from  v$sysstat a, v$sysstat b, v$sysstat cITPUB个人空间},zw{3AF
       where  a.statistic# = 38 and b.statistic# = 39 and ITPUB个人空间z_*j4Aag [o5a%To
        c.statistic# = 40; 
j2y`;wF5o7x!~0  ITPUB个人空间o/]CcK*\w0Zr
7. 监控 SGA 中字典缓冲区的命中率ITPUB个人空间-[ u+sz+A
      select   parameter, gets,Getmisses , 
5L7@Qyp\,V9k0        getmisses/(gets+getmisses)*100 "miss ratio",ITPUB个人空间)u ?2RC:Yz
        (1-(sum(getmisses)/ (sum(gets)+sum(getmisses))))*100 "Hit ratio"ITPUB个人空间*}/pP,e;u+y
       from  v$rowcache 
(x+xF@!I0       where  gets+getmisses <>0
DUQ6qc.gPPA2V0       group by parameter, gets, getmisses; 
?F_ei"J Z0  ITPUB个人空间I}5A(]O
8. 监控 SGA 中共享缓存区的命中率,应该小于1%
n6\W ^4f*|0      select   sum(pins) "Total Pins", sum(reloads) "Total Reloads",
ZW4|"c ^e2M+HX^0        sum(reloads)/sum(pins) *100 libcacheITPUB个人空间Tq`f,j"r{&lR$q
       from  v$librarycache;ITPUB个人空间pbKB7[)_(C
      select   sum(pinhits-reloads)/sum(pins) "hit radio",ITPUB个人空间MY3IQ T?%}Y
        sum(reloads)/sum(pins) "reload percent" ITPUB个人空间G dd?oL |*S6Z vwy
       from  v$librarycache;ITPUB个人空间(B+u,K`so${
  
l1xAm.z09. 显示所有数据库对象的类别和大小ITPUB个人空间1D c.v1sA2T\
      select   type,
IE5c%s5{+T)L.b5t0        count(name) num_instances,
3I2sFa'@]x5p*vH0        sum(source_size) source_size,
7xS4X;i}X5X0        sum(parsed_size) parsed_size,
7kR;K,_ z M"[0        sum(code_size) code_size,
1U7cp!C*I1F"W:mY0        sum(error_size) error_size,ITPUB个人空间#u9tC#{ F{1e)Q
        sum(source_size) +sum(parsed_size) +sum(code_size) +sum(error_size) size_required 
} u.iUt:X,?%] Z'V0       from  dba_object_size 
KU|;y(M-\0       group by type 
|6{!y3w'H#I3S7a3g0       order by 1;
,x&L~{%Q%R6{7S,w0  ITPUB个人空间7{(F(T kU2a*H
10. 监控 SGA 中重做日志缓存区的命中率,应该小于1%ITPUB个人空间H)D6c8fs4m1w A3w
      SELECT   name, gets, misses, immediate_gets, immediate_misses,ITPUB个人空间l#_*GR.X ka
        Decode(gets,0,0,misses/gets*100) ratio1,
&L6G_2Y;@/M0b:J5I9D0        Decode(immediate_gets+immediate_misses,0,0,
5_ F)~x:{0z-Dt5E v0         immediate_misses/(immediate_gets+immediate_misses)*100) ratio2
z+zVqrE1g?0       FROM  v$latch 
&V$q#M5t-d#U7g${ NP0       WHERE  name IN ('redo allocation', 'redo copy'); ITPUB个人空间Q%X!\:K:^~
  ITPUB个人空间 eGL8FTg
11.  监控内存和硬盘的排序比率,最好使它小于 .10,增加 sort_area_size 
9W2U6u?2|\0      SELECT   name, value 
J0C l&i_0       FROM  v$sysstat 
&@V](b3z&k%x^_ @0       WHERE  name IN ('sorts (memory)', 'sorts (disk)'); ITPUB个人空间0T\vw_`;r:c$n
  
m6~cqg)Z8e012. 监控当前数据库谁在运行什么SQL语句
;H9Afb {XT0      SELECT   osuser, username, sql_text 
)S8XIO``.ge+UY0       from  v$session a, v$sqltext b
8s6PnOO FWL-S0       where  a.sql_address =b.address order by address, piece;ITPUB个人空间P9E0fl:|8N)\+]
  ITPUB个人空间rT~&m`.X
13. 监控字典缓冲区
g P?$i2dc+V`0      SELECT   (SUM(PINS - RELOADS)) / SUM(PINS) "LIB CACHE" ITPUB个人空间%}6nUpT6M$LCP
       FROM  V$LIBRARYCACHE;
j|a9U6QP0jt0      SELECT   (SUM(GETS - GETMISSES - USAGE - FIXED)) / SUM(GETS) "ROW CACHE" 
5b+I ]yv0       FROM  V$ROWCACHE;
4?Y2sVX S0      SELECT   SUM(PINS) "EXECUTIONS", SUM(RELOADS) "CACHE MISSES WHILE EXECUTING" 
].v5wj7i'J8[0       FROM  V$LIBRARYCACHE; 
A6cFAv#nw8| N0     后者除以前者,此比率小于1%,接近0%为好。
9Q7P4u't P O.Y'd0      SELECT   SUM(GETS) "DICTIONARY GETS",SUM(GETMISSES) "DICTIONARY CACHE GET MISSES"ITPUB个人空间:[8c7_ ~i2`
       FROM  V$ROWCACHE;
,j)TN9c?$F"Vs Y0  ITPUB个人空间Pj*ZmOOe
14. 找ORACLE字符集ITPUB个人空间+OD ~c/M q7m
      select * from sys.props$ where name='NLS_CHARACTERSET'; ITPUB个人空间`#X}+\rKH-v
  
bf2k:nlz%d015. 监控 MTS
.^.Ic#_ y+x0  select busy/(busy+idle) "shared servers busy" from v$dispatcher;ITPUB个人空间K4Z]E i8|~(W4O
  此值大于0.5时,参数需加大ITPUB个人空间n[R:s*E_
  select sum(wait)/sum(totalq) "dispatcher waits" from v$queue where type='dispatcher';
:C ['whF.I+|0  select count(*) from v$dispatcher;
+H} PeC!h0  select servers_highwater from v$mts;ITPUB个人空间.|dS9{x*@aN
  servers_highwater接近mts_max_servers时,参数需加大
IzB]P(wQC0  ITPUB个人空间0c.Q"pD4_
16. 碎片程度ITPUB个人空间US~Z%U5w
      select   tablespace_name,count(tablespace_name) ITPUB个人空间)Pg Uq$B5ei8B+Q+b
       from  dba_free_space 
:{8H9l'KX7j;D0       group by tablespace_name 
T;nUn lF S,Sv/L-]s0       having count(tablespace_name)>10;
/AUR'a T6F;a.K}0  alter tablespace name coalesce;ITPUB个人空间*x3SzR8Q
  alter table name deallocate unused;ITPUB个人空间/m S.hz.N
      create or replace view ts_blocks_v as
Ib"De9E0       select   tablespace_name,block_id,bytes,blocks,
1d|Q&ow#L"h0         segment_name ITPUB个人空间#h$E e?"ifj
        from  dba_free_spaceITPUB个人空间;m o1Q0i/l$bTR9?
       union allITPUB个人空间8nS#T;zC7D"\
       select   tablespace_name,block_id,bytes,blocks,ITPUB个人空间 ME"`H1s$@D
         segment_name 
w5lYUP:p ^0        from  dba_extents;ITPUB个人空间7P&]mD2u
      select * from ts_blocks_v;
`~ Y e5?"@%J)]0      select   tablespace_name,sum(bytes),max(bytes),count(block_id) ITPUB个人空间'e7o7ZK/r#]E._
       from  dba_free_space ITPUB个人空间9_:b|&Rv,J
       group by tablespace_name;
.Q _9eAKiTW,A~0     查看碎片程度高的表
r,UDY2@0      SELECT   segment_name table_name , COUNT(*) extents
;`#^$_Pq:j@a0       FROM  dba_segments 
I DvlY:R!A)t1?0       WHERE  owner NOT IN ('SYS', 'SYSTEM') 
Uo$?Hu$uz0       GROUP BY segment_name
rSX@z5N*[3idv0       HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM dba_segments GROUP BY segment_name);ITPUB个人空间hw3ta i:m(k
  ITPUB个人空间W t*w H1~4e
17. 表、索引的存储情况检查
(ZPY"A`LA Q0      select   segment_name,sum(bytes),count(*) ext_quan 
mJX|X$eD3B0       from  dba_extents ITPUB个人空间x?!~n6p:}
       where   tablespace_name='&tablespace_name' and 
.i~y u6n\7MNpg&M0        segment_type='TABLE' ITPUB个人空间4i,z*Uu6yc
       group by tablespace_name,segment_name;
2^1ag\ rbr0      select   segment_name,count(*) ITPUB个人空间?u(Ob?cp-fM
       from  dba_extents 
U%F^|8MU$UXM0       where  segment_type='INDEX' and wner='&owner'
:v1c ?oo$E$N0       group by segment_name;
*H bb_X{/f0  ITPUB个人空间@Pl\3w`3W3G
18、找使用CPU多的用户sessionITPUB个人空间G6b4A rsb*lq|4n
     12是cpu used by this sessionITPUB个人空间#sW'z\;G
      select   a.sid,spid,status,substr(a.program,1,40) prog,ITPUB个人空间+p!j$vF[Vr@;G
        a.terminal,osuser,value/60/100 value
]u'`\;i|6u`A0       from  v$session a,v$process b,v$sesstat c
7v(]&m4}R ysNQ"G0       where  c.statistic#=12 and 
I)geJ(J1?3[;p#f)[E7lk0        c.sid=a.sid and ITPUB个人空间K7Rzsl/~"t7P
        a.paddr=b.addr 
8g![o!k$O1E0       order by value desc;ITPUB个人空间SK at6X+Rb#k.A
  ITPUB个人空间mX&?@y6_.}q
19.对可疑/性能不好的Server Process来进行Trace.,可以用tkprof来分析Trace的结果.比较方便.使用Unix的KSH. 
KUJ] Mr(g)o-`?0   (1) start_trc: 
x ^q%T7V7Z'Y0  #!/bin/ksh 
n V0MZB_0  if (( $# != 1 )) ITPUB个人空间I4ySb#n3j8{ S
  then ITPUB个人空间6`OM:A i v
  echo Usuage: start_trc pid ITPUB个人空间z}k0Y]*y7by+h't
  fi ITPUB个人空间#mX(jfIu
  sid_serial=$(print " 
r}7NIda4\ Dj;g0  connect / as sysdba; ITPUB个人空间,}%[pX[R
  set heading off; 
ql+bA)[a I0  set feedback off; ITPUB个人空间*@{ MVr6KUt
  select a.sid,a.serial# from v\$session a,v\$process b where a.paddr=b.addr and b.spid=$1; ITPUB个人空间hhh.J4r0OA
  exit; ITPUB个人空间;d*IS6z6X'e9^3[k
  " | sqlplus -s /nolog | grep -v 'Connected' | sed -e 's/\([0-9]\{1,\}\)/\1,/' -e '/^$/d' ) 
%i*z6[Y*q2? _&VS m0  if [[ -z $sid_serial ]] 
(J@%WE2gtXUu0  then ITPUB个人空间.| ^)yC Yx.Vx'e M)Q
  print "Seems that this process $1 is not an Oracle process!" ITPUB个人空间"A` j1Om0J#e R?
  exit 1 
3\%o0{#K8u[4ulH0  fi ITPUB个人空间'_)W'mUKg&x'bM {
  print " ITPUB个人空间,_'i;B%jYv
  connect / as sysdba; 
*g~5j"pX+\"t)b0  execute dbms_system.set_sql_trace_in_session($sid_serial,true); ITPUB个人空间1J?zDS&Q~ a
  exit; 
/f&Y,`9Z2Lc0  " | sqlplus -s /nolog ITPUB个人空间8Gwa7n _F v1m
  ITPUB个人空间's@!xBd%Ih3{2p XB7nY
  (2) stop_trc: 
P6~3@9a3{ R$q)M0  #!/bin/ksh 
N[ Wlb#n2`0  if (( $# != 1 )) 
Y&|\\SqNA*oL0  then 
(H7]"vgg:J ^0  echo Usuage: stop_trc pid 
B3`g6R~@)[:\/J0  fi 
\j~C4]kUXZ0  sid_serial=$(print " 
{n@l5I[4j8o)^9\0  connect / as sysdba; ITPUB个人空间5i6P)G0w$tQH{Z
  set heading off; 
;QG`)d(KB4~0  set feedback off; ITPUB个人空间%m%^*M rdo~1R
  select a.sid,a.serial# from v\$session a,v\$process b where a.paddr=b.addr and b.spid=$1; 
awx0T'd8N0v N0  exit; ITPUB个人空间2N wy(kTd$h
  " | sqlplus -s /nolog | grep -v 'Connected' | sed -e 's/\([0-9]\{1,\}\)/\1,/' -e '/^$/d' ) 
{;\8B @Cz-sFo q0  if [[ -z $sid_serial ]] ITPUB个人空间w$do;M6s`d ]!|
  then ITPUB个人空间/[7o?&G%Ibp/M
  print "Seems that this process $1 is not an Oracle process!" ITPUB个人空间Df7m5G/l'|{(d"D:~~C
  exit 1 
d4C'o1Cs/z~0  fi 
nJ.^*Pak'\0  print " ITPUB个人空间 g.I;d$D0qvf:uBS$r
  connect / as sysdba; ITPUB个人空间+S[jo2Z} ]o
  execute dbms_system.set_sql_trace_in_session($sid_serial,false); ITPUB个人空间7_Q5_(Ed!i2?F@A1P
  exit; 
OstX X|"z5`0  " | sqlplus -s /nologITPUB个人空间;O%XD$g,[7m,r
  
G|!?"e_ J^7uKX020.查看Lock ITPUB个人空间"F:Do.f,V'R7q7pr
  SELECT   sn.username, m.sid, m.type, ITPUB个人空间#W |1Uqb+k
    DECODE(m.lmode, 0, 'None', ITPUB个人空间v%AQ aa$liL
     1, 'Null', 
]/a$n'_B5Q#r0     2, 'Row Share', 
&Z` H-?es0     3, 'Row Excl.', ITPUB个人空间lN\s h)H#W
     4, 'Share', 
bpC+UH#eP3e g0     5, 'S/Row Excl.', ITPUB个人空间kueF\(GW w(pS3}
     6, 'Exclusive', 
(j'I2[VQ0     lmode, ltrim(to_char(lmode,'990'))) lmode, 
,G8SlC#P~0    DECODE(m.request,0, 'None', 
%Z#Z4l N"w#b0     1, 'Null', 
#Ou9C5q-Ao;eK0     2, 'Row Share', ITPUB个人空间!N)KL l{K
     3, 'Row Excl.', ITPUB个人空间4M;hOy;I1Co4\
     4, 'Share', ITPUB个人空间!A!aL6~]!WS$~
     5, 'S/Row Excl.', ITPUB个人空间Fge+U2s
     6, 'Exclusive', 
OR?U1j0     request, ltrim(to_char(m.request,'990'))) request, ITPUB个人空间n#|*l@;`1m5{
    m.id1, m.id2 ITPUB个人空间/Hmq+_pM
   FROM  v$session sn, v$lock m 
h#a"Ns1y;|1}|f!X4g0   WHERE  (sn.sid = m.sid AND m.request != 0) OR 
@%np4U,I0    ( sn.sid = m.sid AND ITPUB个人空间'e6[BQ+m2i}apZ5a
     m.request = 0 AND ITPUB个人空间1W~xK7r\W4S2E
     lmode != 4 AND 
@/kPMX)n1Y0     (id1, id2) IN (
*h3n sz?h N-M|0       SELECT   s.id1, s.id2 
3bp$Tw.UVlc0        FROM  v$lock s ITPUB个人空间2I NS/VC.{J
        WHERE  request != 0 AND 
;Wb V-F ^P a0         s.id1 = m.id1 AND ITPUB个人空间f4i1oh8OGN3F
         s.id2 = m.id2
b!C9?6yT8p0       ) 
3QkYJo)mH0    ) 
8ml.k"`q,q0   ORDER BY id1, id2, m.request; 
k6~!V`,JWWn6g ?0  
v m1g!SD5Oo0   select   l.sid,s.serial#,s.username,s.terminal, ITPUB个人空间-G-e1F,_/i1T1hJN
     decode(l.type,'RW','RW - Row Wait Enqueue', 
AR}jg,f u(T5EF0      'TM','TM - DML Enqueue', 
JR Z EyR,w0      'TX','TX - Trans Enqueue', 
/O!fpw3XV6qk%w0      'UL','UL - User',l.type||'System') res, ITPUB个人空间W{}!yz`uc a
     substr(t.name,1,10) tab,u.name owner, ITPUB个人空间JHxg ~A#N!A9Gh
     l.id1,l.id2, ITPUB个人空间 b+w/q.stdB;{
     decode(l.lmode,1,'No Lock', ITPUB个人空间'?2nt @:Ser_
      2,'Row Share', ITPUB个人空间!I A.ktK$}#b
      3,'Row Exclusive', 
5poCme:~/oeV0      4,'Share', 
Bj(EN0B!q,bZ0      5,'Shr Row Excl', 
Q t H1e} g0      6,'Exclusive',null) lmode, 
*wC9zclT w*h$D0     decode(l.request,1,'No Lock', ITPUB个人空间\P{zB8GE\
      2,'Row Share', ITPUB个人空间Y'Ub }/^sX.s
      3,'Row Excl', 
L x6[N:hT0      4,'Share', ITPUB个人空间;S-tcQ8U]
      5,'Shr Row Excl', 
PC'G/I\#^9~:j7U0      6,'Exclusive',null) request ITPUB个人空间8y2PM)lJ*Tl
    from  v$lock l, v$session s, ITPUB个人空间M5o)L7As
     sys.user$ u,sys.obj$ t ITPUB个人空间M:ccV&ZH
    where  l.sid = s.sid and ITPUB个人空间/mO.JUI|
     s.type != 'BACKGROUND' and 
:bQNA]M3_0     t.obj# = l.id1 and ITPUB个人空间:}:K3O P6]9K4a
     u.user# = t.owner#;ITPUB个人空间S.KQBk`,XD
監控登入登出的用戶:ITPUB个人空间HXZ\n*}&M
創建如下的兩張表:
'r:QG8k)K] p0create table login_log                        --        登入登出信息表
@VM,lA-I+og0(ITPUB个人空间 V2L.{N1J2y*RE
    session_id int not null,        --        sessionidITPUB个人空间c'WZ!P n/eEP.d w
    login_on_time  date,                --        登入時間       ITPUB个人空间%}y#B`1?1KT
    login_off_time  date,                --        登出時間       
;k:L/^#BF8C3~B6p D1{CE0    user_in_db        varchar2(30),        --        登入的db user
1J([0O#jvb |C*l0    machine    varchar2(20),           --        機器名ITPUB个人空间@Hi,`FAP ~
    ip_address varchar2(20),        --        ip地址
5z-[ D5y+m/x0    run_program varchar2(20)    --        以何程序登入ITPUB个人空间[5vhL:]$J*y$v8S
);

create table allow_user                        --        網域用戶表
rQ#s$A NQ0(ITPUB个人空间)OZy"z} ]y
    ip_address varchar2(20),                --        ip地址
7beP p%cK/a-G!t0    login_user_name nvarchar2(20)   --        操作者姓名
m`(~-s-Rn"QD(e0);

創建如下的兩個觸發器:ITPUB个人空间d W8@og6D&c
create or replace trigger login_on_info                --        紀錄登入信息的觸發器
.go O?.V0after logon on databaseITPUB个人空间 e(]o vKn5M:n;X
BeginITPUB个人空间"BQ mJk%G0V ?:me7V
    insert into login_log(session_id,login_on_time,login_off_time,user_in_db,machine,ip_address,run_program)
v)C\mT0    select AUDSID,sysdate,null,sys.login_user,machine,SYS_CONTEXT('USERENV','IP_ADDRE

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

下一篇: connect by 的用法
请登录后发表评论 登录
全部评论

注册时间:2011-03-08

  • 博文量
    21
  • 访问量
    47805