索引
準則:當任何單個查詢要檢索的行少於或等於整個錶行數的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;