views:

37

answers:

1

Hi,

I have an application that stores xml documents inside a column on SQL Server. The structure of the XML document is similar to the one below:

<document>
    <item>
        ...
        <phoneNumber>0123456789</phoneNumber>
        ....
    </item>
    <item>
        ...
        <phoneNumber>9876543210</phoneNumber>
        ....
    </item>
    ...
</document>

Basically this column stores a set of customer information. The XML documents can have different child elements inside the <item> element, nevertheless some of these child elements are contained in all documents (e.g. the <phoneNumber> element in the above example).

This way I can have for example, one row in the table containing the following value

<document>
    <item>
        <firstName>Carlos</firstName>
        <lastName>Loth</lastName>
        <phoneNumber>0123456789</phoneNumber>
    </item>
    <item>
        <firstName>Alberto</firstName>
        <lastName>Tomatis</lastName>
        <phoneNumber>987654321</phoneNumber>
    </item>
</document>

And another row containing this document

<document>
    <item>
        <orderNumber>XYZ</orderNumber>
        <phoneNumber>0123456789</phoneNumber>
    </item>
    <item>
        <orderNumber>ABC</orderNumber>
        <phoneNumber>987654321</phoneNumber>
    </item>
</document>

So, my question is that is it possible to create an index on that XML column based on the document/item/phoneNumber element? I need to perform a query that returns the information stored in other "fixed known" columns based on the phoneNumber information.

Any suggestions or ideas?

Thanks in advance, Carlos Loth.

A: 

Yes.
SQL Server 2005 supports four different types of XML indexes. Since an XML index is somewhat different than a relational index, it is necessary to know their implementation before we approach how to use them for maximum effectiveness. There is a single "primary XML index" and three different flavors of "secondary XML index".

For more info, see this MSDN article

You need to create a primary XML index before you can define secondary XML indexes:

CREATE PRIMARY XML INDEX xml_idx ON your_table(xml_column)
CREATE XML INDEX xml_idx ON your_table(xml_column) FOR PROPERTY

Create XML Index documentation

OMG Ponies
Thanks, but how can I specify the path inside the xml document?
Carlos Loth
Path for what? Your question was about XML index creation...
OMG Ponies
@Carlos Loth: you define an XML index on the entire XML column - and all your XML will be either indexed for property, path or value access. You cannot limit the index to a given sub-x-path in your document - it's an all or nothing thing. And it uses **a lot** of disk space!!
marc_s
@Carlos: in addition to create an index on the column, you must declare an XML schema. The xml index will help you significantly to improve your querying of the XML data with XPATH and XQUERY. But the performance will skyrocket if you declare your data to conform to a specific XML schema, and the XML indexes will have a lot more information about your data in that case.
Remus Rusanu
@OMG Ponies: I'd like to know if I could specify an XPath inside the XML column to index the xml document based on the that XPath. So my XML document would be indexed against the phoneNumber column in my case.
Carlos Loth