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

通常情况下is null或者!=这些条件如果不是具有很强的过滤性,可以先关注其它的过滤条件。但有些SQL这两种条件具有很强的过滤性,就可以考虑用以下方法。下面先讨论is null的优化,再讨论!=的优化,最后讨论is null or !=一起使用的优化。

以下测试:
Oracle version:11.2.0.4

#新建测试表
create table scott.tb_sj01 as select * from dba_objects;

#处理测试表中的数据
update scott.tb_sj01 set object_name=null where object_id<=10;

update scott.tb_sj01 set object_name='SJ' where object_id>20;

commit;

#收集表的统计信息
begin
  dbms_stats.gather_table_stats('scott','TB_SJ01');
end;

#查看测试表中object_name字段的数据分布
#可以看出object_name字段IS NULL的有9笔记录;object_name !='SJ'有11笔记录。
select nvl(object_name,'NULL') object_name,count(1) cnt
from scott.tb_sj01
group by nvl(object_name,'NULL')
order by 2 ;
/*
OBJECT_NAME            CNT
ICOL$                  1
TS$                    1
OBJ$                  1
FILE$                  1
STDBY_LINK_CT6601SB    1
UNDO$                  1
FET$                  1
I_USER#                1
UET$                  1
SEG$                  1
IND$                  1
NULL                  9
SJ                    86880
*/

1.语句:select * from scott.tb_sj01 where object_name is null的优化
SQL> set autot trace
#优化前,COST=346,consistent gets=1246
SQL> select * from scott.tb_sj01 where object_name is null;

9 rows selected.

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

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

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

1 - filter("OBJECT_NAME" IS NULL)

Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      1246  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
         
#优化方法1:
#增加索引,让object_name IS NULL的也保存在索引中
SQL> create index scott.idx_tb_sj01_01 on scott.tb_sj01(object_name,1);

#优化后,COST=3,consistent gets=5
SQL> select * from scott.tb_sj01 where object_name is null;

9 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1042936765

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

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

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

|  0 | SELECT STATEMENT            |                |    9 |  684 |    3  (0)| 00:00:01 |

|  1 |  TABLE ACCESS BY INDEX ROWID| TB_SJ01        |    9 |  684 |    3  (0)| 00:00:01 |

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

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

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

2 - access("OBJECT_NAME" IS NULL)

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

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