Oracle 11G - Performance effect of indexing at insert

后端 未结 3 966
悲哀的现实
悲哀的现实 2021-01-06 01:50

Objective

Verify if it is true that insert records without PK/index plus create thme later is faster than insert with PK/Index.

Note

3条回答
  •  感情败类
    2021-01-06 02:22

    Oracle has to do more work while inserting data into table having an index. In general, inserting without index is faster than inserting with index.

    Think in this way,

    • Inserting rows in a regular heap-organized table with no particular row order is simple. Find a table block with enough free space, put the rows randomly.

    • But, when there are indexes on the table, there is much more work to do. Adding new entry for the index is not that simple. It has to traverse the index blocks to find the specific leaf node as the new entry cannot be made into any block. Once the correct leaf node is found, it checks for enough free space and then makes the new entry. If there is not enough space, then it has to split the node and distribute the new entry into old and new node. So, all this work is an overhead and consumes more time overall.

    Let's see a small example,

    Database version :

    SQL> SELECT banner FROM v$version where ROWNUM =1;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
    

    OS : Windows 7, 8GB RAM

    With Index

    SQL> CREATE TABLE t(A NUMBER, CONSTRAINT PK_a PRIMARY KEY (A));
    
    Table created.
    
    SQL> SET timing ON
    SQL> INSERT INTO t SELECT LEVEL FROM dual CONNECT BY LEVEL <=1000000;
    
    1000000 rows created.
    
    Elapsed: 00:00:02.26
    

    So, it took 00:00:02.26. Index details:

    SQL> column index_name format a10
    SQL> column table_name format a10
    SQL> column uniqueness format a10
    SQL> SELECT index_name, table_name, uniqueness FROM user_indexes WHERE table_name = 'T';
    
    INDEX_NAME TABLE_NAME UNIQUENESS
    ---------- ---------- ----------
    PK_A       T          UNIQUE
    

    Without Index

    SQL> DROP TABLE t PURGE;
    
    Table dropped.
    
    SQL> CREATE TABLE t(A NUMBER);
    
    Table created.
    
    SQL> SET timing ON
    SQL> INSERT INTO t SELECT LEVEL FROM dual CONNECT BY LEVEL <=1000000;
    
    1000000 rows created.
    
    Elapsed: 00:00:00.60
    

    So, it took only 00:00:00.60 which is faster compared to 00:00:02.26.

提交回复
热议问题