--1)查询和定位数据库问题的SQL语句
--Oracle常用性能监控SQL语句.sql
--1查询锁表信息
select vp.SPID,
vs.P1,
vs.P1RAW,
vs.P2,
vs.EVENT,
vsql.SQL_TEXT,
vsql.SQL_FULLTEXT,
vsql.SQL_ID
from v$session vs, v$sql vsql, v$process vp
where vs.SQL_ID = vsql.SQL_ID
and vs.PADDR = vp.ADDR
and vs.WAIT_CLASS <> 'Idle'
order by vs.EVENT
select vp.spid,
lo.session_id,
lo.oracle_username,
lo.os_user_name,
ao.object_name
from v$process vp, v$session vs, v$locked_object lo, all_objects ao
where vp.addr = vs.paddr
and vs.process = lo.process
and lo.object_id = ao.object_id
--2解锁被锁的表(sid,serial#)
alter system kill session '324,50855';
--3当前活跃会话历史表
select *
from dba_hist_active_sess_history
where session_id = 324 --sid
order by sample_time desc;
--2)索引优化
--开启监控会话的语句
alter session set sql_trace = true;
dbms_system.set_sql_trace_in_session(sid, serial#, true);
dbms_session.set_sql_trace(true);
--索引分析
analyze index act_idx_task_procinst validate structure;
--索引使用分析 lfPercent>20 需要重建索引
select (idx.del_lf_rows_len / idx.lf_rows_len) * 100 as lfPercent
from index_stats idx;
--索引重建
alter index act_idx_task_procinst rebuild;
---ACT_HI_TASKINST (TRUNC(START_TIME_))
--获取sql_id
select vs.sql_id, vs.sql_fulltext, vs.runtime_mem
from v$sqlarea vs
where vs.sql_fulltext like '%papc_bdqd_orders%'
order by vs.last_load_time desc;
--根据SQL_ID得到SQL语句的执行计划
select plan_table_output
from table(dbms_xplan.display_cursor('0sb2f19wmm82u', '0', 'all'));
--3)分析sql性能问题需要关注的表
--根据SQL语句中的条件字段,确定每一项条件中返回的记录数有多少,
--做表连接时应该按照尽量用记录数(或者中间结果集)小的表作为驱动表的原则
--查询表信息 是否进行了收集统计信息
--表信息
select dt.owner, dt.table_name, dt.last_analyzed, dt.num_rows, dt.blocks
from dba_tables dt
where dt.owner = 'papcdata'
and dt.table_name = 'papc_bse_city';
--查询索引信息
--最理想的情况 CLUSTERING_FACTOR = 表的数据块总数 BLOCKS
--表的索引信息
select di.index_name,
di.index_type,
di.blevel,
di.leaf_blocks,
di.last_analyzed,
di.distinct_keys,
di.clustering_factor
from dba_indexes di
where di.owner = 'papcdata'
and di.table_name = 'papc_bse_city';
--查询表的列上使用了索引信息
select dic.index_name, dic.column_name, dic.column_position
from dba_ind_columns dic
where dic.table_owner = 'papcdata'
and dic.table_name = 'papc_bse_city';
--查询表中使用相关列的信息1
select dtcs.column_name,
dtcs.num_distinct,
dtcs.num_nulls,
dtcs.density,
dtcs.low_value,
dtcs.high_value,
dtcs.last_analyzed,
dtcs.histogram
from dba_tab_col_statistics dtcs
where dtcs.owner = 'papcdata'
and dtcs.table_name = 'papc_bse_city';
--查询表中使用相关列的信息2
select dtc.column_name,
dtc.num_distinct,
dtc.num_nulls,
dtc.density,
dtc.low_value,
dtc.high_value,
dtc.histogram,
dtc.last_analyzed
from dba_tab_columns dtc
where dtc.owner = 'papcdata'
and dtc.table_name = 'papc_bse_city';
/*+ INDEX(image_for_policy, IX_IMAGE_FOR_POLICY_CODE) */
exec dbms_stats.GATHER_TABLE_STATS(ownname => 'LIFEDATA',
tabname => 'IMAGE_FOR_POLICY',
estimate_percent => 100,
method_opt => 'FOR COLUMNS DOCUMENT_CODE SIZE 1');
select sql_id,
round(cpu_time / executions / 1000000, 2) cpu_time_s,
round(elapsed_time / executions / 1000000, 2) elapsed_time_s,
round(buffer_gets / executions, 2) buffer_gets,
executions,
parsing_user_id,
round(ROWS_PROCESSED / executions, 2) ROWS_PROCESSED
from v$sql
where sql_id in ('c8t01f69awtw9', 'f0kr8h3dq7dxm');
--数据缓冲命中率 Buffer Hit% < 95% db_cache_size 需要调整,出现db file sequential read 内存排序的占比 In - memory Sort% <> 100% 需调整PGA的大小
--共享池中sql解析的命中率 Library Hit% < 95% 加大共享池,使用绑定变量 修改cursor_shring 软解析占比 Soft Parse% < 95% 需要使用绑定变量 < 80%SQL 没被重用 执行次数占分析次数的百分比 Execute to Parse %值偏小,说明软、硬解析比例过大、快速软解析比例小