Improving performance with INSERT statement

  • The following code uses a WHILE loop to Insert a row in table2 with every ClientID in table1 and every consecutive Year between 2000 and 2010.

    It works, but it is slow when table 1 is large, say 100,000 records.

    DECLARE @MyYear smallint

    SET @MyYear = 2000

    WHILE @MyYear <= 2010-- i.e., for each year from 2000 to 2010

    BEGIN

    INSERT INTO table2 (Year, ClientID)

    SELECT @MyYear, table1.ClientID FROM table1

    SET @MyYear = @MyYear + 1

    END

    Is there any way to speed this up by using just an INSERT statement such as:

    INSERT INTO table2 (Year, ClientID)

    SELECT <years from 2000 to 2010>, table1.ClientID FROM table1

    Where the expression <years from 2000 to 2010> increments the Year value?

    Thanks

  • Google: tally table (or dates table maybe)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (8/31/2013)


    Google: tally table (or dates table maybe)

    +100

    What kills you the way you are doing it is log buffer flushes. Google them too if you are interested. Tally table is also known as a numbers table, and there are a number of articles about what you can do with them here. Jeff Moden is a primary author to seek out. I think he also has an article here on SSC.com using them to generate test data, which would definitely be helpful for you to review as well.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • I would avoid the loop, create a temp table named #temp and insert the values of the Years from 2000 to 2010.

    I would then do your insert like this:

    Insert Into TargetTable (ClientID, Year) Select Distinct ClientID, #temp.Year from SourceTable, #temp

    Note there is no join in the second query, it works by FM (Freaking Magic)

    That ought to do it for you. You can follow me at http://www.LiveSQLHelp.com/myapp

  • LiveSqlHelp (8/31/2013)


    I would avoid the loop, create a temp table named #temp and insert the values of the Years from 2000 to 2010.

    And you would do that without a loop ....

    Note there is no join in the second query, it works by FM (Freaking Magic)

    No magic, just a cartesian product.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks ail. That's exactly what I needed.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply