Sql-server – Slow insert – 300+ Million Records

insertsql serversql-server-2008-r2

The following query takes over an hour on the table to insert.

There are no indexes. There was one which was deleted but still takes an hour after deletion.

Any pointers on improving this appreciated.

INSERT  INTO SAPECC_Full.dbo.[vbep]
    ( [MANDT] ,
      [VBELN] ,
      [VBELN_oco] ,
      [POSNR] ,
      [POSNR_oco] ,
      [ETENR] ,
      [ETTYP] ,
      [LFREL] ,
      [EDATU] ,
      [EZEIT] ,
      [WMENG] ,
      [WMENG_oco] ,
      [BMENG] ,
      [BMENG_oco] ,
      [VRKME] ,
      [LMENG] ,
      [MEINS] ,
      [BDDAT] ,
      [BDART] ,
      [PLART] ,
      [VBELE] ,
      [POSNE] ,
      [ETENE] ,
      [RSDAT] ,
      [IDNNR] ,
      [BANFN] ,
      [BANFN_oco] ,
      [BSART] ,
      [BSTYP] ,
      [WEPOS] ,
      [REPOS] ,
      [LRGDT] ,
      [PRGRS] ,
      [TDDAT] ,
      [MBDAT] ,
      [LDDAT] ,
      [LDDAT_oco] ,
      [WADAT] ,
      [CMENG] ,
      [LIFSP] ,
      [GRSTR] ,
      [ABART] ,
      [ABRUF] ,
      [ROMS1] ,
      [ROMS2] ,
      [ROMS3] ,
      [ROMEI] ,
      [RFORM] ,
      [UMVKZ] ,
      [UMVKN] ,
      [VERFP] ,
      [BWART] ,
      [BNFPO] ,
      [ETART] ,
      [AUFNR] ,
      [PLNUM] ,
      [SERNR] ,
      [AESKD] ,
      [ABGES] ,
      [MBUHR] ,
      [TDUHR] ,
      [LDUHR] ,
      [WAUHR] ,
      [AULWE] ,
      OcoDateChanged
    )
    SELECT  RTRIM([MANDT]) ,
            RTRIM([VBELN]) ,
            dbo.SAPLeading0s(VBELN) ,
            RTRIM([POSNR]) ,
            dbo.SAPLeading0s(POSNR) ,
            RTRIM([ETENR]) ,
            RTRIM([ETTYP]) ,
            RTRIM([LFREL]) ,
            RTRIM([EDATU]) ,
            RTRIM([EZEIT]) ,
            RTRIM([WMENG]) ,
            dbo.SAPNumeric(WMENG) ,
            RTRIM([BMENG]) ,
            dbo.SAPNumeric(BMENG) ,
            RTRIM([VRKME]) ,
            RTRIM([LMENG]) ,
            RTRIM([MEINS]) ,
            RTRIM([BDDAT]) ,
            RTRIM([BDART]) ,
            RTRIM([PLART]) ,
            RTRIM([VBELE]) ,
            RTRIM([POSNE]) ,
            RTRIM([ETENE]) ,
            RTRIM([RSDAT]) ,
            RTRIM([IDNNR]) ,
            RTRIM([BANFN]) ,
            dbo.SAPLeading0s(BANFN) ,
            RTRIM([BSART]) ,
            RTRIM([BSTYP]) ,
            RTRIM([WEPOS]) ,
            RTRIM([REPOS]) ,
            RTRIM([LRGDT]) ,
            RTRIM([PRGRS]) ,
            RTRIM([TDDAT]) ,
            RTRIM([MBDAT]) ,
            RTRIM([LDDAT]) ,
            dbo.SAPDate(LDDAT) ,
            RTRIM([WADAT]) ,
            RTRIM([CMENG]) ,
            RTRIM([LIFSP]) ,
            RTRIM([GRSTR]) ,
            RTRIM([ABART]) ,
            RTRIM([ABRUF]) ,
            RTRIM([ROMS1]) ,
            RTRIM([ROMS2]) ,
            RTRIM([ROMS3]) ,
            RTRIM([ROMEI]) ,
            RTRIM([RFORM]) ,
            RTRIM([UMVKZ]) ,
            RTRIM([UMVKN]) ,
            RTRIM([VERFP]) ,
            RTRIM([BWART]) ,
            RTRIM([BNFPO]) ,
            RTRIM([ETART]) ,
            RTRIM([AUFNR]) ,
            RTRIM([PLNUM]) ,
            RTRIM([SERNR]) ,
            RTRIM([AESKD]) ,
            RTRIM([ABGES]) ,
            RTRIM([MBUHR]) ,
            RTRIM([TDUHR]) ,
            RTRIM([LDUHR]) ,
            RTRIM([WAUHR]) ,
            RTRIM([AULWE]) ,
            GETDATE()
    FROM    vbep_full

Best Answer

You are asking SQL Server to perform 19.5 Billion string manipulations (300,000,000 rows X 65 columns), all in one giant transaction. That is assuming vbep_Full is a regular table with simple column definitions (not a view with joins to other tables and/or with computed comlumns).

If your CPU is not maxed out, you could try breaking the work into multiple parts, process each part in parallel.

I would also recommend working in small batches with implicit transactions. That way if one of the batches fails, you could start that batch over again without having to start the other batches over.