Indexing NULL table column values for fast SQL performance

Indexing NULL table column values for fast SQL performance

Oracle Tips by Burleson Consulting
September 21,  2003 - Revised January 1, 2007

One problem with allrelational databases is having the optional ability to index on a NULLcolumn.  By default, relational databases ignore NULL values (because therelational model says that NULL means "not present").  Hence,Oracle indexes will not include NULL values. 

For example, thisindex definition would not index on "open positions", new employeepositions that are stored with a NULL employee name:

create index
    emp_ename_idx
on
   emp 
   (ename)
;

Whenever a SQL queryasks for the open position employee slots "where ename is NULL",there will be no index entries for NULLS in emp_name_idx and Oraclewould perform an unnecessary large-table full-table scan.

Execution Plan
---------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1 Card=1 Bytes=6) 
1 0 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=1 Bytes=6)

To get around theoptimization of SQL queries that choose NULL column values, we can create afunction-based index using the null value built-in SQL function to index onlyon the NULL columns. 

Note that the"null value" (NVL) function replaces NULL values with the characterstring "null', a real value that can participate in an index:

 

-- create an FBI on ename column with NULL values
create index
    emp_null_ename_idx
on
   emp 
   (nvl(ename,'null'));

 

analyze index emp_null_ename_idx compute statistics;
 

You canalso do this techniques with NULL numeric values.  This syntax replacesNULL values with a zero:
 

-- create an FBI on emp_nbr column with NULL values
create index
    emp_null_emp_nbr_idx
on
   emp 
   (nvl(ename,o));

 

analyze index emp_null_ename_idx compute statistics;

Now we can use theindex and greatly improve the speed of any queries that require access to theNULL columns.  Note that we must make one of two changes:

    1-Add a hint to force the index

   2 -Change the WHERE predicate to match the function

Here is an example ofusing an index on NULL column values:

-- insert a NULL row
insert into emp (empno) values (999);

set autotrace traceonly explain;

 

-- test the index access (change predicate to use FBI)
select /*+ index(emp_null_ename_idx) */
   ename
from
   emp e
where
   nvl(ename,'null') = 'null'
;


Reader Comments:
 

I wasreading "Indexing NULL table column values for fast SQL performance"article, where you have mentioned that if the column is having null values (ofvery less amount) and you want to select where column is null then to use theindex, create function based index and changed your query to use that index.

 

I think,it can be done without changing query as well......

 

SQL> select count(1) from t where n is null;

 

 COUNT(1)

----------

      334

 

Execution Plan

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

  0      SELECT STATEMENTOptimizer=CHOOSE (Cost=3 Card=1 Bytes=3)

  1    0   SORT (AGGREGATE)

  2    1     TABLE ACCESS (FULL)OF 'T' (Cost=3 Card=334 Bytes=1002)

 

SQL> create index tind on t(n, 1); ----> here 1 isjust any arbitary value.

 

Index created.

 

SQL> execdbms_stats.gather_table_stats(user,'t',cascade=>true);

 

PL/SQL procedure successfully completed.

 

SQL> select count(1) from t where n is null;

 

 COUNT(1)

----------

      334

 

Execution Plan

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

  0      SELECT STATEMENTOptimizer=CHOOSE (Cost=2 Card=1 Bytes=4)

  1    0   SORT (AGGREGATE)

  2    1     INDEX (RANGE SCAN)OF 'TIND' (NON-UNIQUE) (Cost=2 Card=3

         34 Bytes=1336)

 

 

SandeepRedkar

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值