索引的优点
索引是与表相关的一个可选结构
一个表中可以存在索引,也可以不存在索引,不做硬性要求。
用以提高 SQL 语句执行的性能
快速定位我们需要查找的表的内容(物理位置),提高sql语句的执行性能。
减少磁盘I/O
取数据从磁盘上取到数据缓冲区中,再交给用户。
磁盘IO非常不利于表的查找速度(效率的提高)。
使用 CREATE INDEX 语句创建索引
在逻辑上和物理上都独立于表的数据
索引与表完全独立,表里的内容是我们真正感兴趣的内容,而索引则是做了一些编制 ,索引和数据可以存放在不同的表空间下面,可以存放在不同的磁盘下面。
Oracle 自动维护索引
当对一个建立索引的表的数据进行增删改的操作时,oracle会自动维护索引,使得其仍然能够更好的工作。
生成类似于 auto_increment
这种ID自动增长 1,2,2,3, 4,5…
auto_increment
这个是mysql
create sequence
序列的名称create sequence
序列的名称
start with
从几开始
increment by
每次增长多少
maxvalue 最大值
| nomaxvalue
minvalue 最小值
| nominvalue
cycle
| nocycle
是否循环 1,2,3,1,2,3
cache 缓存的数量3
| nocache 1,2,3,4,5,6
create sequence seq_test1
start with 1 --从1开始
increment by 2 --每次增加两个
maxvalue 30 --最大值为30
cycle --设置循环
cache 10; --缓存数量为10
currval : 当前值
nextval : 下一个值
注意:currval 需要在调用nextval之后才能使用
select sql_test1.currval from dual;
默认从1开始,默认每次增加1,默认不循环,默认没有上限
create sequence seq_test2;
select seq_test2.nextval from dual;
create sequence seq_test3
start with 1 --从1开始
increment by 2 --每次增加2
maxvalue 30 --最大值为30
minvalue 0 --最小值为0
cycle --设置循环
cache 10; --缓存数量为10
1、cost CPU 调用次数
2、Cardinality 影响行数
索引是用于加速数据存取的数据对象。合理的使用索引可以大大降低i/o
次数,而提高数据访问性能。
create index 索引的名称 on 表名(列)
create table wubaiwanshuju(
name varchar2(30),
address varchar2(20)
);
500000
条数据-- 插入500000条数据
declare
begin
for i in 1..5000000 loop
insert into wubaiwanshuju values('姓名'||i,'地址'|| i);
end loop;
commit;
end;
name='姓名3000000'
速度非常慢
select * from wubaiwanshuju where name='姓名300000';
2.063秒
name='姓名3000000'
创建索引:对wubaiwanshuju
表当中的name列创建索引
create index ind_wubaiwanshuju on wubaiwanshuju(name);
查询:
select * from wubaiwanshuju where name='姓名3000000';
name='姓名3000000' and '地址3000000'
select * from wubaiwanshuju where name='姓名3000000' add address = '地址3000000';
在没有创建复合索引的情况下,name 和 address
创建了复合索引name address
-- 创建复合索引的情况下,再去查询(在索引的基础上去建立索引)
create index ind_wubaiwan2 on wubaiwanshuju(name,address);
创建复合索引的方式查询数据
select * from wubaiwanshuju where name='姓名3000000' and address='地址3000000';
创建复合索引name
和address
btree
balance
Tree
平衡二叉树在创建索引的时候:如果某列作为条件的时候,可以提高查询效率,但是修改时候,会变慢
索引创建好之后,过了一段时间,DBA
都会去做一个事情重构索引