常用DBA SQL[转]

裘宏骏 现供职务于浙江纵横通信服务集团,主持技术工作。负责浙江移动缴费券、空中充值、外呼外包等项目的技术工作。擅长大业务量业务支撑系统的设计、开发与后期运营,有3年以上的项目管理经验、4年以上的Oracle 设计开发与优化经验、5年以上的J2EE项目开发经验、熟悉C++,Java,Perl,PL/SQL,UNIX Shell,熟悉WebLogic,Borland VisiBorker 中间件、熟悉Spring、Hibernate、iBatis、JSF等开发框架,对移动通信系统有较系统和全面的认识。联系方式:MSN QiuHj1978@hotmail.com
http://dev2dev.bea.com.cn/blog/QiuHj/200710/23_524.html

查询最耗时的SQL
select t1.username,
t1.sid,
t1.opname,
t1.TARGET,
t2.sql_text,
t1.START_TIME,
t1.LAST_UPDATE_TIME
from v$session_longops t1, v$sql t2
where t1.sql_address = t2.address
and t1.sql_hash_value = t2.hash_value
order by t1.START_TIME

查找最占用资源的查询(基于V$SQL视图)
select * from (
select sql_text,
rank() over(order by buffer_gets desc) as rank_bufgets,
to_char(100 * ratio_to_report(buffer_gets) over(), '999.99') pct_bufgets
from v$sql ) where rank_bufgets <11


查找最占用资源的查询(基于V$SQLAREA 视图)
select b.username,
a.DISK_READS reads,
a.EXECUTIONS exec,
a.DISK_READS / decode(a.EXECUTIONS, 0, 1, a.EXECUTIONS) rds_exec_ratio,
a.sql_text
from v$sqlarea a, dba_users b
where a.PARSING_USER_ID = b.user_id
and a.DISK_READS > 100000
order by a.DISK_READS desc


按OS进程ID查询数据库联系信息
select v2.* from v$process v1,v$session v2 where v1.ADDR=v2.PADDR and v1.SPID=16860

按OS进程ID查询数据库当前的SQL
SELECT b.sid,b.serail#,a.sql_text FROM
v$sqltext a,
v$session b,
v$process c
WHERE
a.hash_value = b.sql_hash_value and
b.ADDR=c.PADDR and
AND c.SID='&sid'
ORDER BY piece ASC

如何查看各个表空间占用磁盘情况?
select a.tb_name,
b.tb_size/1024/1024 tb_size_M,
(b.tb_size - a.tb_free)/1024/1024 tb_used_M,
a.tb_free/1024/1024 tb_free_M,
((b.tb_size - a.tb_free)/b.tb_size)*100 tb_used_rate
from (select t.tablespace_name tb_name, sum(t.bytes) tb_free
from sys.dba_free_space t
group by t.tablespace_name) a,
(select b.tablespace_name tb_name, sum(b.bytes) tb_size
from dba_data_files b
group by b.tablespace_name) b
where a.tb_name = b.tb_name
order by tb_used_rate desc

查看表空间中Table类型和Index类型对象占用的空间大小
select segment_name , sum(bytes) as total, count(*) ext_quan
from dba_extents
where tablespace_name = '&tablespace_name'
and segment_type = 'TABLE' or segment_type = 'INDEX'
group by tablespace_name, segment_name
order by total desc

杀Session
alter system kill session 'sid,serial#'


分析索引的B树Level

execute Dbms_Stats.gather_index_stats('MSTORE','MSG_SEND_IDX1');

select blevel,index_name from user_indexes where index_name='MSG_SEND_IDX1';

一般情况下如果删除了20~25%的记录,需要重建索引降低 B树Level(应该在5以下)

查看当前Scheme下的索引类型

select * from user_indexes t

查看各个表空间的磁盘IO情况

select b.NAME, a.PHYRDS, a.PHYWRTS, a.READTIM, a.WRITETIM
from v$filestat a, v$dbfile b
where a.FILE# = b.FILE#
order by a.READTIM desc


查看各个表空间的extents的数量(一般情况下应小于1024)
select t.tablespace_name,sum(t.extents) from dba_segments t group by t.tablespace_name

查询当前当前Scheme的中最占空间的segment,对应占用空间比较大的Object应该增加其extents的大小(可以设置tablespace 为 的extents 为 uniform 格式,或者设置 objects 的 stoage 参数),以保证extents的数量不至于太多
select t.segment_name,
t.segment_type,
t.bytes,
t.blocks,
t.extents,
t.bytes / (1024 * 1024) as bytes_M
from user_segments t
order by t.bytes desc

查看数据缓存(db_cache)的命中率,至少应该在95%以上(从90%提高到98%会提高500%的性能)
select physical_reads,
db_block_gets,
consistent_gets,
(1 - physical_reads / (db_block_gets + consistent_gets)) * 100 hitratio
from (select sum(decode(name, 'physical reads', value, 0)) physical_reads,
sum(decode(name, 'db block gets', value, 0)) db_block_gets,
sum(decode(name, 'consistent gets', value, 0)) consistent_gets
from v$sysstat t)

查看
'sga_max_size','pga_aggregate_target','db_cache_size','shared_pool_size'
这个的参数的配置值

select t.name,t.VALUE/1024/1024 M from v$parameter t
where t.name in ('sga_max_size','pga_aggregate_target','db_cache_size','shared_pool_size'
)

查看数据字典缓存的命中率,至少应该在95%以上。如果低于95%增大SHARED_POOL_SIZE参数的值
select (1 - (sum(GETMISSES) / (sum(gets) + sum(GETMISSES)))) * 100 hit_Rate
from v$rowcache t
where t.GETS + t.GETMISSES <> 0;

使用单独的行参数缓存来查看共享池的使用情况(重点分析丢失率在10%的项目,tag 为×)
select t.PARAMETER,
t.GETS,
t.GETMISSES,
t.MODIFICATIONS,
t.FLUSHES,
(getmisses / decode(gets, 0, 1, gets)) "getmiss_ratio%",
(case
when (getmisses / decode(gets, 0, 1, gets)) > 0.1 then
'*'
else
''
end) tag
from v$rowcache t
where t.GETS + t.GETMISSES <> 0
order by "getmiss_ratio%" desc

查看库缓存(LibCache)的重载率(为0)和命中率(接近1) 【否则增大SHARED_POOL_SIZE】
select sum(pins) Hits,
sum(reloads) Misses,
((sum(reloads) / sum(pins)) * 100) "Reload%",
sum(pins) / (sum(pins) + sum(reloads)) * 100 "Hit Ratio%"
from v$librarycache
查看库缓存分项(LibCache)的重载率(低于15%)和命中率(接近1)【否则增大SHARED_POOL_SIZE】
select t.NAMESPACE,
t.PINS,
t.PINHITS,
t.PINHITRATIO "PinHitRatio%",
t.RELOADS / decode(t.PINS, 0, 1, t.PINS) "PinReLoadRatio%"
from v$librarycache t

使用可以内存来判断SHARED_POOL_SIZE是否设置正确(如果运行的时间足够长,且还有大量可用内存则无需增加SHARED_POOL_SIZE)
select to_number(b.VALUE) "Shared Pool Size",
a.BYTES / 1024 / 1024 "Free MB",
(a.BYTES / b.VALUE) * 100 "Percent Free%"
from v$sgastat a, v$parameter b
where a.NAME = 'free memory'
and b.NAME = 'shared_pool_size'
and a.POOL = 'shared pool';

posted on 2008-05-06 22:44  一江水  阅读(517)  评论(0编辑  收藏  举报