如何理解oracle硬解析、软解析、软软解析

55次阅读
没有评论

共计 1948 个字符,预计需要花费 5 分钟才能阅读完成。

如何理解 oracle 硬解析、软解析、软软解析,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

硬解析过程:

    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 执行过程图如下:

关于如何理解 oracle 硬解析、软解析、软软解析问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-20发表,共计1948字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)