綁定變量

    當SQL 語句第一次執行時,在硬解析時,會創建parent cursor 和child cursor。當再次執行這個SQL時,Oracle 會先對SQL 語句進行hash 運算,產生一個hash 值,然後用這個HASH 值到buckets裏去查找,hash value 存放在parent cursor裏。 如果找到了,就去檢查child cursor。 如果可以重用這個child cursor,那麼就直接調用cursor裏的執行計劃。 如果不可重用,就會創建一個新的child cursor。 這個child cursor 的個數,就是version count。 不同parent cursor 對應的child cursor 越多,version count 就越高。 
    減少硬解析可以使用綁定變量,使用綁定變量之後,oracle解析的hash值一致,所以parent cursor相同,如果需要減少軟解析,可以設置session_cached_cursors爲更大值,這樣就能重用child cursor,10G數據庫在使用綁定變量時,最好不要收集直方圖。
    v$sqlarea是父遊標相關信息視圖,v$sql是子游標的。
    select sql_id,version_count from v$sqlarea order by 2 desc;
    version_count代表子游標個數
    select address,child_address,sql_text from v$sqlwheresql_id='dfnz16gh72bwg';

    查看綁定變量值:
    1.通過v$sql_bind_capture,dba_hist_sqlbind獲得sql的綁定變量
    SQL> set linesize 180
    SQL> col var for a20
    SQL> col var_value for a40
    SQL> select name var,value_string var_value,sql_id,hash_value,to_char(last_captured,'yyyy-mm-dd hh24:mi:ss') from v$sql_bind_capture where sql_id='dfnz16gh72bwg';

    VAR                  VAR_VALUE                                SQL_ID        HASH_VALUE TO_CHAR(LAST_CAPTUR
    -------------------- ---------------------------------------- ------------- ---------- -------------------
    :B                   100                                      dfnz16gh72bwg 3765514127 2014-01-14 09:42:35
    :A                   90                                       dfnz16gh72bwg 3765514127 2014-01-14 09:42:35
    :B                   100                                      dfnz16gh72bwg 3765514127 2014-01-14 09:30:29
    :A                   90                                       dfnz16gh72bwg 3765514127 2014-01-14 09:30:29
    V$SQL_BIND_CAPTURE只能捕獲where和having後的綁定變量:
    SQL> col value_string for a40
    SQL> select snap_id, name, position, value_string,last_captured,to_char(last_captured,'yyyy-mm-dd hh24:mi:ss') from dba_hist_sqlbind where    sql_id=   'dfnz16gh72bwg';

    SNAP_ID NAME                             POSITION VALUE_STRING                             LAST_CAPTURE TO_CHAR(LAST_CAPTUR
    ---------- ------------------------------ ---------- ---------------------------------------- ------------ -------------------
       273 :B                                      1 100                                      14-JAN-14    2014-01-14 09:42:35
       273 :A                                      2 90                                       14-JAN-14    2014-01-14 09:42:35

    2.通過dbms_xplans查看sql的綁定變量
    set linesize 180
    select * from TABLE(dbms_xplan.display_cursor('dfnz16gh72bwg',NULL,'PEEKED_BINDS'));

    Peeked Binds (identified by position):
    --------------------------------------
    1 - :B (NUMBER): 100

    select * from TABLE(dbms_xplan.display_awr('dfnz16gh72bwg',null,null,'PEEKED_BINDS'));

    PLAN_TABLE_OUTPUT
    ---------------------------------------------------------
    1 - :B (NUMBER): 100

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章