Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow bulk insert for table with many indexes

I try to insert millions of records into a table that has more than 20 indexes.

In the last run it took more than 4 hours per 100.000 rows, and the query was cancelled after 3½ days...

Do you have any suggestions about how to speed this up.

(I suspect the many indexes to be the cause. If you also think so, how can I automatically drop indexes before the operation, and then create the same indexes afterwards again?)

Extra info:

  • The space used by the indexes is about 4 times the space used by the data alone
  • The inserts are wrapped in a transaction per 100.000 rows.

Update on status:

The accepted answer helped me make it much faster.

like image 746
Ole Lynge Avatar asked Apr 15 '09 10:04

Ole Lynge


People also ask

Do indexes make inserts slower?

The number of indexes on a table is the most dominant factor for insert performance. The more indexes a table has, the slower the execution becomes.

How can I speed up bulk insert?

Below are some good ways to improve BULK INSERT operations : Using TABLOCK as query hint. Dropping Indexes during Bulk Load operation and then once it is completed then recreating them. Changing the Recovery model of database to be BULK_LOGGED during the load operation.

Is bulk insert faster than insert?

In case of BULK INSERT, only extent allocations are logged instead of the actual data being inserted. This will provide much better performance than INSERT. The actual advantage, is to reduce the amount of data being logged in the transaction log.

Is it good to have multiple indexes on a table?

Yes you can have too many indexes as they do take extra time to insert and update and delete records, but no more than one is not dangerous, it is a requirement to have a system that performs well.


1 Answers

You can disable and enable the indexes. Note that disabling them can have unwanted side-effects (such as having duplicate primary keys or unique indices etc.) which will only be found when re-enabling the indexes.

--Disable Index ALTER INDEX [IXYourIndex] ON YourTable DISABLE GO  --Enable Index ALTER INDEX [IXYourIndex] ON YourTable REBUILD GO 
like image 151
Lucero Avatar answered Sep 28 '22 04:09

Lucero