http://www.developer.com/db/article.php/3565996/XML-Queries-and-Indexing-in-SQL-Server-2005.htm
SQL Server 2005 is the first version of Microsoft's flagship database
to truly embrace XML. True, SQL Server 2000 has some bolt-on XML
functionality, but in SQL Server 2005 XML is pervasive. In particular, you
can use the new XML columns to actually store XML documents as part of a
table. In this article, I'll dig into another part of the XML story:
indexing those columns for faster searching. XML columns allow you to store XML documents or fragments in a SQL
Server database (an XML fragment is an XML document without the
top-level To create an XML column, you assign the This creates a table with three columns, the third of which can store
XML documents. To insert data into an XML columns, you can use string
constants. These will be implicitly converted to XML when you perform the
insert. For example: There are two methods to extract data from XML columns: the
Here This gives the results shown in Figure 1. As an alternative to the For example: This gives the results shown in Figure 2. XML columns are stored as binary large objects (BLOBs) in your database.
Because of this, queries that search within an XML column can be slow. You
can speed up these queries by creating a primary XML index on each XML column.
A primary XML index is a special index that "shreds" the XML data to store
information including: To create a primary XML index, execute the For the table I've been working with, this would be: A primary XML index will speed up most queries against an XML column,
and it's a good idea to create one whenever you'll be searching XML
columns that contain large amounts of data. In addition to the primary XML index, each XML column can have up to
three secondary XML indexes. These are specialized XML indexes that
help with particular types of XML queries, and can only be created on
columns that already have a primary XML index: To create a secondary XML index, you must use the Maintaining XML indexes is costly, especially if the data in the XML
column changes frequently. Avoid creating secondary XML indexes unless
you frequently perform the types of queries that they are specifically
designed to help with. A final word of caution: just because SQL Server 2005 can store
XML columns doesn't mean that you have to put XML columns in every
database. In many cases, it will make more sense to break down the data
that might go into XML columns and make regular relational columns from it
instead. But in cases where SQL Server needs to interoperate with some
other component that already depends on XML, storing data in XML
columns can make a lot of sense. That way you can avoid doing constant
data conversions, and use a pure XML format throughout the business
process. In those cases, think carefully about your querying requirements,
create indexes as necessary, and test performance carefully. You'll
prohably find that XML columns make your life considerably easier. Mike Gunderloy is the author of over 20 books and numerous articles on
development topics, and the Senior Technology Partner for Adaptive Strategy, a
Washington State consulting firm. When
he's not writing code, Mike putters in the garden on his farm in eastern
Washington state.
XML Queries and Indexing in SQL Server 2005
November 22, 2005
Storing XML in a Table
xml element. When you add an XML column to a table,
each row of the table stores an individual XML document or fragment. The
individual documents cannot exceed 2GB each. So, for example, you might
create a table of document information in a publishing-related database
that contained the actual text of documents in XML format as a single
column.xml data type to
the column, whether you're using the CREATE TABLE statement or
creating the table with SQL Server Management Studio. Here's an
example:
CREATE TABLE Document (
DocID int IDENTITY NOT NULL,
Description nvarchar(50) NOT NULL,
DocumentStore xml NOT NULL,
CONSTRAINT PK_Document PRIMARY KEY CLUSTERED
(
DocID ASC
)
)
INSERT INTO Document (Description, DocumentStore)
VALUES('Bruce''s poem',
N'
Writing XML Queries
.value method and the .query method. When
you want to extract a single piece of information from an XML
column and have it returned as a SQL data type (such as an int
or a varchar), use the .value method. Its syntax is as
follows:value(XQuery, SQLType)XQuery is an XQuery expression, and SQLType
is the name of the target SQL data type. For example:
SELECT DocumentStore.value('(/Document/@Name)[1]',
'varchar(50)' )
FROM Document
.value method you can use the
.query method. The .query method is used to run
an XQuery expression against an XML column, returning the result as a
value of the xml data type. Its syntax is simple:query('XQuery')
SELECT DocumentStore.query('(/Document/Text)')
FROM Document
Primary XML Indexes
CREATE PRIMARY
XML INDEX statement:
CREATE PRIMARY XML INDEX index_name
ON table_name (xml_column_name)
CREATE PRIMARY XML INDEX PXML_Document
ON Document (DocumentStore)
Secondary XML Indexes
PATH secondary XML index helps with queries that
use XML path expressions.VALUE secondary XML index helps with queries that
search for values anywhere in the XML document.PROPERTY secondary XML index helps with queries
that retrieve particular object properties from within an XML
document.CREATE XML
INDEX statement:
CREATE XML INDEX index_name
ON table_name (xml_column_name)
[USING XML INDEX xml_index_name
[FOR {VALUE|PATH|PROPERTY}]
Use Your Power Wisely
About the Author