使用表中特定行的索引的原因是什么?

时间:2014-09-04 12:19:08

标签: oracle indexing

在文档概念Oracle 11G中使用基于函数的索引example

  

基于函数的索引对于仅索引特定行也很有用   在一张桌子里。例如,sh.customers中的cust_valid列   table有I或A作为值。要仅为A行编制索引,即可   可以编写一个函数,为其他行返回null值   而不是A行。

我只能想象这个用例:通过条件消除一些行来减小索引的大小。当这种可能性有用时,还有其他用例吗?

1 个答案:

答案 0 :(得分:1)

让我们来看看基于函数的索引:

SQL> create table tab1 as select object_name from all_objects;

Table created.

SQL> exec dbms_stats.gather_table_stats(user, 'TAB1');

PL/SQL procedure successfully completed.

SQL> set autotrace traceonly
SQL> select count(*) from tab1 where lower(object_name) = 'all_tables';


Execution Plan
----------------------------------------------------------
Plan hash value: 1117438016

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    19 |    18   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |    19 |            |          |
|*  2 |   TABLE ACCESS FULL| TAB1 |   181 |  3439 |    18   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   2 - filter(LOWER("OBJECT_NAME")='all_tables')


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         63  consistent gets
          ...

如您所知,所有对象都有唯一的名称,但oracle必须分析所有181行并执行63次一致的获取(物理或逻辑块读取)

让我们创建一个基于函数的索引:

SQL> create index tab1_obj_name_idx on tab1(lower(object_name));

Index created.

SQL> select count(*) from tab1 where lower(object_name) = 'all_tables';

Execution Plan
----------------------------------------------------------
Plan hash value: 707634933

---------------------------------------------------------------------------------------
| Id  | Operation         | Name              | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |                   |     1 |    17 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE   |                   |     1 |    17 |            |          |
|*  2 |   INDEX RANGE SCAN| TAB1_OBJ_NAME_IDX |   181 |  3077 |     1   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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

   2 - access(LOWER("OBJECT_NAME")='all_tables')


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          2  consistent gets
          ...

正如您所看到的那样,成本大幅降低(从18降至1),并且只有2个一致的收益。

因此,基于函数的索引可以很好地提高应用程序的性能。

相关问题