• <nav id="cwumo"><code id="cwumo"></code></nav>
  • <menu id="cwumo"><strong id="cwumo"></strong></menu>

    Oracle認證:ORACLE綁定變量BINDPEEKING

    時間:2024-08-25 15:57:01 Oracle認證 我要投稿
    • 相關推薦

    Oracle認證:ORACLE綁定變量BINDPEEKING

      ORACLE 在9i之后引入了bind peeking,通過bind peeking,oracle可以在硬解析的時候窺探綁定變量的值,并根據當前綁定變量的值生成執行計劃。在oracle 9i之前的版本中,oracle僅僅通過統計信息來生成執行計劃。

      下面看一下不同版本oracle下綁定變量對執行計劃的影響

      SQL> alter system flush shared_pool;

      系統已更改。

      SQL> alter system set optimizer_features_enable='8.1.7';

      系統已更改。

      SQL> var v number;

      SQL> exec :v := 1;

      PL/SQL 過程已成功完成。

      SQL> select count(*) from acs_test_tab where record_type = :v;

      COUNT(*)

      ----------

      1

      SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID3rg5r8sghcvb3, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = :v

      Plan hash value: 2956728990

      --------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost |

      --------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 3 |

      | 1 | SORT AGGREGATE | | 1 | 4 | |

      |* 2 | INDEX RANGE SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 2 | 8 | 3 |

      --------------------------------------------------------------------------------

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - access("RECORD_TYPE"=:V)

      已選擇47行。

      SQL> alter system flush shared_pool;

      系統已更改。

      SQL> alter system set optimizer_features_enable='11.2.0.3.1';

      系統已更改。

      SQL> var v number;

      SQL> exec :v := 1;

      PL/SQL 過程已成功完成。

      SQL> select count(*) from acs_test_tab where record_type = :v;

      COUNT(*)

      ----------

      1

      SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID3rg5r8sghcvb3, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = :v

      Plan hash value: 2956728990

      ------------------------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

      ------------------------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 3 (100)| |

      | 1 | SORT AGGREGATE | | 1 | 4 | | |

      |* 2 | INDEX RANGE SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 1 | 4 | 3 (0)| 00:00:01 |

      ------------------------------------------------------------------------------------------------

      Peeked Binds (identified by position):

      --------------------------------------

      1 - :V (NUMBER): 1 --綁定變量窺探

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - access("RECORD_TYPE"=:V)

      已選擇49行。

      SQL> alter system flush shared_pool;

      系統已更改。

      SQL> exec :v := 2;

      PL/SQL 過程已成功完成。

      SQL> select count(*) from acs_test_tab where record_type = :v;

      COUNT(*)

      ----------

      50000

      SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced'));

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID3rg5r8sghcvb3, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = :v

      Plan hash value: 2957754476

      ----------------------------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

      ----------------------------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 136 (100)| |

      | 1 | SORT AGGREGATE | | 1 | 4 || |

      |* 2 | INDEX FAST FULL SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 48031 | 187K| 136 (1)| 00:00:02 |

      ----------------------------------------------------------------------------------------------------

      Peeked Binds (identified by position):

      --------------------------------------

      1 - :V (NUMBER): 2 --綁定變量窺探,綁定變量會影響最初硬解析的執行計劃

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - filter("RECORD_TYPE"=:V)

      已選擇49行。

      使用綁定變量窺測的好處是:可以幫助優化器在第一次硬解析時選擇最優的執行計劃。但是同時這也是其弊端:在第一次硬解析后,后面發生的所有解析都會使用第一次硬解析生成的執行計劃,如果數據的分布是均勻的,問題不大,如果數據分布式傾斜的,那么第一次硬解析生成的執行計劃未必是最優的,甚至可能是非常糟糕的。例如:

      SQL> show parameter optimizer_feat

      NAME TYPE VALUE

      ------------------------------------ ----------- ------------------------------

      optimizer_features_enable string 11.2.0.3.1

      SQL> alter system flush shared_pool;

      系統已更改。

      SQL> var v number;

      SQL> exec :v := 2;

      PL/SQL 過程已成功完成。

      SQL> select count(*) from acs_test_tab where record_type = :v;

      COUNT(*)

      ----------

      50000

      SQL> select * from table(dbms_xplan.display_cursor);

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID3rg5r8sghcvb3, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = :v

      Plan hash value: 2957754476

      ----------------------------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

      ----------------------------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 136 (100)| |

      | 1 | SORT AGGREGATE | | 1 | 4 || |

      |* 2 | INDEX FAST FULL SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 48031 | 187K| 136 (1)| 00:00:02 |

      ----------------------------------------------------------------------------------------------------

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - filter("RECORD_TYPE"=:V)

      已選擇19行。

      SQL> exec :v := 1

      PL/SQL 過程已成功完成。

      SQL> select count(*) from acs_test_tab where record_type = :v;

      COUNT(*)

      ----------

      1

      SQL> select * from table(dbms_xplan.display_cursor);

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID3rg5r8sghcvb3, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = :v

      Plan hash value: 2957754476

      ----------------------------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

      ----------------------------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 136 (100)| |

      | 1 | SORT AGGREGATE | | 1 | 4 || |

      |* 2 | INDEX FAST FULL SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 48031 | 187K| 136 (1)| 00:00:02 |

      ----------------------------------------------------------------------------------------------------

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - filter("RECORD_TYPE"=:V)

      已選擇19行。

      SQL> select count(*) from acs_test_tab where record_type = 1;

      COUNT(*)

      ----------

      1

      SQL> select * from table(dbms_xplan.display_cursor);

      PLAN_TABLE_OUTPUT

      ----------------------------------------------------------------------------------------------------

      SQL_ID1pxm87f6yd0bp, child number 0

      -------------------------------------

      select count(*) from acs_test_tab where record_type = 1

      Plan hash value: 2956728990

      ------------------------------------------------------------------------------------------------

      | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

      ------------------------------------------------------------------------------------------------

      | 0 | SELECT STATEMENT | | | | 3 (100)| |

      | 1 | SORT AGGREGATE | | 1 | 4 | | |

      |* 2 | INDEX RANGE SCAN| ACS_TEST_TAB_RECORD_TYPE_I | 1 | 4 | 3 (0)| 00:00:01 |

      ------------------------------------------------------------------------------------------------

      Predicate Information (identified by operation id):

      ---------------------------------------------------

      2 - access("RECORD_TYPE"=1)

      已選擇19行。

      對于變量v的取值為1的執行計劃和采用常量1的執行計劃性能差距還是比較大的。

      總結:oracle在9i后引入變量窺測技術,該技術對于數據分布均勻的數據是非常合適的,但是對于分布傾斜的數據或者在OLAP系統中是不建議使用的。

    【Oracle認證:ORACLE綁定變量BINDPEEKING】相關文章:

    Oracle認證作用03-19

    Oracle認證簡介11-30

    Oracle最新認證03-09

    Oracle認證途徑03-20

    Oracle認證:Oracle控制件文件修復03-18

    Oracle認證:Oracle內存結構研究-PGA篇03-08

    Oracle認證:Oracle避免全表掃描方式03-08

    Oracle認證職業前景03-19

    Oracle認證考試技巧03-19

    日韩激情