DBA used SQL query database information

Common DBA scripts
1, see the name and size of the tablespace
  SELECT t.tablespace_name, round (SUM (bytes / (1024 * 1024)), 0) ts_size
  from DBA_TABLESPACES T, D dba_data_files
  WHERE t.tablespace_name = d.tablespace_name
  Group by T .tablespace_name;

2, the file name for the table and the size of the physical space
  SELECT tablespace_name, file_id, file_name,
  round (bytes / (1024 * 1024), 0) the 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 ;

4, view the control file
  select name from v $ controlfile;

5, view the log file
  select member from v $ logfile;

6, see table space usage
  SELECT SUM (bytes) / (1024 * 1024) AS the free_space, tablespace_name
  from DBA_FREE_SPACE
  Group by tablespace_name;

SELECT A.TABLESPACE_NAME,A.BYTES TOTAL,B.BYTES USED, C.BYTES FREE,
(B.BYTES*100)/A.BYTES "% USED",(C.BYTES*100)/A.BYTES "% FREE"
FROM SYS.SM$TS_AVAIL A,SYS.SM$TS_USED B,SYS.SM$TS_FREE C
WHERE A.TABLESPACE_NAME=B.TABLESPACE_NAME AND A.TABLESPACE_NAME=C.TABLESPACE_NAME;

7, view the database library objects
  the SELECT owner, object_type, Status, COUNT (*) COUNT #
from all_objects Group by owner, object_type, Status;

8, the version of the database to view 
  the Select the FROM Version Product_component_version
  the Where the SUBSTR (PRODUCT, 1,6) = 'the Oracle';

9, creation date, and archiving of Your Database
Select Created, Log_Mode, Log_Mode From V $ Database;

10, to capture the long run SQL
column username format a12
column opname format A16
column format A8 Progress

select username,sid,opname,
round(sofar*100 / totalwork,0) || '%' as progress,
time_remaining,sql_text
from v$session_longops , v$sql
where time_remaining <> 0
and sql_address = address
and sql_hash_value = hash_value


11、查看数据表的参数信息
SELECT partition_name, high_value, high_value_length, tablespace_name,
pct_free, pct_used, ini_trans, max_trans, initial_extent,
next_extent, min_extent, max_extent, pct_increase, FREELISTS,
freelist_groups, LOGGING, BUFFER_POOL, num_rows, blocks,
empty_blocks, avg_space, chain_cnt, avg_row_len, sample_size,
last_analyzed
FROM dba_tab_partitions
--WHERE table_name = :tname AND table_owner = :towner
ORDER BY partition_position

12. Review the transaction has not submitted
  the SELECT * from v $ locked_object;
  the SELECT * from v $ Transaction;

13.查找object为哪些进程所用
select
p.spid,
s.sid,
s.serial# serial_num,
s.username user_name,
a.type object_type,
s.osuser os_user_name,
a.owner,
a.object object_name,
decode(sign(48 - command),
1,
to_char(command), 'Action Code #' || to_char(command) ) action,
p.program oracle_process,
s.terminal terminal,
s.program program,
s.status session_status
from v$session s, v$access a, v$process p
where s.paddr = p.addr and
s.type = 'USER' and
a.sid = s.sid and
a.object='SUBSCRIBER_ATTR'
order by s.username, s.osuser

14.回滚段查看
select rownum, sys.dba_rollback_segs.segment_name Name, v$rollstat.extents
Extents, v$rollstat.rssize Size_in_Bytes, v$rollstat.xacts XActs,
v$rollstat.gets Gets, v$rollstat.waits Waits, v$rollstat.writes Writes,
sys.dba_rollback_segs.status status from v$rollstat, sys.dba_rollback_segs,
v$rollname where v$rollname.name(+) = sys.dba_rollback_segs.segment_name and
v$rollstat.usn (+) = v$rollname.usn order by rownum

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

17.查看等待(wait)情况
SELECT v$waitstat.class, v$waitstat.count count, SUM(v$sysstat.value) sum_value
FROM v$waitstat, v$sysstat WHERE v$sysstat.name IN ('db block gets',
'consistent gets') group by v$waitstat.class, v$waitstat.count

18. Check sga case
SELECT NAME, BYTES FROM SYS.V_ $ SGASTAT ORDER BY NAME ASC

19.查看catched object
SELECT owner, name, db_link, namespace,
type, sharable_mem, loads, executions,
locks, pins, kept FROM v$db_object_cache

20.查看V$SQLAREA
SELECT SQL_TEXT, SHARABLE_MEM, PERSISTENT_MEM, RUNTIME_MEM, SORTS,
VERSION_COUNT, LOADED_VERSIONS, OPEN_VERSIONS, USERS_OPENING, EXECUTIONS,
USERS_EXECUTING, LOADS, FIRST_LOAD_TIME, INVALIDATIONS, PARSE_CALLS, DISK_READS,
BUFFER_GETS, ROWS_PROCESSED FROM V$SQLAREA

