Oracle 11G - Performance effect of indexing at insert

后端 未结 3 950
悲哀的现实
悲哀的现实 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:21

    It's true that it is faster to modify a table if you do not also have to modify one or more indexes and possibly perform constraint checking as well, but it is also largely irrelevant if you then have to add those indexes. You have to consider the complete change to the system that you wish to effect, not just a single part of it.

    Obviously if you are adding a single row into a table that already contains millions of rows then it would be foolish to drop and rebuild indexes.

    However, even if you have a completely empty table into which you are going to add several million rows it can still be slower to defer the indexing until afterwards.

    The reason for this is that such an insert is best performed with the direct path mechanism, and when you use direct path inserts into a table with indexes on it, temporary segments are built that contain the data required to build the indexes (data plus rowids). If those temporary segments are much smaller than the table you have just loaded then they will also be faster to scan and to build the indexes from.

    the alternative, if you have five index on the table, is to incur five full table scans after you have loaded it in order to build the indexes.

    Obviously there are huge grey areas involved here, but well done for:

    1. Questioning authority and general rules of thumb, and
    2. Running actual tests to determine the facts in your own case.

    Edit:

    Further considerations -- you run a backup while the indexes are dropped. Now, following an emergency restore, you have to have a script that verifies that all indexes are in place, when you have the business breathing down your neck to get the system back up.

    Also, if you absolutely were determined to not maintain indexes during a bulk load, do not drop the indexes -- disable them instead. This preserves the metadata for the indexes existence and definition, and allows a more simple rebuild process. Just be careful that you do not accidentally re-enable indexes by truncating the table, as this will render disabled indexes enabled again.

提交回复
热议问题