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
全部博文(169)
分类: 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. |