1

the below query is taking a long time to return the results. I have tried everything I can think of to optimize the query. There is around 10000 rows of xml stored with this structure and it is taking around 2.5 MINUTES to return the results. I have set a primary xml index on the table, and a secondary (Property) index on the table. I have set the indexes to be rebuilt every 7 days.

I have referenced these locations

Performance Optimizations for the XML Data Type

Guidelines for Using xml Data Type Methods

Xml Index Guidelines

Indexing Xml

Xml Best Practices

Any advice and suggestions would be appreciated thanks.

Sorry For all the code, I can take it out if it is uneeded.

I have a table:

CREATE TABLE [dbo].[XmlTable](
    [XmlId] [int] IDENTITY(1,1) NOT NULL,
    [XmlDocument] [xml] NOT NULL,
 CONSTRAINT [PK_XmlTable] PRIMARY KEY CLUSTERED 
(
    [XmlId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

With a Property Secondary Index

SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET NUMERIC_ROUNDABORT OFF

GO

CREATE XML INDEX [IXML_XmlTable_XmlDocument_Property] ON [dbo].[XmlTable]
(
    [XmlDocument]
)
USING XML INDEX [PXML_XmlTable_XmlDocument] FOR PROPERTY WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

With a Primary Index

SET ARITHABORT ON
SET CONCAT_NULL_YIELDS_NULL ON
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
SET NUMERIC_ROUNDABORT OFF

GO


CREATE PRIMARY XML INDEX [PXML_XmlTable_XmlDocument] ON [dbo].[XmlTable]
(
    [XmlDocument]
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO

With a Structure like this:

<dev:Doc xmlns:dev="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Settings="Testing" Title="Ordering">
  <dev:Base RevisionNumber="0" Baseid="34433" />
  <dev:Rev Time="2013-01-21T15:08:00">
    <dev:Person Name="Me" Systemid="54654" />
  </dev:Rev>
  <dev:Functions Id="A1">
    <dev:A1 Number="1">
      <dev:Codes>D</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Visitors>
        <dev:Visitor Name="Dev01" Location="STLRF">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
        <dev:Visitor Name="Dev02" Location="STLRF">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
        <dev:Visitor Name="Dev03" Location="FGRTY">
          <dev:Divisions>
            <dev:Division Number="1" Name="TFR3" Usage="Monitor">
              <dev:Description>Development Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="2" Name="DEF32" Usage="Monitor">
              <dev:Description>Testing Fundamentals</dev:Description>
            </dev:Division>
            <dev:Division Number="3" Name="DEP13" Usage="None">
              <dev:Description>Guided Fundamentals</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="FGY(14A)" />
      </dev:Senders>
    </dev:A1>
  </dev:Functions>
  <dev:Functions Id="A2">
    <dev:A2 Number="1">
      <dev:Codes>C</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Remarks>Support</dev:Remarks>
      <dev:Notes>Ready</dev:Notes>
      <dev:Visitors>
        <dev:Visitor Name="GHFF">
          <dev:Divisions>
            <dev:Division Number="0" Name="Trial" Usage="None">
              <dev:FromLocation>LOPO</dev:FromLocation>
              <dev:ToLocation>RDSS</dev:ToLocation>
              <dev:Description>Rich Filter</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="W33R" />
      </dev:Senders>
      <dev:IsReady>true</dev:IsReady>
      <dev:IsCall>false</dev:IsCall>
    </dev:A2>
    <dev:A2 Number="2">
      <dev:Codes>A</dev:Codes>
      <dev:Required>true</dev:Required>
      <dev:Informational>false</dev:Informational>
      <dev:Remarks>Loader Ready</dev:Remarks>
      <dev:Notes>Ready</dev:Notes>
      <dev:Visitors>
        <dev:Visitor Name="UDT">
          <dev:Divisions>
            <dev:Division Number="0" Name="Trial" Usage="None">
              <dev:FromLocation>TYUJ</dev:FromLocation>
              <dev:ToLocation>DETF</dev:ToLocation>
              <dev:Description>Web Enhance</dev:Description>
            </dev:Division>
          </dev:Divisions>
        </dev:Visitor>
      </dev:Visitors>
      <dev:Senders>
        <dev:Sender Name="RJ4" />
      </dev:Senders>
      <dev:IsReady>true</dev:IsReady>
      <dev:IsCall>false</dev:IsCall>
    </dev:A2>
  </dev:Functions>
</dev:Doc>

And Query:

;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )
SELECT  

a.value('@RevisionNumber[1]', 'INT') AS Number,
b.value('@Id[1]', 'VARCHAR(10)') AS FunctionID,
c.value('@Number[1]', 'INT') AS Number,
d.value('@Name[1]', 'VARCHAR(10)') AS Visitor,
d.value('@Location[1]', 'VARCHAR(10)') AS Location,
e.value('@Name[1]', 'NVARCHAR(10)') AS Sender

From XmlTable as x

    CROSS APPLY xmlDocument.nodes('Doc/Base') As aa(a)
    CROSS APPLY xmlDocument.nodes('Doc/Functions') bb(b)
    CROSS APPLY b.nodes('*') cc(c)
    CROSS APPLY c.nodes('Visitors/Visitor') dd(d)
    CROSS APPLY c.nodes('Senders/Sender') ee(e)

Here is the insert into the table and for testing I ran it 10000 times

INSERT INTO XmlTable(XMLDocument)
SELECT * FROM OPENROWSET(
   BULK 'C:\Users\123\Desktop\Practice.xml',
   SINGLE_BLOB) AS x;

Go 10000

I was initially this query, but it was x3 slower than the query above

;WITH XMLNAMESPACES (default 'http://www.w3.org/2001/XMLSchema')
SELECT  
    a.value('(Base/@RevisionNumber)[1]', 'INT') AS RevNumber,
    b.value('@Id[1]', 'VARCHAR(10)') AS FunctionID,
    c.value('@Number[1]', 'INT') AS Number,
    d.value('@Name[1]', 'VARCHAR(10)') AS Visitor,
    d.value('@Location[1]', 'VARCHAR(10)') AS Location,
    c.value('(Senders/Sender/@Name)[1]', 'VARCHAR(10)') AS Sender
FROM XmlTable
CROSS APPLY xmlDocument.nodes('Doc') As aa(a)
CROSS APPLY a.nodes('Functions') bb(b)
CROSS APPLY b.nodes('*') cc(c)
CROSS APPLY c.nodes('Visitors/Visitor') dd(d)

1 Answer 1

2

First off it looks like you are doing a lot of cross applies to do the shredding. Can you not just extend off a single cross apply for a common denominator and get your value like:

a.value('/(element)/(element)[(expression)].value('(@thing)', '(type)').

My thinking is that the syntax you have is fine but you are creating a heirarchy five times differently and then cross applying it that many times. I have not done a lot of xml shredding but I know that multiple cross applies could be the killer as you taking a three dimensional value and populating a reference differently five times MAY BE the issue. You can extend elements further with '/(element)' notation.

I could be wrong and this may be fine. If you HAVE TO do that many cross applies could you do them in temp table dumps first potentially to speed it up. Then you may be able to make xml indexes on the temp table potentially as well. EG:

Select a.query('*')
into #NodeA
From XmlTable as x
    CROSS APPLY xmlDocument.nodes('Doc/Base') As aa(a)

Then create an xml index if performance is still slow that will go away after the temp tables are removed. Not sure on xml indexes though as I have not done them muct. I do know you can drop and create regular indexes on temp tables so I would assume you can do it with xml indexes.

http://msdn.microsoft.com/en-us/library/bb934097.aspx

EDIT

Keep in mind you can also mix the 'query' syntax with the 'value' syntax when shredding xml which is VERY BENEFICIAL for finding a tree structure and then populating results. I do it when shredding the xml in the SSRS Report server similar to this:

Use ReportServer
GO

With a as 
                (
                Select 
                                c.Name as ReportName
                ,               cast(sub.ExtensionSettings as xml) XML
                ,               sub.ModifiedDate as SubscriptionModifiedDate
                ,               sub.Description
                ,               sub.LastRunTime
                from dbo.ReportSchedule rs (nolock) 
                                join dbo.Schedule s (nolock) on rs.ScheduleID = s.ScheduleID
                                join dbo.Subscriptions sub (nolock) on rs.SubscriptionID = sub.SubscriptionID
                                join dbo.Catalog c (nolock) on rs.ReportID = c.ItemID
                )
select 
                a.ReportName
,               a.SubscriptionModifiedDate
,               a.Description
,               t.query('/ParameterValues/ParameterValue[* = "Subject"]/Value').value('.', 'varchar(max)') as Subject
from a
    cross apply a.XML.nodes('//ParameterValues') as n(t)
1
  • Posted an older query above that was a bigger performance hit, I will rewrite the query again and see how performance hits Apr 18, 2013 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.