oracle删除表加cascade,设值外键同时delete on cascade的效率,比分别删除两张表的效率低很多啊。...

本文通过一系列SQL语句构建并测试了数据库表,展示了在存在索引和主外键约束的情况下,级联删除操作相对于普通删除的效率提升。测试结果显示,级联删除在处理相同数量的数据时,所需时间更短,证明了其在数据维护中的高效性。
摘要由CSDN通过智能技术生成

构建测试表:

SQL> create table t1 as select * from all_objects;

Table created

SQL> create table t1t as select * from t1;

Table created

SQL> insert into t1t select * from t1t;

67752 rows inserted

SQL> insert into t1t select * from t1t;

135504 rows inserted

SQL> insert into t1t select * from t1t;

271008 rows inserted

SQL> insert into t1t select * from t1t;

542016 rows inserted

SQL> insert into t1t select * from t1t;

1084032 rows inserted

SQL> commit;

Commit complete

检查测试表数据量:

SQL> select count(*) from t1;

COUNT(*)

----------

67751

SQL> select count(*) from t1t;

COUNT(*)

----------

2168032

构建对比测试表:

SQL> create table t2 as select * from t1;

Table created

SQL> create table t2t as select * from t1t;

Table created

创建索引和主外键:

SQL> alter table T1  add constraint pk_t1 primary key (OBJECT_ID);

Table altered

SQL> create index idx_t1t_objectid on T1T (object_id);

Index created

SQL> alter table T1T

2    add constraint fk_t1t_t1 foreign key (OBJECT_ID)

3    references t1 (OBJECT_ID);

Table altered

SQL> alter table T2  add constraint pk_t2 primary key (OBJECT_ID);

Table altered

SQL> create index idx_t2t_objectid on T2T (object_id);

Index created

SQL> alter table T2T

2    add constraint fk_t2t_t2 foreign key (OBJECT_ID)

3    references t2 (OBJECT_ID) on delete cascade;

Table altered

开始测试:

SQL> set timing on

SQL> select count(*) from t1;

COUNT(*)

----------

67751

Elapsed: 00:00:00.28

SQL> select count(*) from t1t;

COUNT(*)

----------

2168032

Elapsed: 00:00:00.35

SQL> delete t1t where object_id=54;

32 rows deleted.

Elapsed: 00:00:00.19

SQL> delete t1 where object_id=54;

1 row deleted.

Elapsed: 00:00:00.00

SQL> commit;

Commit complete.

Elapsed: 00:00:00.01

SQL> delete t2 where object_id=54;

1 row deleted.

Elapsed: 00:00:00.05

SQL> commit;

Commit complete.

Elapsed: 00:00:00.01

SQL> select count(*) from t2t where object_id=54;

COUNT(*)

----------

0

Elapsed: 00:00:00.09

结论:级联删除效率更高。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值