views:

271

answers:

0

I wrote the script below in order to test the performance difference between OPENXML and the newer functions available for the XML datatype in SQL Server 2005.

When I run the script (with the option to include the actual execution plan), the results were strange.

The execution plan said that the cost of the first query (using OPENXML) was 1%, while the other 99% was the cost of the second query (using XML.nodes and XML.value).

Can anyone tell me why this would be? Am I reading this wrong? I would have assumed that the new method would out perform the old method.

UPDATE: Included results from SET STATISTICS TIME ON

These are the results for the first query (using OPENXML):

SQL Server parse and compile time: CPU time = 32 ms, elapsed time = 42 ms.

SQL Server Execution Times: CPU time = 62 ms, elapsed time = 84 ms.

SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.

SQL Server Execution Times: CPU time = 141 ms, elapsed time = 158 ms.

SQL Server Execution Times: CPU time = 109 ms, elapsed time = 109 ms.

SQL Server Execution Times: CPU time = 16 ms, elapsed time = 1 ms.

SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms.

These are the results for the second query (using XML.nodes and XML.value):

SQL Server parse and compile time: CPU time = 19 ms, elapsed time = 19 ms.

SQL Server Execution Times: CPU time = 63 ms, elapsed time = 64 ms.

SQL Server Execution Times: CPU time = 0 ms, elapsed time = 0 ms.

SQL Server Execution Times: CPU time = 235 ms, elapsed time = 232 ms.

SQL Server Execution Times: CPU time = 0 ms, elapsed time = 2 ms.


Here is the script:

declare @Data xml
set @Data=convert(xml,N'<Data>
    <Row>
        <SequenceNumber>1</SequenceNumber>
        <Action>Explode</Action>
        <PartNumber>50-54448-1</PartNumber>
        <Quantity>4</Quantity>
    </Row>
</Data>')


CREATE TABLE #Tbl (
    SequenceNumber INT NOT NULL,
    [Action] VARCHAR(500) NULL,
    PartNumber NVARCHAR(60) NULL,
    Quantity FLOAT NULL
)


DECLARE @iDoc int

EXEC sp_xml_preparedocument @iDoc OUTPUT, @Data

INSERT INTO #Tbl (
    SequenceNumber,
    [Action],
    PartNumber,
    Quantity
)
SELECT
    SequenceNumber,
    [Action],
    PartNumber,
    Quantity
FROM OPENXML(@iDoc, '/Data/Row', 2) WITH (
    SequenceNumber INT,
    [Action] VARCHAR(500),
    PartNumber NVARCHAR(60),
    Quantity FLOAT
)

-- remove the xml doc
EXEC sp_xml_removedocument @iDoc



INSERT INTO #Tbl (
    SequenceNumber,
    [Action],
    PartNumber,
    Quantity
)
SELECT
    Input.Data.value('SequenceNumber[1]', 'INT'),
    Input.Data.value('Action[1]', 'VARCHAR(500)'),
    Input.Data.value('PartNumber[1]', 'VARCHAR(60)'),
    Input.Data.value('Quantity[1]', 'FLOAT')
FROM @Data.nodes('Data/Row') AS Input(Data)


DROP TABLE #Tbl