DBA指令碼 (3)

jss001發表於2009-02-27

下面按類別列出一些ORACLE使用者常用資料字典的查詢使用方法。

一、使用者

檢視當前使用者的預設表空間
SQL>select username,default_tablespace from user_users;

檢視當前使用者的角色
SQL>select * from user_role_privs;

檢視當前使用者的系統許可權和表級許可權
SQL>select * from user_sys_privs;
SQL>select * from user_tab_privs;

二、表

檢視使用者下所有的表
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;

三、索引

檢視索引個數和類別
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');

四、序列號

檢視序列號,last_number是當前值
SQL>select * from user_sequences;

五、檢視

檢視檢視的名稱
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');

六、同義詞

檢視同義詞的名稱
SQL>select * from user_synonyms;

七、約束條件

檢視某表的約束條件
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;

八、儲存函式和過程

檢視函式和過程的狀態
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 owner=user and name=upper('&plsql_name');

九、觸發器

檢視觸發器

set long 50000;
set heading off;
set pagesize 2000;

select
'create or replace trigger "' ||
trigger_name || '"' || chr(10)||
decode( substr( trigger_type, 1, 1 ),
'A', 'AFTER', 'B', 'BEFORE', 'I', 'INSTEAD OF' ) ||
chr(10) ||
triggering_event || chr(10) ||
'ON "' || table_owner || '"."' ||
table_name || '"' || chr(10) ||
decode( instr( trigger_type, 'EACH ROW' ), 0, null,
'FOR EACH ROW' ) || chr(10) ,
trigger_body
from user_triggers;


--分析資料庫效能的SQL

--用於檢視哪些例項的哪些操作使用了大量的臨時段

SELECT to_number(decode(SID, 65535, NULL, SID)) sid,
operation_type OPERATION,trunc(EXPECTED_SIZE/1024) ESIZE,
trunc(ACTUAL_MEM_USED/1024) MEM, trunc(MAX_MEM_USED/1024) "MAX MEM",
NUMBER_PASSES PASS, trunc(TEMPSEG_SIZE/1024) TSIZE
FROM V$SQL_WORKAREA_ACTIVE
ORDER BY 1,2;
---查詢有熱塊查詢的SQL語句
select hash_value
from v$sqltext a,
(select distinct a.owner,a.segment_name,a.segment_type from
dba_extents a,
(select dbarfil,dbablk
from (select dbarfil,dbablk
from x$bh order by tch desc) where rownum < 11) b
where a.RELATIVE_FNO = b.dbarfil
and a.BLOCK_ID <= b.dbablk and a.block_id + a.blocks > b.dbablk) b
where a.sql_text like '%'||b.segment_name||'%' and b.segment_type = 'TABLE'
order by a.hash_value,a.address,a.piece;
--全表掃描
select opname,target,b.num_rows,b.tablespace_name,count(target) from v$session_longops a,all_all_tables b
where a.TARGET=b.owner||'.'||b.table_name
having count(target)>10 group by opname,target,b.num_rows,b.tablespace_name
--檢視磁碟排序和快取排序次數
select to_char(sn.snap_time,'yyyy-mm-dd hh24') time_,
avg(newmen.value - oldmen.value) sorts_memeory,
avg(newdsk.value - olddsk.value) disk_sort
from stats$sysstat oldmen,
stats$sysstat newmen,
stats$sysstat newdsk,
stats$sysstat olddsk,
stats$snapshot sn
where newdsk.snap_id=sn.snap_id
and olddsk.snap_id=sn.snap_id-1
and newmen.snap_id=sn.snap_id
and newdsk.snap_id=sn.snap_id -1
and oldmen.name='sorts (memory)'
and newmen.name='sorts (memory)'
and olddsk.name='sorts (disk)'
and newdsk.name='sorts (disk)'
group by to_char(sn.snap_time,'yyyy-mm-dd hh24')
--執行最慢的前10個SQL???
select * from (
select
to_char(snap_time,'dd Mon HH24:mi:ss') mydate,
executions exec,
loads loads,
parse_calls parse,
disk_reads reads,
buffer_gets gets,
rows_processed rows_proc,
sorts sorts,
sql_text,
hash_value
from
perfstat.stats$sql_summary sql,
perfstat.stats$snapshot sn
where
sql.snap_id >
(select min(snap_id) min_snap
from stats$snapshot where snap_time > sysdate-$days_back)
and
sql.snap_id = sn.snap_id
order by $sortskey desc) tt where rownum<11;
--SQL快取池的命中率查詢(pinhitratio,gethitratio應該大於90%以上)

select namespace,gethitratio,pinhitratio,reloads,invalidations
from v$librarycache
where namespace in ('SQL AREA','TABLE/PROCEDURE','BODY','TRIGGER')


