ITPub博客

首页 > 数据库 > Oracle > 彻底弄懂oracle硬解析、软解析、软软解析

彻底弄懂oracle硬解析、软解析、软软解析

原创 Oracle 作者:zhangsharp20 时间:2017-11-18 14:41:59 0 删除 编辑

硬解析和软解析有相同的一步,而软软解析与硬解析、软解析完全不一样。先来说下理论上的东西,然后来做个实验。

硬解析过程:

    1.语法、语义及权限检查;

    2.查询转换(通过应用各种不同的转换技巧,会生成语义上等同的新的SQL语句,如count(1)会转为count(*));

    3.根据统计信息生成执行计划(找出成本最低的路径,这一步比较耗时);

    4.将游标信息(执行计划)保存到库缓存。

软解析过程:

    1.语法、语义及权限检查;

    2.将整条SQL hash后从库缓存中执行计划。

    软解析对比硬解析省了三个步骤。

软软解析过程:

    要完全理解软软解析先要理解游标的概念,当执行SQL时,首先要打开游标,执行完成后,要关闭游标,游标可以理解为SQL语句的一个句柄。

在执行软软解析之前,首先要进行软解析,MOS上说执行3次的SQL语句会把游标缓存到PGA,这个游标一直开着,当再有相同的SQL执行时,则跳过解析的所有过程直接去取执行计划。

SQL> drop table test purge;
SQL> alter system flush shared_pool;
SQL> create table test as select * from dba_objects where 1<>1;
SQL> exec dbms_stats.gather_table_stats(user,'test');

硬解析:
SQL> select * from test where object_id=20;
未选定行
SQL> select * from test where object_id=30;
未选定行
SQL> select * from test where object_id=40;
未选定行
SQL> select * from test where object_id=50;
未选定行

软解析:
SQL> var oid number;
SQL> exec :oid:=20;
SQL> select * from test where object_id=:oid;
未选定行
SQL> exec :oid:=30;
SQL> select * from test where object_id=:oid;
未选定行
SQL> exec :oid:=40;
SQL> select * from test where object_id=:oid;
未选定行
SQL> exec :oid:=50;
SQL> select * from test where object_id=:oid;
未选定行
软软解析:
SQL> begin
         for i in 1..4 loop
         execute immediate 'select * from test where object_id=:i' using i;
         end loop;
         end;
     /
     
SQL> col sql_text format a40   
SQL> select sql_text,s.PARSE_CALLS,loads,executions from v$sql s
        where sql_text like 'select * from test where object_id%'
        order by 1,2,3,4;
SQL_TEXT                                 PARSE_CALLS      LOADS EXECUTIONS
---------------------------------------- ----------- ---------- ----------
select * from test where object_id=20              1          1          1
select * from test where object_id=30              1          1          1
select * from test where object_id=40              1          1          1
select * from test where object_id=50              1          1          1

select * from test where object_id=:i              1          1          4
select * from test where object_id=:oid            4          1          4  

可以看到软解析与软软解析相比,软软解析只是解析一次。

字段解释:
PARSE_CALLS  解析的次数

LOADS 硬解析的次数

EXECUTIONS 执行的次数




SQL执行过程图如下:





参考文章:http://blog.csdn.net/stevendbaguo/article/details/17267161

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29802484/viewspace-2147442/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-08-12

  • 博文量
    382
  • 访问量
    801412