Use a Native XML Database for Your XML Data
Deciding when an XQuery-based native XML database is better than an SQL database
May. 26, 2005 01:00 PM
dbxml> putDocument "" "for $i in (0 to 2999) return
<part number='{$i}'>
<description>Description of {$i}</description><category>
{$i mod 10}</category>
{if (($i mod 10) = 0) then <parent-part>{$i mod 3}</
parent-part> else ''}</part>" q
A single output line for each document will be printed as confirmation. The generated content looks like this:
<part number="999">
<description>Description of 999</description>
<category>9</category>
</part>
Every 10th document of the 3,000 generated will look something like this:
<part number="990">
<description>Description of 990</description>
<category>0</category>
<parent-part>0</parent-part>
</part>
The numbers vary to allow more complex queries later. Let's dive in. First, let's find all part records that contain a parent-part, or "from the container named parts select all part elements that also contain a parent-part as a direct child of that element" (see Listing 3).
If you're only interested in the parent-part element, try the following:
dbxml> query collection("parts")/part/parent-part
300 objects returned for eager expression 'collection
("parts")/part/parent-part'
dbxml> print
<parent-part>0</parent-part>
<parent-part>0</parent-part>
...
<parent-part>2</parent-part>
<parent-part>2</parent-part>
Or if you simply need the value of the parent-part element, execute this query:
dbxml> query collection("parts")/part/parent-part/text()
300 objects returned for eager expression 'collection("parts")/
part/parent-part/text()'
dbxml> print
0
0
...
2
2
To find documents without parent-part elements, see Listing 4.
To find two specific parts by part number try the following:
dbxml> query 'collection("parts")/part[@number =
1070 or @number = 1032]'
2 objects returned for eager expression 'collection("parts")/
part[@number = 1070 or @number = 1032]'
dbxml> print
<part number="1070"><description>Description of 1070</
description><category>0</category><parent-part>2</
parent-part></part>
<part number="1032"><description>Description of 1032</
description><category>2</category></part>
Listing 5 shows it's equally simple to find a range of parts.
Now let's explore the effect of indices. Your query times may differ as your machine is likely different from ours, but the improvement should be somewhat comparable.
dbxml> setVerbose 1
dbxml> query collection("parts")/part[parent-part]
Query - Finished eager query execution,
time taken = 2399.15ms
300 objects returned for eager expression 'collection("parts")/
part[parent-part]'
Spending 2.4 seconds to examine 3,000 documents and find the matching 300 isn't bad for a file system, but this is a database engine. Indices should improve performance. Berkeley DB XML indices are specified in four parts: path type, node type, key type, and uniqueness. In this case we need to index the node elements to determine if something is present or not, and we won't expect this pattern to be unique. Let's try the query again, but this time with an index to optimize searches for documents containing a parent-part element, a "node-element-presence-none" index.
dbxml> addIndex "" parent-part node-element-presence-none
Adding index type: node-element-presence-none to node:
{}:parent-part
dbxml> query collection("parts")/part[parent-part]
Query - Finished eager query execution,
time taken = 176.925ms
300 objects returned for eager expression 'collection("parts")/
part[parent-part]'
From 2.4 seconds to just under 1/5th of a second seems like a worthwhile improvement. Let's try another query.
dbxml> query 'collection("parts")/part[parent-part = 1]'
Query - Finished eager query execution,
time taken = 215.387ms
100 objects returned for eager expression 'collection("parts")/
part[parent-part = 1]'
The query executed in just over 1/5th of a second without an index. Since this query searches for a specific node's number, a "node-element-equality-decimal" index should help speed things up.
dbxml> addIndex "" parent-part node-element-equality-decimal
Adding index type: node-element-equality-decimal to node: {}:parent-part
dbxml> query 'collection("parts")/part[parent-part = 1]'
Query - Finished eager query execution,
time taken = 79.973ms
100 objects returned for eager expression 'collection("parts")/
part[parent-part = 1]'
Wonderful, query execution time has been reduced by more than half, to under 1/10th of a second by adding that second index. Let's try one final example - the range query we tried earlier.
dbxml> query 'collection("parts")/part[@number >
100 and @number < 105]'
Query - Finished eager query execution,
time taken = 5917.83ms
4 objects returned for eager expression 'collection("parts")/
part[@number > 100 and @number < 105]'
Six seconds is an eternity in database terms, so let's create another and try again.
dbxml> addIndex "" number node-attribute-equality-decimal
Adding index type: node-attribute-equality-decimal to node:
{}:number
dbxml> query 'collection("parts")/part[@number >
100 and @number < 105]'
Query - Finished eager query execution,
time taken = 85.99ms
4 objects returned for eager expression 'collection("parts")/
part[@number > 100 and @number < 105]'
The index reduced the query time back down to less than a 1/10th of a second.
Conclusion
Pick the right tool for the job. An XML-centric design will benefit from a native XML database. Managing XML content can be easy, predictable, flexible, secure, and fast. Save yourself the headaches, time, and expense of solutions that are less suited to the task.
About Gregory BurdGregory Burd is the Product Manager for Sleepycat Software, now a part of Oracle. Prior to Sleepycat, he was on the business team at KnowNow, a Kleiner Perkins startup in the San Francisco Bay Area. He has many years of software development and product leadership within companies such as JavaSoft, a division of Sun Microsystems, Marble Associates, a consulting company, and NeXT Computer, now part of Apple Computer.
About Kimbro StakenKimbro Staken is an independent consultant, author, and open source developer specializing in technologies for XML data management. He is one of the primary developers of the dbXML Core Open Source native XML database and a cofounder of the XML:DB Initiative.