title: Difference between SQL 2005 and SQL 2008 for inserting multiple rows with XML
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3978807/why-insert-select-to-variable-table-from-xml-variable-so-slow">Why insert-select to variable table from XML variable so slow?</a>  </p>
  </blockquote>
  
  
  
  <p>I am using the following SQL code for inserting multiple rows of data in a table. The data is passed to the stored procedure using an XML variable :</p>
  
  <pre><code>INSERT INTO MyTable
     SELECT SampleTime = T.Item.value('SampleTime[1]', 'datetime'),
            Volume1 = T.Item.value('Volume1[1]', 'float'),
            Volume2 = T.Item.value('Volume2[1]', 'float')
     FROM @xml.nodes('//Root/MyRecord') T(item)
  </code></pre>
  
  <p>I have a whole bunch of unit tests to verify that I am inserting the right information, the right number of records, etc.. when I call the stored procedure. </p>
  
  <p>All fine and dandy - that is, until we began to monkey around with the compatibility level of the database.</p>
  
  <p>The code above worked beautifully as long as we kept the compatibility level of the DB at 90 (SQL 2005).
  When we set the compatibility level at 100 (SQL 2008), the unit tests failed, because the stored procedure using the code above times out.</p>
  
  <p>The unit tests are dropping the database, re-creating it from scripts, and running the tests on the brand new DB, so it's not - I think - a question of the 'old compatibility level' sticking around.</p>
  
  <p>Using the SQL Management studio, I made up a quick test SQL script. Using the same XML chunk, I alter the DB compat level , truncate the table, then use the code above to insert 650 rows. When the level is 90 (SQL 2005), it runs in milliseconds. When the level is 100 (SQL 2008) it sometimes takes over a minute, sometimes runs in milliseconds.</p>
  
  <p>I'd appreciate any insight anyone might have into that.</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>The script takes over a minute to run with my actual data, which has more rows than I show here, is a real table, and has an index.
  With the following example code, the difference goes between milliseconds and around 5 seconds.</p>
  
  <pre><code>--use [master]
  --ALTER DATABASE MyDB SET compatibility_level =100
  
  use [MyDB]
  
  declare @xml xml
  
  set @xml = '&lt;?xml version="1.0"?&gt;
  &lt;Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Record&gt;
      &lt;SampleTime&gt;2009-01-24T00:00:00&lt;/SampleTime&gt;
      &lt;Volume1&gt;0&lt;/Volume1&gt;
      &lt;Volume2&gt;0&lt;/Volume2&gt;
    &lt;/Record&gt;
  ..... 653 records, sample time spaced out 4 hours ........
  &lt;/Root&gt;'
  
  DECLARE @myTable TABLE(
  ID int IDENTITY(1,1) NOT NULL,
      [SampleTime] [datetime] NOT NULL,
      [Volume1] [float] NULL,
      [Volume2] [float] NULL)
  
  INSERT INTO @myTable
      select 
          T.Item.value('SampleTime[1]', 'datetime') as SampleTime,
              Volume1 = T.Item.value('Volume1[1]', 'float'),
              Volume2 = T.Item.value('Volume2[1]', 'float')
      FROM @xml.nodes('//Root/Record') T(item)
  </code></pre>
  
  <p>I uncomment the 2 lines at the top, select them and run just that (the ALTER DATABASE statement), then comment the 2 lines, deselect any text and run the whole thing.</p>
  
  <p>When I change from 90 to 100, it runs all the time in 5 seconds (I change the level once, but I run the series several times to see if I have consistent results).
  When I change from 100 to 90, it runs in milliseconds all the time.
  Just so you can play with it too.
  I am using SQL Server 2008 R2 standard edition.</p>