Building XML Hierarchies

XQuery is all about XML, which is based on hierarchy and sequence. If an XQuery is to be fast, the XQuery implementation must handle hierarchy efficiently. Choosing the correct SQL algorithm is important, so our performance testing of DataDirect XQuery® included a variety of algorithms. You can choose any of these algorithms for a given query.

The sort-merge join algorithm is an efficient way to obtain data for building hierarchies in all queries; in many cases it is the fastest, and it has very good worst-case performance, so this is the default algorithm in DataDirect XQuery. For each level of hierarchy, SQL result sets are generated using the same sort order. These result sets are then merged in the XQuery engine, which creates the XML structures that represent the hierarchy.

Example 8. Sort-merge Join

Using the sort-merge algorithm, the inner and outer FLWOR expressions are each translated into a SQL query, sorted by row id, and the results are merged in the XQuery engine.

XQuery

for $u in collection("USERS")/USERS
return
<user name="{$u/LASTNAME}">{
for $h in collection("HOLDINGS")/HOLDINGS
where $h/USERID = $u/USERID
return
<holding id="{$h/STOCKTICKER}">{data($h/SHARES)}</holding>
}</user>

SQL (for users)

SELECT ALL
nrm5."LASTNAME" AS RACOL1,
nrm5."ROWID" AS RACOL2
FROM
"PEPPINO"."USERS" nrm5
ORDER BY
nrm5."ROWID" ASC

SQL (for holdings)

SELECT ALL
nrm5."ROWID" AS RACOL2,
nrm9."STOCKTICKER" AS RACOL3,
nrm9."SHARES" AS RACOL4
FROM
"PEPPINO"."USERS" nrm5,
"PEPPINO"."HOLDINGS" nrm9
WHERE
nrm9."USERID" = nrm5."USERID" AND
LENGTH(nrm9."USERID") = LENGTH(nrm5."USERID")
ORDER BY
nrm5."ROWID" ASC

In some cases, such as when the XML nesting level is limited to four or less, you get better performance using the outer join algorithm, which uses only a single SQL statement. However, when the XML nesting level is greater than four, this single SQL statement can be very complex, so this is not the default algorithm in DataDirect XQuery®. The following query shows how to choose the outer join algorithm.

Example 9. Outer Join

This example shows how to use a declaration option to tell DataDirect XQuery® to use the outer join algorithm when generating SQL for building hierarchies.

XQuery

declare option ddtek:sql-rewrite-algorithm "outer-join";
for $u in collection("USERS")/USERS
return
<user name="{$u/LASTNAME}">{
for $h in collection ("HOLDINGS")/HOLDINGS
where $h/USERID = $u/USERID
return
<holding id="{$h/STOCKTICKER}">{data($h/SHARES)}</holding>
}</user>

Generated SQL

SELECT ALL
RAREL3.RACOL3 AS RACOL3,
RAREL3.RACOL4 AS RACOL4,
RAREL3.RAOJ1 AS RAOJ1,
nrm5."LASTNAME" AS RACOL1,
nrm5."ROWID" AS RACOL2
FROM
{oj
"PEPPINO"."USERS" nrm5
LEFT OUTER JOIN
(SELECT ALL
nrm9."STOCKTICKER" AS RACOL3,
nrm9."SHARES" AS RACOL4,
nrm9."USERID" AS USERID,
2 AS RAOJ1
FROM "PEPPINO"."HOLDINGS" nrm9) RAREL3
ON
RAREL3.USERID = nrm5."USERID" AND
LENGTH (RAREL3.USERID) = LENGTH(nrm5."USERID")} ORDER BY RACOL2 ASC

Try DataDirect XQuery® Free!

Put the power, scalability, and performance of DataDirect XQuery® to work for you today! Our free trial lets you see for yourself how easy it is to build data integration applications that access relational, EDI, and other file formats as XML!

Download DataDirect XQuery® today!

Online Video Tutorials!

Our easy-to-follow online video tutorials are a great way to get acquainted with the many features of DataDirect XQuery®.

And if you like what you see, download a free copy today and try DataDirect XQuery® for yourself!

Back to School with DataDirect XQuery®

Getting your mind around the possibilities of a data integration technology as promising as XQuery can be difficult, but our XML developers Webinars will help you understand the power and versatility of XQuery, and our favorite XQuery processor, DataDirect XQuery®.

New Features in DataDirect XQuery®

DataDirect XQuery® is now released!

DataDirect XQuery® provides full update support for relational data, easy integration for Web Services, additional enhancements for performance and scalability and more!

Download now!

Stay Informed!

XQuery is one of the hottest XML technologies being developed today. Stay informed with vital news about standards, tools, and trends by signing up for the DataDirect XQuery® newsletter.

DataDirect XQuery FAQ

This informative DataDirect XQuery® FAQ answers frequently-asked questions about DataDirect XQuery®, including questions about performance, scalability, use-cases, resources, and more.

If you're more of a hands-on learner, then download a free copy and start exploring DataDirect XQuery® today!