Skip to content
Advertisement

Maximizing query performance by joining with XML

While working on query performance optimisation, I noticed that the pattern below outperforms by a wide margin other, more obvious, ways of writing the same query. After looking at the execution plans, it appears this is due to parallelism.

The table MyTable, has a clustered primary key on (Identifier, MyId, date). The @xml variable usually contains tens of entries and data returned is a few hundred thousand rows.

Is there a way to achieve parallelism without using the XML or is this a standard pattern/trick?

For example, the stored procedure below which returns the same data severely underperforms the query above (parameters for stored proc are passed in and the whole thing is executed using sp_executesql).

The actual execution plan of the XML query is shown below.

Actual Execution Plan of XML query

See also:

sp_executesql is slow with parameters

SQL Server doesn’t have the statistics for the table variable?

Advertisement

Answer

As Jeroen Mostert said, table variables do not have statistics and the actual execution plan is not optimal. In my case, the xml version of the query was parallelised whereas the stored proc was not (this is what I mean by the execution plan not being optimal).

A way to help the optimiser is to add an appropriate primary key or an index on the table variables. One can also create statistics on the table columns in question but in the SQL server that I am using, table variables do not support statistics.

Having added an index on all columns in the table variables, the optimiser started parallelising the query and the execution speed was greatly improved.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement