oracle bind value peeking繫結變數窺視

wisdomone1發表於2012-11-23
SQL> var a number;
SQL> exec :a:=10;
PL/SQL 過程已成功完成。
SQL> select * from dept where deptno=:a;
    DEPTNO DNAME          LOC                                                  
---------- -------------- -------------                                        
        10 ACCOUNTING     NEW YORK                                             
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED'));
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
SQL_ID  6a2y9jbwu5fz9, child number 0                                          
-------------------------------------                                          
select * from dept where deptno=:a                                             
                                                                               
Plan hash value: 2852011669                                                    
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Tim
e     |                                                                        
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
|   0 | SELECT STATEMENT            |         |       |       |     1 (100)|   
      |                                                                        
                                                                               
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    19 |     1   (0)| 00:
00:01 |                                                                        
                                                                               
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      |                                                                        
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
                                                                               
Query Block Name / Object Alias (identified by operation id):                  
-------------------------------------------------------------                  
                                                                               
   1 - SEL$1 /                                                       
   2 - SEL$1 /                                                       
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
Outline Data                                                                   
-------------                                                                  
                                                                               
  /*+                                                                          
      BEGIN_OUTLINE_DATA                                                       
      IGNORE_OPTIM_EMBEDDED_HINTS                                              
      OPTIMIZER_FEATURES_ENABLE('11.2.0.1')                                    
      DB_VERSION('11.2.0.1')                                                   
      ALL_ROWS                                                                 
      OUTLINE_LEAF(@"SEL$1")                                                   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      INDEX_RS_ASC(@"SEL$1" " ("DEPT"."DEPTNO"))                  
      END_OUTLINE_DATA                                                         
  */                                                                           
                                                                               
Peeked Binds (identified by position):                                         
--------------------------------------                                         
                                                                               
   1 - :A (NUMBER): 10                                                         
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
   2 - access("DEPTNO"=:A)                                                     
                                                                               
Column Projection Information (identified by operation id):                    
-----------------------------------------------------------                    
                                                                               
   1 - "DEPTNO"[NUMBER,22], "DEPT"."DNAME"[VARCHAR2,14],                       
       "DEPT"."LOC"[VARCHAR2,13]                                               
   2 - "DEPT".ROWID[ROWID,10], "DEPTNO"[NUMBER,22]                             
                                                                               
已選擇51行。
SQL> exec :a:=20;
PL/SQL 過程已成功完成。
 
SQL> select * from dept where deptno=:a;
    DEPTNO DNAME          LOC                                                  
---------- -------------- -------------                                        
        20 RESEARCH       DALLAS                                               
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED'));
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
SQL_ID  6a2y9jbwu5fz9, child number 0                                          
-------------------------------------                                          
select * from dept where deptno=:a                                             
                                                                               
Plan hash value: 2852011669                                                    
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Tim
e     |                                                                        
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
|   0 | SELECT STATEMENT            |         |       |       |     1 (100)|   
      |                                                                        
                                                                               
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    19 |     1   (0)| 00:
00:01 |                                                                        
                                                                               
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      |                                                                        
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
                                                                               
Query Block Name / Object Alias (identified by operation id):                  
-------------------------------------------------------------                  
                                                                               
   1 - SEL$1 /                                                       
   2 - SEL$1 /                                                       
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
Outline Data                                                                   
-------------                                                                  
                                                                               
  /*+                                                                          
      BEGIN_OUTLINE_DATA                                                       
      IGNORE_OPTIM_EMBEDDED_HINTS                                              
      OPTIMIZER_FEATURES_ENABLE('11.2.0.1')                                    
      DB_VERSION('11.2.0.1')                                                   
      ALL_ROWS                                                                 
      OUTLINE_LEAF(@"SEL$1")                                                   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      INDEX_RS_ASC(@"SEL$1" " ("DEPT"."DEPTNO"))                  
      END_OUTLINE_DATA                                                         
  */                                                                           
                                                                               
Peeked Binds (identified by position):                                         
--------------------------------------                                         
                                                                               
   1 - :A (NUMBER): 10         --小結:說明對於繫結變數,再次執行繫結變數的值可能還是首次執行繫結變數的值,這樣可能選擇差的執行計劃                                                
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
   2 - access("DEPTNO"=:A)                                                     
                                                                               
Column Projection Information (identified by operation id):                    
-----------------------------------------------------------                    
                                                                               
   1 - "DEPTNO"[NUMBER,22], "DEPT"."DNAME"[VARCHAR2,14],                       
       "DEPT"."LOC"[VARCHAR2,13]                                               
   2 - "DEPT".ROWID[ROWID,10], "DEPTNO"[NUMBER,22]                             
                                                                               
已選擇51行。
sqlplus as sysdba
alter system flush shared_pool;
conn
SQL> exec :a:=20;
PL/SQL 過程已成功完成。
SQL> select * from dept where deptno=:a;
    DEPTNO DNAME          LOC                                                  
---------- -------------- -------------                                        
        20 RESEARCH       DALLAS                                               
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ADVANCED'));
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
SQL_ID  6a2y9jbwu5fz9, child number 0                                          
-------------------------------------                                          
select * from dept where deptno=:a                                             
                                                                               
Plan hash value: 2852011669                                                    
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Tim
e     |                                                                        
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
|   0 | SELECT STATEMENT            |         |       |       |     1 (100)|   
      |                                                                        
                                                                               
|   1 |  TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    19 |     1   (0)| 00:
00:01 |                                                                        
                                                                               
|*  2 |   INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      |                                                                        
                                                                               
--------------------------------------------------------------------------------
-------                                                                        
                                                                               
                                                                               
Query Block Name / Object Alias (identified by operation id):                  
-------------------------------------------------------------                  
                                                                               
   1 - SEL$1 /                                                       
   2 - SEL$1 /                                                       
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
Outline Data                                                                   
-------------                                                                  
                                                                               
  /*+                                                                          
      BEGIN_OUTLINE_DATA                                                       
      IGNORE_OPTIM_EMBEDDED_HINTS                                              
      OPTIMIZER_FEATURES_ENABLE('11.2.0.1')                                    
      DB_VERSION('11.2.0.1')                                                   
      ALL_ROWS                                                                 
      OUTLINE_LEAF(@"SEL$1")                                                   
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
      INDEX_RS_ASC(@"SEL$1" " ("DEPT"."DEPTNO"))                  
      END_OUTLINE_DATA                                                         
  */                                                                           
                                                                               
Peeked Binds (identified by position):                                         
--------------------------------------                                         
                                                                               
   1 - :A (NUMBER): 20           --小結:重新解析sql會運用繫結變數最新的值                                              
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
PLAN_TABLE_OUTPUT                                                              
--------------------------------------------------------------------------------
                                                                               
   2 - access("DEPTNO"=:A)                                                     
                                                                               
Column Projection Information (identified by operation id):                    
-----------------------------------------------------------                    
                                                                               
   1 - "DEPTNO"[NUMBER,22], "DEPT"."DNAME"[VARCHAR2,14],                       
       "DEPT"."LOC"[VARCHAR2,13]                                               
   2 - "DEPT".ROWID[ROWID,10], "DEPTNO"[NUMBER,22]                             
                                                                               
已選擇51行。
SQL> spool off

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-749803/,如需轉載,請註明出處,否則將追究法律責任。

相關文章