21.查看object分类数量
select decode (o.type#,1,'INDEX' , 2,'TABLE' , 3 , 'CLUSTER' , 4, 'VIEW' , 5 ,
'SYNONYM' , 6 , 'SEQUENCE' , 'OTHER' ) object_type , count(*) quantity from
sys.obj$ o where o.type# > 1 group by decode (o.type#,1,'INDEX' , 2,'TABLE' , 3
, 'CLUSTER' , 4, 'VIEW' , 5 , 'SYNONYM' , 6 , 'SEQUENCE' , 'OTHER' ) union select
'COLUMN' , count(*) from sys.col$ union select 'DB LINK' , count(*) from

22。按用户查看object种类
select u.name schema, sum(decode(o.type#, 1, 1, NULL)) indexes,
sum(decode(o.type#, 2, 1, NULL)) tables, sum(decode(o.type#, 3, 1, NULL))
clusters, sum(decode(o.type#, 4, 1, NULL)) views, sum(decode(o.type#, 5, 1,
NULL)) synonyms, sum(decode(o.type#, 6, 1, NULL)) sequences,
sum(decode(o.type#, 1, NULL, 2, NULL, 3, NULL, 4, NULL, 5, NULL, 6, NULL, 1))
others from sys.obj$ o, sys.user$ u where o.type# >= 1 and u.user# =
o.owner# and u.name <> 'PUBLIC' group by u.name order by
sys.link$ union select 'CONSTRAINT' , count(*) from sys.con$

23.有关connection的相关信息
1)查看有哪些用户连接
select s.osuser os_user_name, decode(sign(48 - command), 1, to_char(command),
'Action Code #' || to_char(command) ) action, p.program oracle_process,
status session_status, s.terminal terminal, s.program program,
s.username user_name, s.fixed_table_sequence activity_meter, '' query,
0 memory, 0 max_memory, 0 cpu_usage, s.sid, s.serial# serial_num
from v$session s, v$process p where s.paddr=p.addr and s.type = 'USER'
order by s.username, s.osuser

2) The availability of resources occupation, etc. See the corresponding connection v.sid
SELECT n.name,
v.value,
n.class,
n.statistic #
from STATNAME n-V $,
V $ V SESSTAT
WHERE and 71 is v.sid =
V. # # = n.statistic statistic
the Order by n.class, n.statistic #

3)根据sid查看对应连接正在运行的sql
select /*+ PUSH_SUBQ */
command_type,
sql_text,
sharable_mem,
persistent_mem,
runtime_mem,
sorts,
version_count,
loaded_versions,
open_versions,
users_opening,
executions,
users_executing,
loads,
first_load_time,
invalidations,
parse_calls,
disk_reads,
buffer_gets,
rows_processed,
sysdate start_time,
sysdate finish_time,
'>' || address sql_address,
'N' status
from v$sqlarea
where address = (select sql_address from v$session where sid = 71)

twenty four. Query table space usage
select a.tablespace_name "table space name",
100-round ((NVL (b.bytes_free, 0) /a.bytes_alloc) * 100,2) "occupancy rate (%)",
round (A. bytes_alloc / 1024 / 1024,2) "capacity (M)",
round (NVL (b.bytes_free, 0) / 1024 / 1024,2) "idle (M)",
round ((a.bytes_alloc-NVL (B. bytes_free, 0)) / 1024 / 1024,2) " use (M)",
, largest "maximum expansion section (M)",
TO_CHAR (SYSDATE, 'YYYY-mm-dd HH24: mi The: SS') "sampling time"
from (SELECT f.tablespace_name,
SUM (f.bytes) bytes_alloc,
SUM (decode (f.autoextensible, 'YES', f.maxbytes, 'NO', f.bytes)) MaxBytes
from dba_data_files F
Group by tablespace_name) A,
(f.tablespace_name the SELECT,
SUM (f.


(select round(max(ff.length)*16/1024,2) Largest,
ts.name tablespace_name
from sys.fet$ ff, sys.file$ tf,sys.ts$ ts
where ts.ts#=ff.ts# and ff.file#=tf.relfile# and ts.ts#=tf.ts#
group by ts.name, tf.blocks) c
where a.tablespace_name = b.tablespace_name and a.tablespace_name = c.tablespace_name;

25. The extent lookup table space debris

select tablespace_name,count(tablespace_name) from dba_free_space group by tablespace_name
having count(tablespace_name)>10;

alter tablespace name coalesce;
alter table name deallocate unused;

create or replace view ts_blocks_v as
select tablespace_name,block_id,bytes,blocks,'free space' segment_name from dba_free_space
union all
select tablespace_name,block_id,bytes,blocks,segment_name from dba_extents;

select * from ts_blocks_v;

tablespace_name select, SUM (bytes), max (bytes), COUNT (block_id) from DBA_FREE_SPACE
Group by tablespace_name;


26. The index is monitored using
alter index & index_name monitoring usage; - start monitoring
alter index & index_name nomonitoring usage; - Canceled monitor
select from $ OBJECT_USAGE WHERE V * = & index_name index_name;

27. a request data file I / O distribution
SELECT df.name, phyrds, phywrts, phyblkrd, phyblkwrt, singleblkrds, readtim, writetim
from $ FILESTAT V FS, V $ DF dbfile
WHERE FS # # = df.file the Order .file by df.name;

28. the parameter values seek a hidden
COL ksppinm format A54
COL ksppstvl A54 format
the SELECT ksppinm, ksppstvl
from the X-$ ksppi PI, the X-CV $ ksppcv
the WHERE cv.indx = pi.indx and pi.ksppinm like '\ _% ' escape '\' and pi.ksppinm like '% ¶ sea% ';

29. A system seeking greater LATCH
SELECT name, SUM (the gets), SUM (Misses), SUM (Sleeps), SUM (WAIT_TIME)
from V $ LATCH_CHILDREN
Group name by HAVING SUM (the gets)> 50 Order by 2;

30 . seeking archive log switching frequency (the system may produce a very long time)
SELECT start_recid, START_TIME, end_recid, END_TIME, minutes from (SELECT Test. *, AS rownum RN
from (SELECT b.recid start_recid, TO_CHAR (b.first_time, 'YYYY-mm-dd HH24: mi The: SS') START_TIME,
a.recid end_recid, TO_CHAR (a.first_time, 'YYYY-mm-dd HH24: mi The: SS') END_TIME, round (((B-a.first_time .first_time) * 24) * 60, 2) minutes
from V $ LOG_HISTORY a, V B WHERE a.recid = $ LOG_HISTORY b.recid +. 1 and b.first_time
> sysdate- a specified value {} order by a.first_time desc ) test) y where y.rn <30

31.求回滚段正在处理的事务
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;

32.求出无效的对象
select 'alter procedure '||object_name||' compile;'
from dba_objects
where status='INVALID' and owner='&' 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;

- seeking the current session state of
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'

- generating an execution plan using dbms_xplan
EXPLAIN Plan statement_id SET = '& SQL_ID' for & SQL;
SELECT * from Table (dbms_xplan.display);

- seeking a transaction redo information (bytes)
SELECT s.name, m.value
from $ MYSTAT V m, V $ S STATNAME
WHERE m.statistic # = # s.statistic and s.name like 'the 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 ;

- seeking space usage log file
SELECT le.leseq current_log_sequence #, * 100 cp.cpodr_bno / le.lesiz percentage_full
from CP kcccp X $, $ 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

- seeking current transaction redo size
SELECT value
from $ MYSTAT V, V $ STATNAME
WHERE V = V $ # $ mystat.statistic statname.statistic # and $ statname.name = V 'the 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;

- seeking to create an object code that
column column_name the format A36
column the format SQL_TEXT A99
SELECT DBMS_METADATA.GET_DDL ( 'TABLE', '&. 1') from Dual;
SELECT DBMS_METADATA.GET_DDL ( 'the 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'

- seeking the current session of the SID, the SERIAL #
SELECT SID, Serial # from the session WHERE V $ = audsid the SYS_CONTEXT ( 'USERENV', 'the SESSIONID');

- seeking table space unused space
COL Mbytes the format 9999.9999
SELECT tablespace_name, SUM (bytes) / 1024/1024 Mbytes from Group DBA_FREE_SPACE 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';

- seeking Undefined index table
select table_name from user_tables where table_name not in (select table_name from user_ind_columns);

- perform common procedure
Exec print_sql ( 'SELECT COUNT (*) from Tab');
Exec show_space2 ( 'table_name');

- Memory seeking Free
SELECT * from V $ sgastat, WHERE name = 'Free Memory';
SELECT a.name, SUM (b.value) from $ STATNAME V A, V B WHERE a.statistic SESSTAT $ # = # b.statistic group by a.name;
look at who is using the rollback may have, or may have to look at a user using rollback segments,
identify the transaction rollback lead growing, then look at how to deal with it, can it commit, and then not
to see if we can kill it, etc., see the rollback segments currently in use user information and rollback information:
the SET LINESIZE 121
the SELECT r.name "ROLLBACK SEGMENT NAME", L .sid "the ORACLE the PID", p.spid "the PID the SYSTEM", s.username "the ORACLE USERNAME"
the FROM Lock V $ L, $ Process V P, V R & lt ROLLNAME $, $ V S the session
the 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;

- view the user information rollback
SELECT s.username, rn.name from the session $ V S, V $ Transaction T, R & lt ROLLSTAT V $, $ V RN ROLLNAME
WHERE s.saddr = t.ses_addr and t.xidusn = r.usn and r.usn = rn.usn

- execution plan
EXPLAIN Plan statement_id SET = 'A1' for &. 1;
- the execution plan view
SELECT LPAD ( '', 2 * (Level-. 1)) || Operation Operation, Options, the OBJECT_NAME, PLAN_TABLE position from
Start ID with = 0 and statement_id = 'a1' connect by prior id = parent_id and statement_id = 'a1'

--查看内存中存的使用
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';

- view the user memory condition
SELECT username, SUM (sharable_mem), SUM (persistent_mem), SUM (runtime_mem)
from sys.v_ $ SQLAREA A, B DBA_USERS
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;

- Check the library cache hit ratio
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;

- View dictionary hit ratio
select (sum (getmisses) / sum (gets)) ratio from v $ rowcache;

- View undo segment usage
the SELECT d.segment_name, extents, optsize, Shrinks, aveshrink, aveactive, d.status
the FROM $ ROLLNAME n-V, V $ ROLLSTAT S, D DBA_ROLLBACK_SEGS
the 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';

- find a process, and it tracked
the SELECT s.sid, s.serial # from v $ S the session, the WHERE s.paddr the p-v $ Process = p.addr and p.spid = & 1;
Exec the dbms_system. SET_SQL_TRACE_IN_SESSION (&. 1, & 2, to true);
Exec DBMS_SYSTEM.set_sql_trace_in_session (&. 1, & 2, to 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;

- seeking the object block number and file where the
SELECT segment_name, header_file, header_block
from DBA_SEGMENTS WHERE segment_name like '. 1 &';

- Back-off transaction occurs when an object request section number and the block
SELECT a.segment_name, a.header_file, a.header_block
from DBA_SEGMENTS A, B DBA_ROLLBACK_SEGS
WHERE a.segment_name = b.segment_name and b.segment_id = '. 1 &'

--9i online table redefinition
/ * If the online redefined table primary key need not create the primary key * /
Exec dbms_redefinition.can_redef_table ( 'Cybercafe', 'Announcement');
Create AS SELECT * from Table anno2 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 anno2 Table
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', options=>sys.dbms_logmnr.new);
exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22912.dbf', options=>sys.dbms_logmnr.addfile);
exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22913.dbf', options=>sys.dbms_logmnr.addfile);
exec sys.dbms_logmnr.add_logfile(logfilename=>'/home/oracle/oradata/esal/archive/1_22914.dbf', options=>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;

- permission-related dictionary
ALL_COL_PRIVS express authorization, user and is listed on the PUBLIC grantees
ALL_COL_PRIVS_MADE express authorization on the column, the user is the owner and the grantee
ALL_COL_RECD express authorization, user and PUBLIC is the grantee column
ALL_TAB_PRIVS express authorization on the object, the user is granted or the user or PUBLIC is the owner
ALL_TAB_PRIVS_MADE means permission on the object, the user is owner or grantor
ALL_TAB_PRIVS_RECD means permission on the object, user or PUBLIC is the grantee
DBA_COL_PRIVS database column All authorized on
DBA_ROLE_PRIVS display has been granted to users or other roles roles
DBA_SYS_PRIVS has been granted to a user or role-system permissions
for all permissions on DBA_TAB_PRIVS database objects
ROLE_ROLE_PRIVS display user has been granted the role of
ROLE_SYS_PRIVS display granted to a user through role system privileges
ROLE_TAB_PRIVS displayed by role object privileges granted to the user
SESSION_PRIVS display all system users can now take advantage of the privileges
USER_COL_PRIVS display the permissions on the column, the user is the owner, grantor or grantee
USER_COL_PRIVS_M ADE has been granted permission to display the column, the user is owner or grantor
USER_COL_PRIVS_RECD display the column has been granted permission, the user is owner or grantor
USER_ROLE_PRIVS display has been granted to users of all roles
USER_SYS_PRIVS display has been granted to users of all system privileges
USER_TAB_PRIVS displays all object privileges granted to users
USER_TAB_PRIVS_MADE display has been granted to another user's object privileges, the user is the owner
USER_TAB_PRIVS_RECD display has been granted to other user object permissions, the user is the grantee


--如何用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
);

Guess you like

Origin www.cnblogs.com/vmsysjack/p/12150810.html