Oracle SQL过滤条件是IS NULL or !=的优化(2)

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          5  consistent gets
          0  physical reads
          0  redo size
      1850  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed

#优化方法2:
#增加函数索引,只让object_name IS NULL的保存在索引中
create index scott.idx_tb_sj01_02 on scott.tb_sj01(decode(object_name,null,1));

#原语句改写为:
select * from scott.tb_sj01 where decode(object_name,null,1)=1;

#优化后,COST=2,consistent gets=4
SQL> select * from scott.tb_sj01 where decode(object_name,null,1)=1;

9 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 612345449

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

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

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

|  0 | SELECT STATEMENT            |                |  869 | 66044 |    2  (0)| 00:00:01 |

|  1 |  TABLE ACCESS BY INDEX ROWID| TB_SJ01        |  869 | 66044 |    2  (0)| 00:00:01 |

|*  2 |  INDEX RANGE SCAN          | IDX_TB_SJ01_02 |    9 |      |    1  (0)| 00:00:01 |

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

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access(DECODE("OBJECT_NAME",NULL,1)=1)

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
          4  consistent gets
          0  physical reads
          0  redo size
      1850  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed

2.语句:select * from scott.tb_sj01 where object_name!='SJ'的优化

#优化前,COST=346,consistent gets=1246
SQL> select * from scott.tb_sj01 where object_name!='SJ';

11 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 283620643

-----------------------------------------------------------------------------
| Id  | Operation        | Name    | Rows  | Bytes | Cost (%CPU)| Time    |
-----------------------------------------------------------------------------
|  0 | SELECT STATEMENT  |        | 79650 |  5911K|  346  (1)| 00:00:05 |
|*  1 |  TABLE ACCESS FULL| TB_SJ01 | 79650 |  5911K|  346  (1)| 00:00:05 |
-----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

1 - filter("OBJECT_NAME"<>'SJ')

Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      1246  consistent gets
          0  physical reads
          0  redo size
      1979  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
        11  rows processed

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/58f6bf20c3bc4789c61b32b3d0971452.html