• Haven’t had time to look at this but at first glance I must say that this is not bad at all Jeff, the only suggestion for immediate improvement is to remove the “exist” in the WHERE clause and use IS NOT NULL instead, removes 4 operators from the execution plan.
    😎

    There could also be a benefit of adding a temp table into the mix and first insert all the Recompile elements into the temp table before querying for the attribute values, something like this:
    ;WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/2004/07/showplan' as p)
      SELECT
          xt.RowNum
       ,RECOMP.DATA.query('.') AS ELEMENT_XML
      FROM #XMLTest xt
      OUTER APPLY TheXML.nodes('//p:Recompile') RECOMP(DATA)

    This would greatly reduce the size and the complexity of the XML work-set.

    Further, you should use OPTION MAXDOP 1 as if this goes parallel then it will be very slow!

    Regarding the namespace, there is virtually no difference between the wildcard namespace and the defined namespace queries when there is only one namespace used in the XML. The wildcard approach is often handy but sometimes not applicable depending on the structure of the XML, i.e. if same elements exist within different namespaces etc.