2014-09-04 53 views
1

有一個在文檔概念的Oracle 11G使用基於函數的索引的example什麼原因使用索引表中的特定行?

基於函數的指數也用於索引有用僅特定行 在表中。例如,在sh.customers 表cust_valid列具有I或A作爲值。索引只有A行,你 可以寫返回其他 比A行的任何行的空值的函數。

我可以想象只有這個用例:通過消除一些條件的行來減小索引的大小。當這種可能性有用時,還有其他用例嗎?

+1

例如一個完整的列,它是Y代表該行的99.999%,而您只需要查詢那些完成!= Y.然後在NULLIF(完成後,「Y」)這樣小的指數將使查詢在哪裏NULLIF (完成後, 'Y')IS NOT NULL或WHERE NULLIF(完成後, 'Y')= 'N' 能夠做索引的快速全掃描速度非常快。 – 2014-09-04 12:38:10

回答

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 consi支架得到。

因此,基於函數的索引可以提高應用程序的性能非常出色。

相關問題