如何查看共享池中哪些sql没有绑定变量--from ask tom

来源:互联网 发布:在家当淘宝客服 编辑:程序博客网 时间:2024/05/18 01:51

1.首先创建一个表


create table t1 as select sql_text from v$sqlarea;


2.给表增加一个字段


alter table t1 add sql_text_wo_constants varchar2(1000);

 

3.创建函数remove_constants


create or replace function remove_constants(p_query in varchar2)
  return varchar2 is
  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 remove_constants;


4.将v$sql视图中的数据用该函数处理后更新到t1表中


update t1 set sql_text_wo_constants =remove_constants(sql_text);


5.查出除了谓词条件不同的sql语句和他们的执行次数:


select sql_text_wo_constants,count(1) from t1 group by  sql_text_wo_constants having count(1)>2(--这里数字可以根据需要修改) order by 2;


6.可以看到输出结果了,执行次数大于2次的有6条,其中谓词条件被@代替,这样通过这个函数,就可以很轻易的找到共享池中哪些sql没有绑定变量。


0 0
原创粉丝点击