提示:以下是本篇文章正文内容,下面案例可供参考

一、索引概念

是一个单独的、物理的数据库对象
用于存放表中每一条记录的位置的对象。
在创建索引时,先要对索引字段进行排序。
索引由Oracle自动维护
优点:提高查询速度
缺点:创建和维护索引需要时间;索引需要物理空间,随着数据量的增大而增大;

二、索引分类

1.按照索引数据的存储方式可以将索引分为B树索引、位图索引、函数索引、簇索引、反序索引等。
2.按照索引列的唯一性又可以分为唯一索引和非唯一索引
3.按照索引列的个数索引可以分为单列索引和复合索引

三、创建索引

1.自动创建:通过约束,系统创建
2.手工创建
create index index_name on table_name(column_name)

1.创建B树索引

create index dname_dept on dept(dname);
默认情况下创建的索引不是唯一索引

2.创建基于函数的索引

create index ename_lower_index on emp(lower(ename));
select * from emp where lower(ename)=‘smith’;

3.创建复合索引

create index emp_idx on emp (job,ename);
适合于查询where job=‘’ and ename=‘’;

四、查看索引

1.使用数据字典user_indexes,查看当前用户下所建立的索引。

select index_name,index_type,table_name,tablespace_name from user_indexes

2.查看相应的列

select * from user_ind_columns;

五、修改索引

重命名索引

alter index index_name rename to new index_name;
alter index dept_deptno_pk rename to pk_deptno_dept;

六、删除索引

删除索引与索引创建时采用的方式有关
1.手动创建的索引,通过命令删除

drop index index_name;

2.自动创建的索引
通过禁用约束或删除约束的方式来删除对应的索引。
删除表,则会完全删除所有索引

七、练习

select * from emp where sal<2000;–全表扫描
create index emp_index1 on emp(ename);–创建B树索引
create index emp_index2 on emp(lower(ename));–函数索引
select * from emp where lower(ename)=‘smith’;
create index emp_index3 on emp(job,ename);–两个字段排序不同,索引不同
select * from emp where job=‘’ and ename=‘’;
select * from emp where ename=‘’ and job=‘’;
desc user_indexes;
select index_name,index_type,table_name from user_indexes;
alter index emp_index1 rename to emp_idx1;
drop index emp_index3;

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