ITeye博客

oracle 索引

hougbin · 2010-12-30 09:34 · oracle · 数据库 · HTML · Blog · SQL · Oracle · Markdown

索引分类:

  • 唯一索引,作用是数据约束,保证数据唯一,还有就是数据索引,提高查询效率
  • 一般索引,只有数据索引的作用。

唯一索引:

Sql代码 复制代码

  1. create unique index idx_employee_empname on employee(empname);
create unique index idx_employee_empname on employee(empname);

一般索引:

Sql代码 复制代码

  1. create index idx_employee_address on employee(address);
  2. create unique index idx_employee_field1_field2 on employee(field1,field2);
create index idx_employee_address on employee(address);
create unique index idx_employee_field1_field2 on employee(field1,field2);

函数索引:

Sql代码 复制代码

  1. create index idx_product_nvl_price on product(nvl(price,0.0));
create index idx_product_nvl_price on product(nvl(price,0.0));

删除索引:

Sql代码 复制代码

  1. drop index idx_employee_empname;
drop index idx_employee_empname;

指定索引表空间

Sql代码 复制代码

  1. create index idx_employee_address on employee(address) tablespace indexs;
create index idx_employee_address on employee(address) tablespace indexs;

查看索引

Sql代码 复制代码

  1. select * from user_indexes where table_name = 'SHOP_PD'
select * from user_indexes where table_name = 'SHOP_PD'

参考:
http://www.java2s.com/Tutorial/Oracle/0180__Index/Createindexwithtablespace.htm
http://hi.baidu.com/luodaijun/blog/item/2ea274f065c856aea50f5219.html

以下情况下索引会失效

  • 查询条件中字段使用顺序和组合索引定义中字段的顺序不一致时,索引无效oracle 10这种情况下索引已经起作用,低版本的不起作用
  • 使用!=,<>操作符会限制索引,换成 > or < 不会oracle 10这种情况下索引已经起作用,低版本的不起作用
  • 对索引列使用函数会忽略索引必要的情况下,可以建立函数索引
  • 试着对被SUM,MAX,MIN的列增加索引

有时候索引不起作用是因为oralce在使用基于成本的优化器(CBO),走索引的代价反而更加高,使得sql走全表扫描,可以如下使用强制索引语法,然后查看执行计划中的消耗

Sql代码 复制代码

  1. select /*+ index(t,IDX_AR_JDXX_SELL)*/t.province, t.city, t.country
  2. from ar_sell t
  3. group by t.type_code,t.province, t.city, t.country

评论(1)

  1. #1 楼 · doc_886 · 2011-05-25 15:42

    :o .

评论功能维护中,当前仅展示历史评论。