ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 如何让table表的null列由不走索引变为可走索引

如何让table表的null列由不走索引变为可走索引

原创 Linux操作系统 作者:wisdomone1 时间:2013-02-26 22:33:58 0 删除 编辑

SQL> create table t_null(a int not null,b int);

Table created.

SQL> insert into t_null select level,level+2 from dual connect by level<=10;

10 rows created.

SQL> commit;

Commit complete.

SQL> create index idx_1 on t_null(a);

Index created.

SQL> create index idx_2 on t_null(b);

Index created.

SQL> explain plan for select a from t_null;

Explained.

--非空a列采用了索引全扫描
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

Plan hash value: 3375905911

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |    10 |   130 |     1   (0)| 00:00:01 |
|   1 |  INDEX FULL SCAN | IDX_1 |    10 |   130 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------

Note
-----

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

   - dynamic sampling used for this statement (level=2)

12 rows selected.

SQL> explain plan for select b from t_null;

Explained.
--空b列采用了全表扫描
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

Plan hash value: 2443327197

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |    10 |   130 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| T_NULL |    10 |   130 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

Note
-----

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

   - dynamic sampling used for this statement (level=2)

12 rows selected.

--对空b列添加where 条件b is not null,可走索引全扫描
SQL> explain plan for select a from t_null where a is not null;

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

Plan hash value: 3375905911

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |    10 |   130 |     1   (0)| 00:00:01 |
|   1 |  INDEX FULL SCAN | IDX_1 |    10 |   130 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------

Note
-----

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

   - dynamic sampling used for this statement (level=2)

12 rows selected.

--如对空b列进行聚集运算,如下count会自动filter out null value,走了索引全扫描
SQL> explain plan for select count(b) from t_null;

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

Plan hash value: 1382308919

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |     1 |    13 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE  |       |     1 |    13 |            |          |
|   2 |   INDEX FULL SCAN| IDX_2 |    10 |   130 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------

Note

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------

-----
   - dynamic sampling used for this statement (level=2)

13 rows selected.

--删除b列索引
SQL> drop index idx_2;

Index dropped.
--构建b列如下的索引,即如b列为空,则转化为0
SQL> create index idx_2 on t_null(b,0);

Index created.

SQL> explain plan for select b from t_null;

Explained.

--修正后b列可走索引全扫描
SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------

Plan hash value: 3964661047

--------------------------------------------------------------------------
| Id  | Operation        | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT |       |    10 |   130 |     1   (0)| 00:00:01 |
|   1 |  INDEX FULL SCAN | IDX_2 |    10 |   130 |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------

Note
-----

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------

   - dynamic sampling used for this statement (level=2)

12 rows selected.

 

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

请登录后发表评论 登录
全部评论
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥

注册时间:2008-04-04

  • 博文量
    2189
  • 访问量
    12685995