The constraint also helps speed up the query.

本文通过实验展示Oracle数据库中约束如何影响查询性能。不仅限于保持数据完整性,合适的约束也能加速查询过程。文中提供了创建表、索引及使用不同约束条件下的查询计划对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As we know the constraint is an object to keep data integrity in oracle database.

But it is also help accelerate the query.So we should not disable the contraint simply just because our data is cleansed in our database warehouse. Let us look the following test on this.

$ sqlplus /

SQL*Plus: Release 10.2.0.3.0 - Production on Sun Nov 28 23:51:53 2010

Copyright (c) 1982, 2006, Oracle. All Rights Reserved.

Connected to:

Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production

With the Partitioning, Real Application Clusters, Oracle Label Security, OLAP

and Data Mining options

SQL> create table t as select * from all_objects;

Table created.

SQL> create index t_idx on t(object_type);

Index created.

SQL> exec dbms_stats.gather_table_stats(user,'T');

PL/SQL procedure successfully completed.

SQL> select count(*) from t;

Execution Plan

----------------------------------------------------------

Plan hash value: 1842905362

-------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU)| Time |

-------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 233 (3)| 00:00:02 |

| 1 | SORT AGGREGATE | | 1 | | |

| 2 | TABLE ACCESS FULL| T | 52862 | 233 (3)| 00:00:02 |

-------------------------------------------------------------------

SQL> alter table t modify object_type not null;

Table altered.

SQL> select count(*) from t;

Execution Plan

----------------------------------------------------------

Plan hash value: 1799443504

-----------------------------------------------------------------------

| Id | Operation | Name | Rows | Cost (%CPU)| Time |

-----------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 49 (7)| 00:00:01 |

| 1 | SORT AGGREGATE | | 1 | | |

| 2 | INDEX FAST FULL SCAN| T_IDX | 52862 | 49 (7)| 00:00:01 |

-----------------------------------------------------------------------

SQL> alter table t modify object_type null;

Table altered.

SQL> select * from t where object_type is null;

Execution Plan

----------------------------------------------------------

Plan hash value: 2153619298

--------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

--------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 94 | 236 (4)| 00:00:02 |

|* 1 | TABLE ACCESS FULL| T | 1 | 94 | 236 (4)| 00:00:02 |

--------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter("OBJECT_TYPE" IS NULL)

SQL> drop index t_idx;

Index dropped.

SQL> create index t_idx on t(object_type,0);

Index created.

SQL> select * from t where object_type is null;

Execution Plan

----------------------------------------------------------

Plan hash value: 1020776977

-------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

-------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 1 | 94 | 1 (0)| 00:00:01 |

| 1 | TABLE ACCESS BY INDEX ROWID| T | 1 | 94 | 1 (0)| 00:00:01 |

|* 2 | INDEX RANGE SCAN | T_IDX | 1 | | 1 (0)| 00:00:01 |

-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - access("OBJECT_TYPE" IS NULL)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值