DBA常用的一些SQL和檢視(轉)

物理狂人發表於2011-12-21
alterindex&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';

--
sessionOS程式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.hash_value=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'

--
求當前會話的SIDSERIAL#
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 "ORACLEPID",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'
執行計劃
   1)根據SID,從v$sql中找到相應SQLHASH_VALUEADDRESS
   SELECT a.sql_text , a.address , a.hash_value
   FROM   v$sql a , v$session b
   where  a.hash_value = b.sql_hash_value
   and    b.sid = &sid ;
Alan Lee(160921) 22:58:07
2)根據hash_valueaddress的值,從v$sql_plan中找到真實的執行計劃。
   set line 200;
   col oper format a100;
   select lpad(oper,length(oper)+level*2,' ') oper,cost
   from   (
           select object_name||':'||operation||' '||options as oper,cost,id,parent_id
           from   v$sql_plan
           where  hash_value = &hash_value
           and    address = '&address'
          )
   start with id=0
   connect by prior id = parent_id;
Alan Lee(160921) 22:58:26
2步,就可以找出實際正在跑的SQL使用的是什麼執行計劃
set autotrace traceonly statistics
set autotrace traceonly explain
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;

--
計算databuffer命中率
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)查詢死鎖的程式:
sqlplus "/as sysdba"
SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,l.ORACLE_USERNAME,
l.OS_USER_NAME,l.PROCESS FROM V$LOCKED_OBJECT l,V$SESSION S WHERE l.SESSION_ID=S.SID;

2kill掉這個死鎖的程式:
alter system kill session 'sid,serial#';(其中sid=l.session_id
3)如果還不能解決,
select pro.spid from v$session ses,v$process pro where ses.sid=XX and ses.paddr=pro.addr;
其中sid用死鎖的sid替換。
exit

--與許可權相關的字典
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
*/
常用系統表,檢視和作用
檢視有關使用者的資訊:dba_users
檢視有關角色的信:dba_roles,dba_role_privs,role_sys_privs
檢視有關係統許可權的資訊:dba_sys_privs
看當前資料庫表空間狀況:dba_tablespaces
檢視使用者的系統許可權:user_sys_privs
檢視某個使用者對另外一個使用者授予的權:user_tab_privs_made
檢視某個使用者對另外一個使用者授予的列級許可權:user_col_privs_made
檢視某個使用者接受的許可權:user_tab_privs_recd
檢視某個使用者接受的列級許可權:user_col_privs_recd
檢視有關使用者的角色資訊:user_role_privs
檢視有關授予某個角色的系統許可權資訊:role_sys_privs
檢視有關授予某個角色的物件許可權資訊:role_tab_privs
檢視當前使用者所擁有的表資訊:user_tables
檢視當前使用者有許可權訪問的表信:all_tables
檢視當前使用者所擁有的所有表的列資訊:user_tab_columns
檢視當前使用者可以訪問的表中的列信:all_tab_columns
檢視當前使用者所擁有的所有約束資訊:user_constraint
檢視當前使用者所擁有的所有約束和列的關係:user_cons_constraint
檢視錶中註釋內容:user_tab_comments
檢視錶中列註釋內容:user_col_comments
提供練習的表:dual
檢視相關時區的名稱和簡稱:v$timezone_names
V$OPTION:顯示已安裝Oracle選項
select * from v$option;
取得Oracle版本的詳細資訊
select * from v$version;
取得初始化引數的詳細資訊
select name,value,description from v$parameter;
取得當前例程的詳細資訊
select * from v$instance;

1、使用者
  檢視當前使用者的預設表空間
  SQL>select username,default_tablespace from user_users;
  檢視當前使用者的角
  SQL>select * from user_role_privs;
  檢視當前使用者的系統許可權和表級許可權
  SQL>select * from user_sys_privs;
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');

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24104518/viewspace-713713/,如需轉載,請註明出處,否則將追究法律責任。

相關文章