--資料庫的常規引數我就不說了,除了V$parameter中的常規引數外,ORACLE還有大量的隱含引數,下面的語句就可以查詢到資料庫的所有隱含引數以及其值與引數的描述。
SELECT NAME
,VALUE
,decode(isdefault, 'TRUE','Y','N') as "Default"
,decode(ISEM,'TRUE','Y','N') as SesMod
,decode(ISYM,'IMMEDIATE', 'I',
'DEFERRED', 'D',
'FALSE', 'N') as SysMod
,decode(IMOD,'MODIFIED','U',
'SYS_MODIFIED','S','N') as Modified
,decode(IADJ,'TRUE','Y','N') as Adjusted
,description
FROM ( --GV$SYSTEM_PARAMETER
SELECT x.inst_id as instance
,x.indx+1
,ksppinm as NAME
,ksppity
,ksppstvl as VALUE
,ksppstdf as isdefault
,decode(bitand(ksppiflg/256,1),1,'TRUE','FALSE') as ISEM
,decode(bitand(ksppiflg/65536,3),
1,'IMMEDIATE',2,'DEFERRED','FALSE') as ISYM
,decode(bitand(ksppstvf,7),1,'MODIFIED','FALSE') as IMOD
,decode(bitand(ksppstvf,2),2,'TRUE','FALSE') as IADJ
,ksppdesc as DESCRIPTION
FROM x$ksppi x
,x$ksppsv y
WHERE x.indx = y.indx
AND substr(ksppinm,1,1) = '_'
AND x.inst_id = USERENV('Instance')
)
ORDER BY NAME
--想知道現在哪個使用者正在利用臨時段嗎?這個語句將告訴你哪個使用者正在利用臨時段。

SELECT b.tablespace, b.segfile#, b.segblk#, b.blocks, a.sid, a.serial#,
a.username, a.osuser, a.status,c.sql_text
FROM v$session a,v$sort_usage b, v$sql c
WHERE a.saddr = b.session_addr
AND a.sql_address = c.address(+)
ORDER BY b.tablespace, b.segfile#, b.segblk#, b.blocks;
--檢視磁碟碎片
select tablespace_name,sqrt(max(blocks)/sum(blocks))*
(100/sqrt(sqrt(count(blocks)))) FSFI
from dba_free_space
group by tablespace_name order by 1
1.檢視錶空間的名稱及大小
select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_size
from dba_tablespaces t, dba_data_files d
where t.tablespace_name = d.tablespace_name
group by t.tablespace_name;

2.檢視錶空間物理檔案的名稱及大小
select tablespace_name, file_id, file_name,
round(bytes/(1024*1024),0) total_space
from dba_data_files
order by tablespace_name;

3.檢視回滾段名稱及大小
select segment_name, tablespace_name, r.status,
(initial_extent/1024) InitialExtent,(next_extent/1024) NextExtent,
max_extents, v.curext CurExtent
From dba_rollback_segs r, v$rollstat v
Where r.segment_id = v.usn(+)
order by segment_name
15。耗資源的程式(top session)
select s.schemaname schema_name, decode(sign(48 - command), 1,
to_char(command), 'Action Code #' || to_char(command) ) action, status
session_status, s.osuser os_user_name, s.sid, p.spid , s.serial# serial_num,
nvl(s.username, ' Oracle process ') user_name, s.terminal terminal,
s.program program, st.value criteria_value from v$sesstat st, v$session s , v$process p
where st.sid = s.sid and st.statistic# = to_number('38') and ('ALL' = 'ALL'
or s.status = 'ALL') and p.addr = s.paddr order by st.value desc, p.spid asc, s.username asc, s.osuser asc

16。檢視鎖(lock)情況
select /*+ RULE */ ls.osuser os_user_name, ls.username user_name,
decode(ls.type, 'RW', 'Row wait enqueue lock', 'TM', 'DML enqueue lock', 'TX',
'Transaction enqueue lock', 'UL', 'User supplied lock') lock_type,
o.object_name object, decode(ls.lmode, 1, null, 2, 'Row Share', 3,
'Row Exclusive', 4, 'Share', 5, 'Share Row Exclusive', 6, 'Exclusive', null)
lock_mode, o.owner, ls.sid, ls.serial# serial_num, ls.id1, ls.id2
from sys.dba_objects o, ( select s.osuser, s.username, l.type,
l.lmode, s.sid, s.serial#, l.id1, l.id2 from v$session s,
v$lock l where s.sid = l.sid ) ls where o.object_id = ls.id1 and o.owner
<> 'SYS' order by o.owner, o.object_name
--檢視低效率的SQL語句
SELECT EXECUTIONS , DISK_READS, BUFFER_GETS,
ROUND((BUFFER_GETS-DISK_READS)/BUFFER_GETS,2) Hit_radio,
ROUND(DISK_READS/EXECUTIONS,2) Reads_per_run,
SQL_TEXT
FROM V$SQLAREA
WHERE EXECUTIONS>0
AND BUFFER_GETS > 0
AND (BUFFER_GETS-DISK_READS)/BUFFER_GETS < 0.8
ORDER BY 4 DESC

[@more@]

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

相關文章