Slow bulk insert for table with many indexes

前端 未结 4 2156
野的像风
野的像风 2020-12-02 15:52

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 cancell

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 16:21

    This sounds like a data warehouse operation. It would be normal to drop the indexes before the insert and rebuild them afterwards.

    When you rebuild the indexes, build the clustered index first, and conversely drop it last. They should all have fillfactor 100%.

    Code should be something like this

    if object_id('Index') is not null drop table IndexList
    select name into Index from dbo.sysindexes where id = object_id('Fact')
    
    if exists (select name from Index where name = 'id1') drop index Fact.id1
    if exists (select name from Index where name = 'id2') drop index Fact.id2        
    if exists (select name from Index where name = 'id3') drop index Fact.id3
    .
    .
    BIG INSERT
    
    RECREATE THE INDEXES
    

提交回复
热议问题