ITPub博客

首页 > Linux操作系统 > Linux操作系统 > null作为条件使用索引的情况

null作为条件使用索引的情况

原创 Linux操作系统 作者:q418441117 时间:2013-02-01 13:51:32 0 删除 编辑

通常情况下,null作为条件是不会使用到索引的,但创建符合索引后,可以使用到索引。
实验如下:
SQL> create table test1 as select * from dba_objects;

Table created.

SQL> create index test1_idx on test1(object_id);      --创建一般索引

Index created.

SQL> exec dbms_stats.gather_table_stats('sys','test1',cascade=>true);

PL/SQL procedure successfully completed.

SQL> set autotrace trace;

SQL> select * from test1 where object_id is null;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 4122059633

---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |     1 |    97 |   282   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| TEST1 |     1 |    97 |   282   (1)| 00:00:04 |
---------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID" IS NULL)


Statistics
----------------------------------------------------------
         51  recursive calls
          0  db block gets
       1093  consistent gets
          0  physical reads
          0  redo size
       1343  bytes sent via SQL*Net to client
        512  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          0  rows processed

SQL> select * from test1 where object_id is null;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 4122059633

---------------------------------------------------------------------------
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |       |     1 |    97 |   282   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| TEST1 |     1 |    97 |   282   (1)| 00:00:04 |
---------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID" IS NULL)


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

SQL> create index test1_idx2 on test1(object_id,1) compute statistics;   --创建有常量1的符合索引

Index created.

SQL> select * from test1 where object_id is null;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 3452673582

------------------------------------------------------------------------------------------
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |            |     1 |    97 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST1      |     1 |    97 |     1   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | TEST1_IDX2 |     1 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

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

   2 - access("OBJECT_ID" IS NULL)


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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26655292/viewspace-753742/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2012-09-11

  • 博文量
    203
  • 访问量
    471912