sql >> Base de Datos >  >> RDS >> Sqlserver

Cómo optimizar el SQL 'XQuery'

Si solo desea un valor de cada fila, no es necesario usar cross apply .

select XMLCol.value('(/*[local-name()=sql:variable("@Root")]
                      /*[local-name(.)=sql:variable("@Entity")]
                      /*[local-name(.)=sql:variable("@ParentNode")]
                      /*[local-name(.)=sql:variable("@Separator")]
                      /*[local-name(.)=sql:variable("@ChildNode")])[1]', 'varchar(max)')
from XMLTable

Otra forma de obtener lo mismo es usar FLWOR . En mis pruebas limitadas, esto funcionará un poco más rápido.

select XMLCol.value('(for $n1 in /*,
                          $n2 in $n1/*,
                          $n3 in $n2/*,
                          $n4 in $n3/*,
                          $n5 in $n4/*
                      where $n1[local-name(.) = sql:variable("@Root")] and
                            $n2[local-name(.) = sql:variable("@Entity")] and
                            $n3[local-name(.) = sql:variable("@ParentNode")] and
                            $n4[local-name(.) = sql:variable("@Separator")] and
                            $n5[local-name(.) = sql:variable("@ChildNode")]
                      return $n5
                     )[1]', 'varchar(max)')
from XMLTable