您的位置:首页 > 数据库

remove_constans() ——检查共享池总的sql运行情况。

2012-06-25 17:14 323 查看
关于如何确定系统中是否存在绑定的情况,ASKTOM网站提供了一个不错的函数remove_constans()来检查共享池总的sql运行情况。

SQL> create table test as select * from v$sqlarea;  --创建测试表test,并把v$sqlarea中的数据复制一份。

表已创建。

SQL> alter table test add sql_text_wo_constants varchar2(1000);  --给test表增加一个字段。

表已更改。
创建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;
elsif (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;
/


用函数更新sql_text_wo_constants 字段

SQL> update test set sql_text_wo_constants = remove_constants(sql_text);

已更新1552行。

SQL> commit;

提交完成。
查出除了谓词条件不同的sql语句和他们的执行次数,在这里是查询sql没有被重用超过100次的sql语句:

select sql_text_wo_constants, count(*)
from test
group by sql_text_wo_constants
having count(*) > 100
order by 2;


下面是一个例子:

SQL> begin
2  for i in 1..1000 loop
3  execute immediate 'select * from t where rn = '||i;
4  end loop;
5  end;
6  /

PL/SQL 过程已成功完成。

SQL> drop table test purge;

表已删除。

SQL> create table test as select * from v$sqlarea;

表已创建。

SQL> alter table test add sql_text_wo_constants varchar2(1000);

表已更改。

SQL> update test set sql_text_wo_constants = remove_constants(sql_text);

已更新1171行。

SQL> commit;

提交完成。

SQL> select sql_text_wo_constants, count(*)
2    from test
3   group by sql_text_wo_constants
4  having count(*) > 100
5   order by 2;

SQL_TEXT_WO_CONSTANTS                                                                                  COUNT(*)
---------------------------------------------------------------------------------------------------- ----------
SELECT * FROM T WHERE RN = @                                                                               1000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