/*
索引:相当于一本书的目录,能够提高查询速度
索引有两种:
1.自动创建--在创建主键primary key或唯一性约束unique的时候
数据库会自动的在对应的列上,创建唯一性索引
2.手动创建--在不唯一的列上创建非唯一性的索引,加速查询效率
*/
--自动创建(给scott用户授权dba,并切换到scott用户)
grant dba to scott;
select * from emp;
--EMP表已有一个索引,是它的主键。
--给EMP表增加一个唯一性约束
ALTER TABLE EMP ADD CONSTRAINTS ENAME_UNQ UNIQUE(ENAME);
--查看index
select index_name,index_type,table_name from user_indexes ;
--删除索引
ALTER TABLE EMP DROP CONSTRAINTS ENAME_UNQ;
/*
手动创建索引
create index 索引名称 on 表名(列)
*/
create table bigdatatable(
ename varchar2(20),
address varchar2(30)
);
--插入五百万条数据
declare
begin
for i in 1..5000000 loop
insert into bigdatatable values('姓名'||i , '地址'||i);
end loop;
commit;
end;
select count(ename) from bigdatatable;
select * from bigdatatable;
--在没有索引的情况下,查询ename = '姓名3000000'的时间
select * from bigdatatable where ename = '姓名3000000';
--F5查看并截图,cost:cpu调用次数;cardinality:影响行数
--创建索引ename,再查询ename = '姓名3000000'的时间 -0.03
create index index_name on bigdatatable(ename);
select * from bigdatatable where ename = '姓名3000000';
--F5查看并截图,cost:cpu调用次数;cardinality:影响行数
--创建复合索引,在indexes中查看索引
create index index_bigdatatable on bigdatatable(ename, address);
select * from bigdatatable where ename = '姓名3000000' and address = '地址3000000';
--F5查看并截图,cost:cpu调用次数;cardinality:影响行数
--删除索引
drop index index_bigdatatable;
--删除表
drop table bigdatatable;
Oracle之索引
最新推荐文章于 2024-06-18 18:01:38 发布