如何查找硬解析问题,找到未使用绑定变量的SQL---脚本

来源:互联网 发布:node服务器和appache 编辑:程序博客网 时间:2024/05/05 17:27


此脚本转自梁敬彬老师的《收获,不止SQL优化》,感谢老师的分享


构造一个未使用绑定变量并频繁执行的SQL如下:

drop table t purge;create table t(x int);select * from v$mystat where rownum=1;begin    for i in 1 .. 100000    loop        execute immediate        'insert into t values ( '||i||')';    end loop;    commit;end;/

找出未使用绑定变量的SQL。

drop table t_bind_sql purge;create table t_bind_sql as select sql_text,module from v$sqlarea;alter table t_bind_sql add sql_text_wo_constants varchar2(1000);create or replace function remove_constants( p_query in varchar2 ) return varchar2as    l_query long;    l_char  varchar2(10);    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;/update t_bind_sql set sql_text_wo_constants = remove_constants(sql_text);commit;

使用以下查询定位SQL

set linesize 266col  sql_text_wo_constants format a30col  module format  a30col  CNT format  999999select sql_text_wo_constants, module,count(*) CNT  from t_bind_sql group by sql_text_wo_constants,modulehaving count(*) > 100 order by 3 desc;SQL_TEXT_WO_CONSTANTS          MODULE                             CNT------------------------------ ------------------------------ -------INSERT INTO T VALUES ( @)      SQL*Plus                          1116


阅读全文
0 0
原创粉丝点击