We can create the index before creating the primary key.
2010-11-30 16:09
302 查看
Our system encourter a big index contention then we found the index is created by the primary key.
then we think about if we can remove the index instead of keeping the primary key if the index is not used by any query.
but we find it is impossiable.
We can create the index before creating the primary key.
As we Know the index will be created at same time when we create the primary key.
But we can create the index maually before creating the primary key.
This is helpful when the index is big.
We can compress ,parallel option etc.
See the example as below.
$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.3.0 - Production on
Tue Nov 30 02:20:26 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> alter table t add constraint pk_t
primary key (OBJECT_ID);
Table altered.
The index is created at same time and also named as pk_t.
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
-----------------------------------------------------------
SQL> set autotrace off;
SQL> select CONSTRAINT_NAME,INDEX_NAME from dba_constraints where TABLE_NAME='T';
CONSTRAINT_NAME INDEX_NAME
------------------------------ ------------------------------
PK_T PK_T
SQL> drop index PK_T;
drop index PK_T
*
ERROR at line 1:
ORA-02429: cannot drop index used for
enforcement of unique/primary key
The index and constraint are coupled. You can not drop the index but have the constraint.
But you can drop the constraint while have the index with the following command.
SQL> alter table t drop constraint PK_T
keep index;
Table altered.
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
-----------------------------------------------------------
SQL> alter table t add constraint pk_t2
primary key (OBJECT_ID);
Table altered.
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
SQL> set autotrace off;
SQL> select CONSTRAINT_NAME,INDEX_NAME from dba_constraints where TABLE_NAME='T';
CONSTRAINT_NAME INDEX_NAME
------------------------------ ------------------------------
PK_T2 PK_T
SQL> drop index PK_T;
drop index PK_T
*
ERROR at line 1:
ORA-02429: cannot drop index used for
enforcement of unique/primary key
SQL> alter table t drop constraint PK_T;
alter table t drop constraint PK_T
*
ERROR at line 1:
ORA-02443: Cannot drop constraint
- nonexistent constraint
SQL> alter table t drop constraint PK_T2;
Table altered.
If we used this command then the index PK_T was dropped with the constrain PK_T2.
That was to say the constraint PK_T2 associated with index PK_T automitically.
SQL> select
INDEX_NAME,TABLE_OWNER,INDEX_TYPE
from
dba_indexes where TABLE_NAME='T';
INDEX_NAME
TABLE_OWNER
------------------------------
------------------------------
INDEX_TYPE
---------------------------
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
--------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
--------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
226
(2)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
TABLE ACCESS FULL| T
| 36063 |
226
(2)|
--------------------------------------------------------
then we think about if we can remove the index instead of keeping the primary key if the index is not used by any query.
but we find it is impossiable.
We can create the index before creating the primary key.
As we Know the index will be created at same time when we create the primary key.
But we can create the index maually before creating the primary key.
This is helpful when the index is big.
We can compress ,parallel option etc.
See the example as below.
$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.3.0 - Production on
Tue Nov 30 02:20:26 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> alter table t add constraint pk_t
primary key (OBJECT_ID);
Table altered.
The index is created at same time and also named as pk_t.
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
-----------------------------------------------------------
SQL> set autotrace off;
SQL> select CONSTRAINT_NAME,INDEX_NAME from dba_constraints where TABLE_NAME='T';
CONSTRAINT_NAME INDEX_NAME
------------------------------ ------------------------------
PK_T PK_T
SQL> drop index PK_T;
drop index PK_T
*
ERROR at line 1:
ORA-02429: cannot drop index used for
enforcement of unique/primary key
The index and constraint are coupled. You can not drop the index but have the constraint.
But you can drop the constraint while have the index with the following command.
SQL> alter table t drop constraint PK_T
keep index;
Table altered.
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
-----------------------------------------------------------
SQL> alter table t add constraint pk_t2
primary key (OBJECT_ID);
Table altered.
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
-----------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
-----------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
38
(8)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
INDEX FAST FULL SCAN| PK_T | 36063
|
38
(8)|
SQL> set autotrace off;
SQL> select CONSTRAINT_NAME,INDEX_NAME from dba_constraints where TABLE_NAME='T';
CONSTRAINT_NAME INDEX_NAME
------------------------------ ------------------------------
PK_T2 PK_T
SQL> drop index PK_T;
drop index PK_T
*
ERROR at line 1:
ORA-02429: cannot drop index used for
enforcement of unique/primary key
SQL> alter table t drop constraint PK_T;
alter table t drop constraint PK_T
*
ERROR at line 1:
ORA-02443: Cannot drop constraint
- nonexistent constraint
SQL> alter table t drop constraint PK_T2;
Table altered.
If we used this command then the index PK_T was dropped with the constrain PK_T2.
That was to say the constraint PK_T2 associated with index PK_T automitically.
SQL> select
INDEX_NAME,TABLE_OWNER,INDEX_TYPE
from
dba_indexes where TABLE_NAME='T';
INDEX_NAME
TABLE_OWNER
------------------------------
------------------------------
INDEX_TYPE
---------------------------
SQL> set autotrace trace exp
SQL> select count(*) from t;
Execution Plan
----------------------------------------------------------
--------------------------------------------------------
| Id
|
Operation
| Name | Rows
| Cost (%CPU)|
--------------------------------------------------------
|
0 |
SELECT STATEMENT
|
|
1 |
226
(2)|
|
1
|
SORT AGGREGATE
|
|
1 |
|
|
2
|
TABLE ACCESS FULL| T
| 36063 |
226
(2)|
--------------------------------------------------------
相关文章推荐
- The primary key can work with the index well
- Mongodb分片问题之-please create an index that starts with the shard key before sharding
- phoenix create table Order of columns in primary key constraint must match the order in which they'r
- Can we Create the Connection in Universe using JDBC drivers option
- Should we stop to find what on earth has changed before we can move forward?
- MySQL - EF : The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull
- mac curl: (58) SSL: Can't load the certificate and its private key: OSStatus -25299 的解决方案
- mysql key primary key unique index index的区别
- How can we double the data rate of 802.11a?
- VMWARE 安装失败 “FAILED TO CREATE THE REQUESTED REGISTRY KEY KEY
- openstack newton dashboard报错RuntimeError: Unable to create a new session key. It is likely that the
- Find out all the Primary Key and Foreign Key Constraints in a table
- VMware安装失败 “Failed to create the requested registry key Key:installer Error:1021"
- phprpc Can't find the key for decryption 解决方案
- win7 安装 vmware出错: failed to create the requested registry key key installer error 1021 的解决办法。
- 【集算器】使用switch函数提示“the primary key is not the only”
- The "sampleIndex" KeyPath Of CABasicAnimation
- Using the Index Server to create Query Page in asp.net
- Create the struct of index page via CSS
- RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.