Why insert-select to variable table from XML variable so slow?

后端 未结 2 1327
难免孤独
难免孤独 2020-12-03 08:09

I\'m trying to insert some data from a XML document into a variable table. What blows my mind is that the same select-into (bulk) runs in no time while insert-select takes a

相关标签:
2条回答
  • 2020-12-03 08:51

    This is a bug in SQL Server 2008. Use

    insert @columns 
    select ColumnNames.value('.', 'nvarchar(300)') name
    from @xColumns.nodes('/columns/name') T1(ColumnNames)
    OPTION (OPTIMIZE FOR ( @xColumns = NULL ))
    

    This workaround is from an item on the Microsoft Connect Site which also mentions a hotfix for this Eager Spool / XML Reader issue is available (under traceflag 4130).

    The reason for the performance regression is explained in a different connect item

    The spool was introduced due to a general halloween protection logic (that is not needed for the XQuery expressions).

    0 讨论(0)
  • 2020-12-03 09:09

    Looks to be an issue specific to SQL Server 2008. When I run the code in SQL Server 2005, both inserts run quickly and produce identical execution plans that start with the fragment shown below as Plan 1. In 2008, the first insert uses Plan 1 but the second insert produces Plan 2. The remainder of both plans beyond the fragment shown are identical.

    Plan 1

    alt text

    Plan 2

    alt text

    0 讨论(0)
提交回复
热议问题