[20170620]11G 12c expand sql text.txt

[20170620]11G 12c expand sql text.txt

--//原来写的脚本只能分别在11g,12c单独使用.上午花一点点时间.把两者整合起来.
--//讨论链接:http://www.itpub.net/thread-2088981-1-1.html
--//再次感谢solomon_007的指点:

set long 20000
set serveroutput on
declare
    L_sqltext clob := null;
    l_version varchar2(3) := null;
    l_sql     clob := null;
    l_result  clob := null;
begin
    select regexp_replace(version,'\..*') into l_version from v$instance;
    select sql_fulltext into l_sqltext  from v$sqlarea where sql_id='&&1';

    if l_version = '11' then
       l_sql := 'begin
                   dbms_sql2.expand_sql_text( :a,:b );
                 end;';

    elsif l_version = '12' then

      l_sql := 'begin
                  dbms_utility.expand_sql_text(:a,:b);
                end;';
    end if;
    execute immediate l_sql using in l_sqltext,out l_result;
    dbms_output.put_line(l_result);
end;
/
set serveroutput off

--//继续拿原来的例子测试:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

create table t1( id, col1, col2, col3, col4, col5, padding )
cache
pctfree 95 pctused 5
-- compress for query low
as
select
        1, 100 , 200 , 300 , 400 , 500,rpad('x',100)
from
        all_objects
where
        rownum <= 50000 ;
       
execute dbms_stats.gather_table_stats(user,'t1',method_opt=>'for all columns size 1')
 
select
        /*+ gather_plan_statistics find this */
        id, max(val) as high_val
from
        t1
unpivot include nulls (
        val for source in (col1, col2, col3, col4, col5)
)
group by id
order by id
;

--//获得sql_id=2v7uzcnf4kj9s.

SCOTT@book> @ &r/expand_sql_text 2v7uzcnf4kj9s
SELECT "A1"."ID" "ID",MAX("A1"."VAL") "HIGH_VAL" FROM  ( (SELECT "A3"."ID" "ID","A3"."PADDING" "PADDING",'COL1' "SOURCE","A3"."COL1" "VAL" FROM "SCOTT"."T1" "A3") UNION ALL  (SELECT "A4"."ID" "ID","A4"."PADDING" "PADDING",'COL2' "SOURCE","A4"."COL2"
"VAL" FROM "SCOTT"."T1" "A4") UNION ALL  (SELECT "A5"."ID" "ID","A5"."PADDING" "PADDING",'COL3' "SOURCE","A5"."COL3" "VAL" FROM "SCOTT"."T1" "A5") UNION ALL  (SELECT "A6"."ID" "ID","A6"."PADDING" "PADDING",'COL4' "SOURCE","A6"."COL4" "VAL" FROM
"SCOTT"."T1" "A6") UNION ALL  (SELECT "A7"."ID" "ID","A7"."PADDING" "PADDING",'COL5' "SOURCE","A7"."COL5" "VAL" FROM "SCOTT"."T1" "A7")) "A1" GROUP BY "A1"."ID" ORDER BY "A1"."ID"

PL/SQL procedure successfully completed.

--//toad格式化看看:
/* Formatted on 2017/6/20 16:10:46 (QP5 v5.269.14213.34769) */
  SELECT "A1"."ID" "ID", MAX ("A1"."VAL") "HIGH_VAL"
    FROM ( (SELECT "A3"."ID" "ID"
                  ,"A3"."PADDING" "PADDING"
                  ,'COL1' "SOURCE"
                  ,"A3"."COL1" "VAL"
              FROM "SCOTT"."T1" "A3")
          UNION ALL
          (SELECT "A4"."ID" "ID"
                 ,"A4"."PADDING" "PADDING"
                 ,'COL2' "SOURCE"
                 ,"A4"."COL2" "VAL"
             FROM "SCOTT"."T1" "A4")
          UNION ALL
          (SELECT "A5"."ID" "ID"
                 ,"A5"."PADDING" "PADDING"
                 ,'COL3' "SOURCE"
                 ,"A5"."COL3" "VAL"
             FROM "SCOTT"."T1" "A5")
          UNION ALL
          (SELECT "A6"."ID" "ID"
                 ,"A6"."PADDING" "PADDING"
                 ,'COL4' "SOURCE"
                 ,"A6"."COL4" "VAL"
             FROM "SCOTT"."T1" "A6")
          UNION ALL
          (SELECT "A7"."ID" "ID"
                 ,"A7"."PADDING" "PADDING"
                 ,'COL5' "SOURCE"
                 ,"A7"."COL5" "VAL"
             FROM "SCOTT"."T1" "A7")) "A1"
