About me:Oracle ACE pro,optimistic,passionate and harmonious. Focus on ORACLE,MySQL and other database programming,peformance tuning,db design, j2ee,Linux/AIX,Architecture tech,etc
全部博文(172)
分类: Oracle
2024-10-18 10:09:58
dingjun123@ORADB> DROP TABLE t; Table dropped. dingjun123@ORADB> CREATE TABLE t AS SELECT * FROM dba_objects; Table created. dingjun123@ORADB> CREATE INDEX idx_t ON t(object_id); Index created. dingjun123@ORADB> EXEC dbms_stats.gather_table_stats(NULL,'T',cascade => TRUE); PL/SQL procedure successfully completed. dingjun123@ORADB> select count(*) from t; COUNT(*) ---------- 75236 |
dingjun123@ORADB> set autotrace traceonly dingjun123@ORADB> var oid number; dingjun123@ORADB> exec :oid := 20; PL/SQL procedure successfully completed. dingjun123@ORADB> select * from t WHERE :OID IS NULL OR object_id=:OID ; 1 row selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3763 | 356K| 300 (1)| 00:00:04 | |* 1 | TABLE ACCESS FULL| T | 3763 | 356K| 300 (1)| 00:00:04 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(:OID IS NULL OR "OBJECT_ID"=TO_NUMBER(:OID)) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 1075 consistent gets 0 physical reads 0 redo size 1389 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
dingjun123@ORADB> select * from t WHERE 20 IS NULL OR object_id=20 ; 1 row selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1594971208 ------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 97 | 2 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 97 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IDX_T | 1 | | 1 (0)| 00:00:01 | ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID"=20) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 1 physical reads 0 redo size 1392 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
******************************************* Peeked values of the binds in SQL statement ******************************************* ----- Bind Info (kkscoacd) ----- Bind#0 oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00 oacflg=03 fl2=1000000 frm=00 csi=00 siz=24 off=0 kxsbbbfp=0ed92eac bln=22 avl=02 flg=05 value=20 Bind#1 No oacdef for this bind. ----------------------------- SYSTEM STATISTICS INFORMATION ----------------------------- Using NOWORKLOAD Stats CPUSPEEDNW: 1332 millions instructions/sec (default is 100) IOTFRSPEED: 4096 bytes per millisecond (default is 4096) IOSEEKTIM: 10 milliseconds (default is 10) MBRC: -1 blocks (default is 8) *************************************** BASE STATISTICAL INFORMATION *********************** Table Stats:: Table: T Alias: T #Rows: 75236 #Blks: 1095 AvgRowLen: 97.00 Index Stats:: Index: IDX_T Col#: 4 LVLS: 1 #LB: 167 #DK: 75235 LB/K: 1.00 DB/K: 1.00 CLUF: 1307.00 Access path analysis for T *************************************** SINGLE TABLE ACCESS PATH Single Table Cardinality Estimation for T[T] Table: T Alias: T Card: Original: 75236.000000 Rounded: 3763 Computed: 3762.75 Non Adjusted: 3762.75 Access Path: TableScan Cost: 299.85 Resp: 299.85 Degree: 0 Cost_io: 298.00 Cost_cpu: 29503827 Resp_io: 298.00 Resp_cpu: 29503827 ****** trying bitmap/domain indexes ****** ****** finished trying bitmap/domain indexes ****** Best:: AccessPath: TableScan Cost: 299.85 Degree: 1 Resp: 299.85 Card: 3762.75 Bytes: 0 |
dingjun123@ORADB> SELECT VALUE,DESCRIPTION FROM all_parameters WHERE NAME='_or_expand_nvl_predicate'; VALUE DESCRIPTION -------------------- ------------------------------------------------------------ TRUE enable OR expanded plan for NVL/DECODE predicate 1 row selected. |
dingjun123@ORADB> select * from t WHERE object_id = nvl(:OID,object_id) ; 1 row selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1189289681 --------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 75236 | 7126K| 303 (1)| 00:00:04 | | 1 | CONCATENATION | | | | | | |* 2 | FILTER | | | | | | |* 3 | TABLE ACCESS FULL | T | 75235 | 7126K| 301 (1)| 00:00:04 | |* 4 | FILTER | | | | | | | 5 | TABLE ACCESS BY INDEX ROWID| T | 1 | 97 | 2 (0)| 00:00:01 | |* 6 | INDEX RANGE SCAN | IDX_T | 1 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter(:OID IS NULL) 3 - filter("OBJECT_ID" IS NOT NULL) 4 - filter(:OID IS NOT NULL) 6 - access("OBJECT_ID"=:OID) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 2 physical reads 0 redo size 1389 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
dingjun123@ORADB> ALTER SESSION SET "_or_expand_nvl_predicate"=FALSE; Session altered. dingjun123@ORADB> select * from t WHERE object_id = decode(:OID,null,object_id,:OID) ; 1 row selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 97 | 301 (1)| 00:00:04 | |* 1 | TABLE ACCESS FULL| T | 1 | 97 | 301 (1)| 00:00:04 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID"=DECODE(:OID,NULL,"OBJECT_ID",:OID)) |
dingjun123@ORADB> select * from t WHERE :OID IS NULL 2 UNION ALL 3 SELECT * FROM t WHERE :OID IS NOT NULL AND object_id =:OID ; 1 row selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1690602373 --------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 75237 | 7126K| 303 (2)| 00:00:04 | | 1 | UNION-ALL | | | | | | |* 2 | FILTER | | | | | | | 3 | TABLE ACCESS FULL | T | 75236 | 7126K| 301 (1)| 00:00:04 | |* 4 | FILTER | | | | | | | 5 | TABLE ACCESS BY INDEX ROWID| T | 1 | 97 | 2 (0)| 00:00:01 | |* 6 | INDEX RANGE SCAN | IDX_T | 1 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter(:OID IS NULL) 4 - filter(:OID IS NOT NULL) 6 - access("OBJECT_ID"=TO_NUMBER(:OID)) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 4 consistent gets 0 physical reads 0 redo size 1389 bytes sent via SQL*Net to client 415 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 1 rows processed |
dingjun123@ORADB> exec :oid := null; PL/SQL procedure successfully completed. --原始数据75236条 dingjun123@ORADB> SELECT COUNT(*) from t ; COUNT(*) ---------- 75236 1 row selected. --写法1考虑NULL情况 dingjun123@ORADB> select count(*) from t WHERE :OID IS NULL OR object_id=:OID ; COUNT(*) ---------- 75236 1 row selected. --NVL/DECODE写法未考虑NULL情况,少1条数据 dingjun123@ORADB> SELECT COUNT(*) from t WHERE object_id = decode(:OID,null,object_id,:OID) ; COUNT(*) ---------- 75235 1 row selected. dingjun123@ORADB> SELECT COUNT(*) FROM t WHERE object_id = nvl(:OID, object_id); COUNT(*) ---------- 75235 1 row selected. --写法3 UNION ALL写法考虑了NULL情况 dingjun123@ORADB> SELECT COUNT(*) 2 FROM ( 3 SELECT * FROM t WHERE :OID IS NULL 4 UNION ALL 5 SELECT * FROM t WHERE :OID IS NOT NULL AND object_id =:OID 6 ) ; COUNT(*) ---------- 75236 1 row selected. |