1. 程式人生 > >三十一、索引的弊端之讀寫分離的引入

三十一、索引的弊端之讀寫分離的引入

                   索引的弊端之讀寫分離的引入

索引=列值+rowid
索引弊端:索引是需要排序的,當資料插入時將進行重新排序,導致慢。
索引好處:查詢速度快

1、多索引的表插入資料越慢

實驗SQL:

drop table test1 purge;
drop table test2 purge;
drop table test3 purge;
drop table t purge;
create table t as select * from dba_objects;
create table test1 as select * from t;
create table test2 as select * from t;
create table test3 as select * from t;
create index idx_owner on test1(owner);
create index idx_object_name on test1(object_name);
create index idx_data_obj_id on test1(data_object_id);
create index idx_created on test1(created);
create index idx_last_ddl_time on test1(last_ddl_time);
create index idx_status on test1(status);
create index idx_t2_sta on test2(status);
create index idx_t2_objid on test2(object_id);
set timing on 
--語句1(test1表有6個索引)
insert into test1 select * from t;
commit;
--語句2(test2表有2個索引)
insert into test2 select * from t;
commit;
--語句3(test3表有無索引)
insert into test3 select * from t;
commit;

實驗結果:


SQL> --語句1(test1表有6個索引)
SQL> insert into test1 select * from t;

已建立72690行。

已用時間:  00: 00: 05.72
SQL> commit;

提交完成。

已用時間:  00: 00: 00.01
SQL> --語句2(test2表有2個索引)
SQL> insert into test2 select * from t;

已建立72690行。

已用時間:  00: 00: 02.93
SQL> commit;

提交完成。

已用時間:  00: 00: 00.00
SQL> --語句3(test3表有無索引)
SQL> insert into test3 select * from t;

已建立72690行。

已用時間:  00: 00: 00.85
SQL> commit;

提交完成。

已用時間:  00: 00: 00.01
test1表六個索引,用時:5.72
test2表兩個索引,用時:2.93
test3表無索引,用時:0.85

2、先建索引再插資料與先插資料再建索引的時間比較

drop table t purge;
create table t as select * from dba_objects;
insert into t select * from t;
insert into t select * from t;
commit;
--請從這裡開始注意累加的時間(從建索引到插入記錄完畢)
set timing on 
create index idx_t_owner on t(owner);
create index idx_t_obj_name on t(object_name);
create index idx_t_data_obj_id on t(data_object_id);
create index idx_t_created on t(created);
create index idx_t_last_ddl on t(last_ddl_time);

--語句1(t表有6個索引)
insert into t select * from t;
commit;
 

--以下進行試驗2
drop table t purge;
create table t as select * from dba_objects;
insert into t select * from t;
insert into t select * from t;
commit;

---也從這裡開始這裡開始注意累加的時間(從插入記錄完畢到建索引完畢)

set timing on 

--語句1(t表有6個索引,此時先不建)
insert into t select * from t;
create index idx_t_owner on t(owner);
create index idx_t_obj_name on t(object_name);
create index idx_t_data_obj_id on t(data_object_id);
create index idx_t_created on t(created);
create index idx_t_last_ddl on t(last_ddl_time);