索引
准则:当任何单个查询要检索的行少于或等于整个表行数的10%时,就应当创建索引。
B-树索引:索引的候选列应该是用来存储很大范围的值的列
位图索引:包含小范围值的列
1.创建B-树索引
注意:由于性能原因,应该将索引与表存储到不同的表空间中。
假设test表包含很多行
select id,type
from test
where type='01';
create index i_test_type on test(type);
唯一索引
create unique index i_test_id on test(id);
复合索引
create index i_test_id_type on test(id, type);
2.创建基于函数的索引
select id,type
from test
where type=UPPER('SFZ');
create index i_func_test_type on test(UPPER(type));
注意:使用基于函数的索引,必须要将初始化参数query_rewrite_enabled设置为true。
3.获取有关信息的索引
select index_name, table_name, uniqueness, status
from user_indexes
where table_name='test';
4.获取列索引的信息
select index_name, table_name, column_name
from user_ind_columns
where table_name='test';
5.修改索引
alter index i_test_id_type rename to i_test_id_and_type;
6.删除索引
drop index i_test_id_and_type;
7.创建位图索引
create bitmap index i_order on order(status);
视图
注意:视图并不存储行,他们始终存储在表中。视图返回存储在表中的行
1.创建视图
connect system/systemgrant create view to zoey;
connect zoey/zoey
create view test_view asselect * from test where type='02';
使用视图
select id from test_view;
注意:简单视图支持DML操作,复杂视图不支持DML操作。
2.创建具有check option约束的视图指定对视图执行的DML操作必须满足子查询的条件。
create view test_view2 asselect * from test where type='02'
with check option constraint test_view2;
3.创建具有read only约束的视图(只读)
create view test_view3 asselect * from test where type='02'
with read only constraint test_view2;
4.获取有关视图定义的信息
describe test_view3;
5.获取有关视图的约束信息
select constraint_name, constraint_type, status , deferrable, deferred
from user_constraintswhere table_name='test';
6.修改视图
create or replace view test_view3 asselect * from test where type in ('02', '01');
7.删除视图
drop view test_view3;
五.闪回数据归档
create flashback archive test_archivetablespace example --归档在example创建
quota 1 M --限额是1M
retention 1 DAy;--保留期限是1天
停止归档
alter zoey.test no flashback archive;
清除归档中的所有数据
alter flashback archive test_archive purge all;
删除闪回归档
drop flashback archive test_archive;