--监控索引是否使用
alter index &index_name monitoring usage;
alter index &index_name nomonitoring usage;
select * from v$object_usage where index_name = &index_name;
--求数据文件的I/O分布
selectdf.name,phyrds,phywrts,phyblkrd,phyblkwrt,singleblkrds,readtim,writetim
from v$filestat fs,v$dbfile df
where fs.file#=df.file# order bydf.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.ksppinmlike '\_%' escape '\' and pi.ksppinm like '%meer%';
--求系统中较大的latch
selectname,sum(gets),sum(misses),sum(sleeps),sum(wait_time)
from v$latch_children
group by name having sum(gets) > 50order by 2;
--求归档日志的切换频率(生产系统可能时间会很长)
select start_recid,start_time,end_recid,end_time,minutesfrom (select test.*, rownum as rn
from (select b.recidstart_recid,to_char(b.first_time,'yyyy-mm-dd hh24:mi:ss') start_time,
a.recidend_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 bwhere a.recid=b.recid+1 and b.first_time > sysdate - 1
order by a.first_time desc) test) ywhere y.rn < 30
--求回滚段正在处理的事务
selecta.name,b.xacts,c.sid,c.serial#,d.sql_text
from v$rollname a,v$rollstatb,v$session c,v$sqltext d,v$transaction e
where a.usn=b.usn and b.usn=e.xidusnand c.taddr=e.addr
and c.sql_address=d.address andc.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' andwner='&' and object_type in ('PACKAGE','PACKAGE BODY');
/
selectowner,object_name,object_type,status from dba_objects where status='INVALID';
--求process/session的状态
selectp.pid,p.spid,s.program,s.sid,s.serial#
from v$process p,v$session s wheres.paddr=p.addr;
--求当前session的状态
select sn.name,ms.value
from v$mystat ms,v$statname sn
where ms.statistic#=sn.statistic# andms.value > 0;
--求表的索引信息
select ui.table_name,ui.index_name
from user_indexes ui,user_ind_columnsuic
where ui.table_name=uic.table_name andui.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' andconstraint_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_constraintschild,user_constraints parent,
user_cons_columns cp,user_cons_columnspc
where child.constraint_type = 'R' andchild.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 bychild.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_VALUEfrom user_tab_partitions where table_name='&table_name'
--使用dbms_xplan生成一个执行计划
explain plan set statement_id ='&sql_id' for &sql;
select * fromtable(dbms_xplan.display);
--求某个事务的重做信息(bytes)
select s.name,m.value
from v$mystat m,v$statname s
where m.statistic#=s.statistic# ands.name like '%redo size%';
--求cache中缓存超过其5%的对象
selecto.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) > (selectto_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 wherep.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_objectso,v$session s
where l.object_id=o.object_id andl.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_objectso
where l.sid = s.sid and l.id1 =o.object_id(+) and s.username is not null ;
--求等待的事件及会话信息/求会话的等待及会话信息
selectse.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 andse.sid=s.sid and s.status='ACTIVE' and se.event not like '%SQL*Net%' order bys.username;
selects.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 andsw.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 eventnot like '%rdbms%' and event not like '%mon%' order by event;
select name,wait_time from v$latch lwhere exists (select 1 from (select sid,event,p1text, p1, p2text, p2, p3text,p3
from v$session_wait
where event not like '%SQL%' and eventnot 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 ando.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_waitseconds, w.state
from v$session_wait w, v$session s,dba_extents o
where w.event in (select name fromv$event_name where parameter1 = 'file#'
and parameter2 = 'block#' and name notlike '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_$bgprocessb,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 andb.statistic#=5;
--求DISK READ较多的SQL
select st.sql_text from v$sqls,v$sqltext st
where s.address=st.address ands.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 andsort1.blocks > 200;
--求对象的创建代码
column column_name format a36
column sql_text format a99
selectdbms_metadata.get_ddl('TABLE','&1') from dual;
selectdbms_metadata.get_ddl('INDEX','&1') from dual;
--求表的索引
set linesize 131
selecta.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 anda.table_name='&1';
求索引中行数较多的
selectindex_name,blevel,num_rows,CLUSTERING_FACTOR,status from user_indexes wherenum_rows > 10000 and blevel > 0
selecttable_name,index_name,blevel,num_rows,CLUSTERING_FACTOR,status fromuser_indexes where status <> 'VALID'
--求当前会话的SID,SERIAL#
select sid, serial# from v$sessionwhere audsid = SYS_CONTEXT('USERENV','SESSIONID');
--求表空间的未用空间
col mbytes format 9999.9999
selecttablespace_name,sum(bytes)/1024/1024 mbytes from dba_free_space group by tablespace_name;
--求表中定义的触发器
selecttable_name,index_type,index_name,uniqueness from user_indexes wheretable_name='&1';
select trigger_name from user_triggerswhere table_name='&1';
--求未定义索引的表
select table_name from user_tableswhere table_name not in (select table_name from user_ind_columns);
--执行常用的过程
exec print_sql('select count(*) fromtab');
exec show_space2('table_name');
--求free memory
select * from v$sgastat wherename='free memory';
select a.name,sum(b.value) fromv$statname a,v$sesstat b where a.statistic# = b.statistic# group by a.name;
查看一下谁在使用那个可以得回滚段,或者查看一下某个可以得用户在使用回滚段,找出领回滚段不断增长的事务,再看看如何处理它,是否可以将它commit,再不行就看能否kill它,查看当前正在使用的回滚段的用户信息和回滚段信息:
set linesize 121
SELECT r.name "ROLLBACK SEGMENTNAME ",l.sid "ORACLE PID",p.spid "SYSTEM PID",s.username "ORACLE USERNAME"
FROM v$lock l, v$process p, v$rollnamer, v$session s
WHERE l.sid = p.pid(+) AND s.sid=l.sidAND TRUNC(l.id1(+)/65536) = r.usn AND l.type(+) = 'TX' AND l.lmode(+) = 6 ORDERBY r.name;
--查看用户的回滚段的信息
select s.username, rn.name fromv$session s, v$transaction t, v$rollstat r, v$rollname rn
where s.saddr = t.ses_addr andt.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 fromplan_table
start with id=0 and statement_id='a1'connect by prior id=parent_id and statement_id='a1'
--查看内存中存的使用
selectdecode(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) "OnDirty",count(*) "Total"
from x$bh group bydecode(greatest(class,10),10,decode(class,1,'Data',2,'Sort',4,'Header',to_char(class)),'Rollback');
--查看表空间状态
selecttablespace_name,extent_management,segment_space_management fromdba_tablespaces;
selecttable_name,freelists,freelist_groups from user_tables;
--查看系统请求情况
SELECT DECODE (name, 'summed dirtywrite queue length', value)/
DECODE (name, 'write requests', value)"Write Request Length"
FROM v$sysstat WHERE name IN ( 'summeddirty 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 andb.statistic# = 41 and c.statistic# = 42;
SELECT name,(1-(physical_reads/(db_block_gets+consistent_gets)))*100 H_RATIO FROMv$buffer_pool_statistics;
--查看内存使用情况
selectleast(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)))*100avail_pool_pct
from v$sgastat a, v$parameter b where(a.pool='shared pool' and a.name not in ('free memory')) andb.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_idgroup by username;
--查看对象的缓存情况
selectOWNER,NAMESPACE,TYPE,NAME,SHARABLE_MEM,LOADS,EXECUTIONS,LOCKS,PINS,KEPT
from v$db_object_cache where type notin ('NOT LOADED','NON-EXISTENT','VIEW','TABLE','SEQUENCE')
and executions>0 and loads>1 andkept='NO' order by owner,namespace,type,executions desc;
select type,count(*) fromv$db_object_cache group by type;
--查看库缓存命中率
select namespace,gets, gethitratio*100gethitratio,pins,pinhitratio*100 pinhitratio,RELOADS,INVALIDATIONS fromv$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))*100u_hash_ratio
from dba_users a, v$sqlarea b wherea.user_id=b.parsing_user_id group by a.username;
--查看字典命中率
select (sum(getmisses)/sum(gets))ratio from v$rowcache;
--查看undo段的使用情况
SELECTd.segment_name,extents,optsize,shrinks,aveshrink,aveactive,d.status
FROM v$rollname n,v$rollstats,dba_rollback_segs d
WHERE d.segment_id=n.usn(+) andd.segment_id=s.usn(+);
--无效的对象
select owner,object_type,object_namefrom dba_objects where status='INVALID';
select constraint_name,table_name from dba_constraints where status='INVALID';
--求出某个进程,并对它进行跟踪
select s.sid,s.serial# from v$sessions,v$process p where s.paddr=p.addr and p.spid=&1;
execdbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,true);
execdbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,false);
--求出锁定的对象
select do.object_name,session_id,process,locked_mode
from v$locked_object lo, dba_objectsdo 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' ANDp2.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_namelike '&1';
--求对象发生事务时回退段及块号
selecta.segment_name,a.header_file,a.header_block
from dba_segments a,dba_rollback_segsb
where a.segment_name=b.segment_nameand b.segment_id='&1'
--9i的在线重定义表
/*如果在线重定义的表没有主键需要创建主键*/
execdbms_redefinition.can_redef_table('cybercafe','announcement');
create table anno2 as select * fromannouncement
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
execdbms_redefinition.abort_redef_table('cybercafe','announcement','anno2');
--常用的logmnr脚本(cybercafe)
exec sys.dbms_logmnr_d.build(dictionary_filename=>'esal',dictionary_location =>'/home/oracle/logmnr');
execsys.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 * fromv$logmnr_contents;
--与权限相关的字典
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 sizeauto',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 assize_clause := SIZE {integer | REPEAT | AUTO | SKEWONLY}
integer--Number of histogram buckets.Must be in the range [1,254].
REPEAT--Collects histograms only onthe columns that already have histograms.
AUTO--Oracle determines the columns tocollect histograms based on data distribution and the workload of the columns.
SKEWONLY--Oracle determines thecolumns to collect histograms based on the data distribution of the columns
*/
分享到:
相关推荐
数据库管理员日常工作中必备的sql列表:
这些SQL语句只是数据库管理员日常工作中的一小部分,实际工作中的SQL需求可能更复杂,需要根据具体的数据库环境和业务需求进行调整和定制。理解并熟练使用这些SQL语句,能够帮助DBA更有效地管理和优化数据库系统。
数据库管理员在日常工作中需要掌握多种SQL技能,以确保数据库的稳定运行和高效管理。以下是数据库管理员应当熟练使用的SQL命令及其应用场景的详细介绍: 1. 监控索引使用情况:数据库管理员需要定期检查索引是否被...
Oracle数据库管理员指南是一本深入解析Oracle数据库管理的权威著作,对于从事大型数据库开发与分析的专业人士来说,它无疑是必备的参考资料。Oracle数据库是全球广泛使用的数据库管理系统之一,尤其在企业级应用中...
以下是一些在数据库管理员日常工作中常见的SQL语句及其用途: 1. 监控索引使用:`ALTER INDEX &index_name MONITORING USAGE` 和 `ALTER INDEX &index_name NOMONITORING USAGE` 用于开启或关闭对特定索引使用情况...
手册涵盖了Oracle数据库的基础知识、安装配置、性能优化、备份恢复、安全控制等多个重要方面,是DBA进行日常工作的必备参考资料。 一、Oracle数据库基础 Oracle数据库是全球广泛使用的大型关系型数据库管理系统之一...
Oracle数据库管理员技术指南是针对那些想要深入理解和管理Oracle数据库的专业人士的重要参考资料。Oracle数据库作为全球广泛使用的数据库管理系统,其复杂性和功能强大性使得熟练掌握其管理技术至关重要。本指南将...
以上这些SQL语句是Oracle数据库管理员日常工作中不可或缺的工具,它们能够帮助管理员有效地监控数据库健康状况,优化性能,及时发现和解决问题。熟悉并掌握这些命令,将大大提高工作效率和数据库的稳定性。
以下是一些Oracle DBA在日常工作中必备的SQL技能和知识要点: 1. **数据文件I/O分布**:理解数据文件的I/O分布是优化数据库性能的基础。DBA需要通过SQL查询监控各个数据文件的读写情况,例如使用`V$DATAFILE`和`V$...
通过"ORACLe数据库管理员教程.ppt"这份资料,你将获得实践操作的指导,深入理解Oracle DBA的角色和职责,并掌握必备的技能,为你的职业生涯奠定坚实的基础。不断学习和实践,才能成为一名优秀的Oracle数据库管理员。
Oracle数据库管理员教程是针对那些希望深入理解Oracle数据库管理和运维的专业人士设计的。Oracle数据库作为全球广泛使用的数据库管理系统,其管理员的角色至关重要,他们负责确保数据库的高效运行、数据的安全存储...
Oracle数据库管理员指南主要涵盖的内容...以上只是Oracle数据库管理员需要掌握的一部分知识点,实际工作中还会涉及更多细节和高级特性。通过深入学习和实践,管理员能够有效地管理Oracle数据库,确保其稳定、高效运行。
Oracle数据库管理系统是全球广泛使用的大型关系型数据库系统之一,尤其在企业级应用中占据着重要地位。Oracle数据库管理涉及的内容非常广泛,包括数据库设计、安装配置、性能优化、备份恢复、安全控制等多个方面。...
Oracle数据库管理员教程是一份专为想要深入理解和掌握Oracle数据库管理技术的人士准备的学习资源。Oracle数据库是全球广泛使用的大型企业级关系型数据库管理系统,它的功能强大且复杂,因此,对于数据库管理员(DBA...
Oracle数据库管理员技术指南是深入学习和掌握Oracle数据库管理的关键资源,尤其对于那些希望在IT行业中从事DBA(数据库管理员)工作的人来说,这是必备的知识库。Oracle数据库是全球广泛使用的数据库系统之一,它...