Wednesday, August 1, 2012

IGNORE_ROW_ON_DUPKEY_INDEX HINT

In oracle 11gr2 there is a new hint call IGNORE_ROW_ON_DUPKEY_INDEX.
It does wat it says ignore duplicate rows violation.

SQL> create table aap(id number constraint aap_pk_idx primary key
           using index(create index aap_pk_idx on aap(id)), a1 varchar2(3));
Table created.

SQL> insert into aap select rownum, 'AHH' from dual connect by level <= 10;
10 rows created.
SQL> commit;
Commit complete

SQL> insert into aap select rownum, 'AHH' from dual connect by level <= 12;
     insert into aap select rownum, 'AHH' from dual connect by level <= 12
      *
ERROR at line 1:
ORA-00001: unique constraint (GDA001.AAP_PK_IDX) violated
Oke now that is what was expected. Lets use the hint.

SQL>insert /*+ ignore_row_on_dupkey_index(aap,aap_pk_idx) */
         into aap select rownum, 'AHH'
         from dual connect by level <= 12;
into aap select rownum, 'AHH'
     *
ERROR at line 2:
ORA-38913: Index specified in the index hint is invalid
Why doesn't the hint gives this messages Well

SQL> select index_name, uniqueness, table_name
           from dba_indexes
          where index_name='AAP_PK_IDX'; 
INDEX_NAME                     UNIQUENES TABLE_NAME
------------------------------ --------- ------------------------------
AAP_PK_IDX                     NONUNIQUE AAP

The index in non unique and for the hint to work is has to be.

SQL>create table aap(id number constraint aap_pk_idx primary key
          using index(create unique index aap_pk_idx on aap(id)), a1 varchar2(3));
.table created

SQL> select index_name, uniqueness, table_name
           from dba_indexes
          where index_name='AAP_PK_IDX'; 
INDEX_NAME                     UNIQUENES TABLE_NAME
------------------------------ --------- ------------------------------
AAP_PK_IDX                      UNIQUE         AAP

SQL> insert into aap select rownum, 'AHH' from dual connect by level <= 10;
SQL commit;

SQL> insert /*+ ignore_row_on_dupkey_index(aap,aap_pk_idx) */
     into aap select rownum, 'AHH'
     from dual connect by level <= 12;
2 rows created.

So with this hint duplicate are ignored.

SQL> select * from aap;
        ID A1
---------- ---
         1 AHH
         2 AHH
         3 AHH
         4 AHH
         5 AHH
         6 AHH
         7 AHH
         8 AHH
         9 AHH
        10 AHH
        11 AHH
        ID A1
---------- ---
        12 AHH
12 rows selected.
And there is 1 thing. The hint doesn't work with the update statement.

SQL> update /*+ ignore_row_on_dupkey_index(aap,aap_pk_idx) */
          aap set id = 13 where id = 5;
   update /*+ ignore_row_on_dupkey_index(aap,aap_pk_idx) */
*
ERROR at line 1:
ORA-38917: IGNORE_ROW_ON_DUPKEY_INDEX hint disallowed for this operation

Thats it.

No comments:

Post a Comment