GROUP BY "A1"."ID"
ORDER BY "A1"."ID";

--//可以看出使用unpivot实际上内部要全表扫描T1 5次.

时间: 2024-09-20 18:47:19

[20170620]11G 12c expand sql text.txt的相关文章

[20170726]11G 12c expand sql text 2.txt

[20170726]11G 12c expand sql text 2.txt --//原来写的脚本只能分别在11g,12c单独使用.上午花一点点时间.把两者整合起来. --//讨论链接:http://www.itpub.net/thread-2088981-1-1.html --//感谢solomon_007的指点,通过建立动态sql语句来实现.链接:http://blog.itpub.net/267265/viewspace-2141010/ --//qqjue给出建立建立类似c的宏来实现C

[20161208]11g直方图与char数据类型.txt

[20161208]11g直方图与char数据类型.txt --以前看tom大师的书提到过不要使用char数据类型,哪怕是char(1)也不要使用,最近看了几篇blob里面都提到了11g升级后会出现char数 --据类型直方图统计发生了变化,我重复别人的例子来说明.再次强调不要生产环境使用char类型. --参考链接:http://blog.dbi-services.com/histograms-on-character-strings-between-11-2-0-3-and-11-2-0-4

[20160704]从跟踪文件抽取sql语句.txt

[20160704]从跟踪文件抽取sql语句.txt --以前写过两篇,链接如下: http://blog.itpub.net/267265/viewspace-775398/ http://blog.itpub.net/267265/viewspace-748041/ 1个利用tkprof的recodr参数,可以记录跟踪整个sql语句序列,缺点就是丢失一些递归的sql语句.另外一个利用awk脚本抽取sql语句. 今天再写一个简单一点的脚本: 1.环境: SCOTT@book> @ &r/v

[20170114]12c varchar2类型直方图.txt

[20170114]12c varchar2类型直方图.txt --我曾经提到慎用nvarchar2数据类型,链接:http://blog.itpub.net/267265/viewspace-2120925/ --我那里提到数据类型nvarchar2类型,因为1个字符占用2个字节,这样如果前面16个字符重复很多,直方图的建立就是鸡肋, --毫无用处(因为分析仅仅对前面32个字节有效),12c 直方图支持更多类型: 高度直方图,频率直方图.混和类型(HYBRID). --看看12c关于直方图方面

[20150402]关于SQL Monitoring.txt

[20150402]关于SQL Monitoring.txt --今天学习sql monitor,oracle从11g开始,能够记录运行事件较长的sql语句,我自己在工作中很少使用这个功能,前几天看别人在优化时使用, -- 自己也学习学习,这个是学习笔记. 1.相关参数: SCOTT@test> @ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -----------

[20160815]查询相关表的sql语句.txt

[20160815]查询相关表的sql语句.txt --以前我要查询相关表的sql语句,我一般在toad下执行sga trace,直接在sql search text 输入表名,但是这里存在一个问题,仅仅检 --索sql语句的前1000个字节才有效. --如果我要查询的表在1000字符外,无法查询,通过例子说明: 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ---------

[20120803]11G SPM的学习1.txt

[20120803]11G SPM的学习1.txt     开始学习SQL Plan Management(SPM) ,11G开始提供SPM,在10g下我经常使用sql profile看一些bad sql语句,sql profile我觉得已经做的很好,有时候能够提供很好的建议.我开始学习SPM的时候感觉不习惯(也许是因为在toad下使用sql profile很简单)为什么oracle还有搞出SPM来,慢慢看资料,才明白其中一些细节.     我看过别人在从8i升级到9i的时候,出现性能波动,里面

[20130809]12c Clustering Factor(2).txt

[20130809]12c Clustering Factor(2).txt 参考以下链接,自己重复测试: http://richardfoote.wordpress.com/2013/05/14/clustering-factor-calculation-improvement-part-ii-blocks-on-blocks/ 1.测试环境: SQL> @ver BANNER                                                           

[20131128]12c的dbms_utility.expand_sql_text.txt

[20131128]12c的dbms_utility.expand_sql_text.txt SCOTT@ztest> @verBANNER                                                                              CON_ID-------------------------------------------------------------------------------- ----------Oracl