时间:2021-07-01 10:21:17 帮助过:3人阅读
-如何确定系统中是否存在绑定变量的情况:
首先创建一个表,用于存放整理过得数据:
create table t1 as select sql_text from v$sqlarea;
----V$SQLAREA本视图持续跟踪所有shared pool中的共享cursor,
--在shared pool中的每一条SQL语句都对应一列。本视图在分析SQL语句资源使用方面非常重要。
/**
*1.查看消耗资源最多的SQL:
SELECT hash_value, executions, buffer_gets, disk_reads, parse_calls
FROM V$SQLAREA
WHERE buffer_gets > 10000000 OR disk_reads > 1000000
ORDER BY buffer_gets + 100 * disk_reads DESC;
*/
2.查看某条SQL语句的资源消耗:
SELECT hash_value, buffer_gets, disk_reads, executions, parse_calls
FROM V$SQLAREA
WHERE hash_Value = 228801498 AND address = hextoraw(‘CBD8E4B0‘);
查找前10条性能差的sql语句
SELECT * FROM (select PARSING_USER_ID,EXECUTIONS,SORTS,COMMAND_TYPE,DISK_READS,sql_text FROM v$sqlarea
order BY disk_reads DESC )where ROWNUM<10 ;
--EXECUTIONS表示同一条SQL语句一共执行了多少次,SORTS表示排序的次数,DISK_READS表示物理读的数量。
分析性能差的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 ;
查询共享池中已经解析过的SQL语句及其相关信息
--EXECUTIONS 所有子游标的执行这条语句次数
--DISK_READS 所有子游标运行这条语句导致的读磁盘次数
--BUFFER_GETS 所有子游标运行这条语句导致的读内存次数
--Hit_radio 命中率
--Reads_per_run 每次执行读写磁盘数
笼统的说EXECUTIONS,BUFFER_GETS,Hit_radio越高表示读内存多,磁盘少是比较理想的状态,因此越高越好
另外两个越高读磁盘次数越多,因此低点好
选出最占用资源的查询 :
select b.username username,a.disk_reads reads,a.executions exec,
a.disk_reads/decode(a.executions,0,1,a.executions) rds_exec_ratio,
a.sql_text statement
from v$sqlarea a,dba_users b
where a.parsing_user_id=b.user_id
and a.disk_reads>10000
给表增加一个字段:
alter table t1 add sql_text_wo_constants varchar2(1000);
创建函数 remove_constants:
create or replace function
remove_constants(p_query in varchar2) return varchar2 as
l_query long;
l_char varchar2(1);
l_in_quotes boolean default false;
begin
for i in 1 .. length(p_query) loop
l_char :=substr(p_query,i,1);
if(l_char ="" and l_in_quotes) then
l_in_quotes= false;
else if(l_char ="" and not l_in_quotes) then
l_in_quotes= true;
l_query:=l_query||‘#‘;
end if;
if(not l_in_quotes) then
l_query := l_query||l_char;
end if;
end loop;
l_query := translate(l_query,‘0123456789‘,‘@@@@@@@@@@‘);
for i in 0..8 loop
l_query := replace(l_query,lpad(‘@‘,10-i,‘@‘),‘@‘);
l_query := replace(l_query,lpad(‘ ‘,10-i,‘ ‘),‘ ‘);
end loop;
return upper(l_query);
end;
/
----下面是如何使用这个函数
将v$sql视图中的数据用remove_constants处理后,更新到t1表中:
update t1 set sql_text_wo_constants = remove_constants(sql_text);
--查出除了谓语条件不同的SQL语句和它们的执行次数
select sql_text_wo_constants,count(*) from t1
group by sql_text_wo_constants having count(*)>100 order by 2;
---使用一个循环执行1000次某条SQL,每次执行时只有谓语不同:
ed
begin
for i in 1..1000 loop
execute immediate ‘select *from t where rm=‘||i;
end loop;
end;
select sql_text_wo_constants,count(*) from t1
group by sql_text_wo_constants
having count(*)>100
order by 2;
V$SQLAREA
版权声明:本文为博主原创文章,未经博主允许不得转载。
oracle v$sqlarea 分析SQL语句使用资源情况 确认是否绑定变量
标签: