Oracle索引index

来源:互联网 发布:java中什么叫多态 编辑:程序博客网 时间:2024/06/07 06:55
 

--索引

/*有一条基本的创建索引原则是:当任何单个查询要检索的行少于或等于整个表行数的10%.*/

--创建索引

        /*create [ unique] index index_name on

        table_name (column_name[,column_name ...])

        tablespace tab_space;

        其中unique指定索引列中的值必须是唯一的

colunm_name指定要对哪个列创建索引。如果对多列创建索引,这种索引称为复合索引

tab_space指定存储该索引的表空间.如果没有指定,那么索引被存储到用户默认的表空间中。

         

        由于性能方面的原因,通常将表与索引存储在不同的表空间中。

 */

 

--在创建索引时应该遵循一些原则,假设customers表包含很多行,并且通常使用一个包含where子句的select语句对customers表进行检索,其中where子句对last_name列

--进行过滤;例如:

select *

from t_customers

where last_name='Brown';

--假设last_name列的值都是唯一的,以使任何where子句中使用last_name列的查询所返回的行数都小于表总行数的10%。因此,last_name列非常适合创建索引

create index idx_customers_last_name on t_customers(last_name);

--创建基于函数的索引

--假设执行下列这个查询

select *

from t_customers

where last_name=upper('PRICE');

--由于查询使用了一个函数UPPER(),因此就不会使用索引idx_customers_last_name,如果想让索引可以基于函数的结果使用,必须创建基于函数的索引.

create index idx_func_customers_last_name on t_customers(UPPER(last_name));

--另外,为了利用基于函数的索引,DBA必须把初始化参数QUERY_REWRITE_ENABLED设置为true(默认值为false)

conn system/tiger

alter system set QUERY_REWRITE_ENABLED=TRUE;