深入理解Oracle索引(21):视图和小表是否应该加索引

      ㈠ 视图能使用索引吗
     
     
        答案是不能、为什么呢?
        我们先要知道什么是视图?
        视图、本质上讲、就只是一条被存储的SQL语句而已
        那有人就要问了、为什么物化视图可以哩?
        这个.....这个、这根本就是两码事嘛、两个完全不同的概念
        物化视图、本质上而言、是一张表、有数据

        所以、只要基表索引使用得当、便是对视图的最大安慰和馈赠

 

hr@ORCL> create view v_employees as select * from employees;



View created.



hr@ORCL> create index idx_v_employeess on v_employees(EMPLOYEE_ID);

create index idx_v_employeess on v_employees(EMPLOYEE_ID)

                                 *

ERROR at line 1:

ORA-01702: a view is not appropriate here


 

     ㈡ 小表使用索引有意义吗
        
        一般我们会认为、小表全表扫描可能性能更好、但是、请看下面一个简单测试

 

hr@ORCL> select * from t where y='9999a';





Execution Plan

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

Plan hash value: 1601196873



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

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

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

|   0 | SELECT STATEMENT  |      |     3 |    90 |    57   (4)| 00:00:01 |

|*  1 |  TABLE ACCESS FULL| T    |     3 |    90 |    57   (4)| 00:00:01 |

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



Predicate Information (identified by operation id):

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



   1 - filter("Y"='9999a')



Note

-----

   - dynamic sampling used for this statement





Statistics

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

          0  recursive calls

          0  db block gets

        249  consistent gets

          0  physical reads

          0  redo size

        462  bytes sent via SQL*Net to client

        385  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          1  rows processed



/* y上建立索引 */



hr@ORCL> create index idx_t_y on t (y);



Index created.



hr@ORCL> exec dbms_stats.gather_table_stats(ownname=>'HR',tabname=>'T',estimate_percent=>100,cascade=>TRUE,no_invalidate=>false);



PL/SQL procedure successfully completed.



hr@ORCL> select * from t where y='9999a';





Execution Plan

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

Plan hash value: 2903481642



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

| Id  | Operation                   | Name    | Rows  | Bytes | Cost (%CPU)| Time     |

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

|   0 | SELECT STATEMENT            |         |     1 |    11 |     2   (0)| 00:00:01 |

|   1 |  TABLE ACCESS BY INDEX ROWID| T       |     1 |    11 |     2   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IDX_T_Y |     1 |       |     1   (0)| 00:00:01 |

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



Predicate Information (identified by operation id):

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



   2 - access("Y"='9999a')





Statistics

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

          0  recursive calls

          0  db block gets

          4  consistent gets

          0  physical reads

          0  redo size

        466  bytes sent via SQL*Net to client

        385  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          0  sorts (memory)

          0  sorts (disk)

          1  rows processed





/* 逻辑读从 249一下子卡到4、虽然这个表有10w行、但也绝对不是大表*/


 

        小表 DML 不会很多、因此、不会有维护索引带来的额外开销、这是第二个理由
        
        综上、不使用索引访问小表、效率未必是最高、这和表记录大小、记录的分布有很大关系

 

 

By David Lin 
2013-06-07
Good Luck

 

你可能感兴趣的:(oracle)