Unique index vs. unique constraint that uses a uni

2019-05-07 02:23发布

问题:

I am trying to compare two schemas to ensure that they are in sync. In one schema, I see a unique index on 2 columns. In addition to this, I see a unique constraint that uses the unique index.

I'm new to Oracle, but my general understanding and according to this a unique constraint only ensures no duplicates, that it isn't necessarily indexed. For performance an index should be added over the FK columns to allow the constraint to be performed efficiently.

In my target database, I have only the unique index HD_FILL_DISPNSNG_FEE_VAL_AK1.

Since this index it is marked "UNIQUE" doesn't this provide both the unique constraint and the performance benefits of an index? I see many posts on the subject like the referenced link but I am having trouble applying it to this particular situation.

Questions: Would there be any downside to removing the UNIQUE CONSTRAINT since I have the unique index? Similarly, how does a unique constraint that uses an index different than a unique index?

CREATE UNIQUE INDEX RXFINODS_O.HD_FILL_DISPNSNG_FEE_VAL_AK1 ON RXFINODS_O.HD_FILL_DISPNSNG_FEE_VAL
(CVRG_TY_CD, VAL_EFF_START_DT)
LOGGING
TABLESPACE RXFINODS_INDEX
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            NEXT             1M
            MAXSIZE          UNLIMITED
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

-- 
-- Non Foreign Key Constraints for Table HD_FILL_DISPNSNG_FEE_VAL 
-- 
ALTER TABLE RXFINODS_O.HD_FILL_DISPNSNG_FEE_VAL ADD (
  CONSTRAINT HD_FILL_DSPSNG_FEE_VAL_AK1
  UNIQUE (CVRG_TY_CD, VAL_EFF_START_DT)
  USING INDEX .HD_FILL_DISPNSNG_FEE_VAL_AK1
  ENABLE VALIDATE);

回答1:

"that it isn't necessarily indexed." Oracle, for performance reasons, is always going to insure that there is an index for any unique constraint (including primary key constraints.) By default Oracle will create a unique index when the unique constraint is created.


"how does a unique constraint that uses an index different than a unique index?"

The index allows duplicate entries.

Example 1: Deferable constraints. On insert, the index needs to allow duplicate entries because duplicates are allowed until the transaction commits. Then on commit, Oracle checks for duplicates.

Example 2: An index is needed on A, B & C for performance reasons. A unique constraint is needed on A. One can tell Oracle to use the index on A, B & C to enforce the unique constraint on A even if the index is non-unique. So Oracle will not have to maintain another index.


"Would there be any downside to removing the UNIQUE CONSTRAINT since I have the unique index?"

There is no upside to removing the constraint.



标签: oracle12c