1 / 75

Efficient Querying of XML Data Using Structural Joins

Efficient Querying of XML Data Using Structural Joins. Content. A quick look at XML query languages Lore - an example of a native XML database DB2 - an example of RDBMS’s support for XML On supporting containment queries in RDBMS The Tree-Merge and Stack-Tree algorithms

odele
Download Presentation

Efficient Querying of XML Data Using Structural Joins

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Efficient Querying of XML Data Using Structural Joins

  2. Content • A quick look at XML query languages • Lore - an example of a native XML database • DB2 - an example of RDBMS’s support for XML • On supporting containment queries in RDBMS • The Tree-Merge and Stack-Tree algorithms • The StackPath algorithm

  3. XML • Replacement for HTML • Focus is on storing and processing. • Electronic Data Interchange • Querying becomes desirable. People with many XML documents actually have an XML database.

  4. XML query languages • XML-QL • Influenced by SQL • Submitted to W3C (lost favor to XQuery) • XPath • used in XSLT • the basis for path expressions in XQuery • XQuery • A W3C working draft (version 1.0) • Based on Quilt (which in turn was mainly influenced by XML-QL and Lorel) • No updates, limited IR features

  5. XPath • */para • selects all para grandchildren of the context node • /doc/chapter[5]/section[2] • selects the second section of the fifth chapter of the doc • chapter//para • selects the para element descendants of the chapter element children of the context node • para[@type="warning"] • selects all para children of the context node that have a type attribute with value warning • chapter[title="Introduction"] • selects the chapter children of the context node that have one or more title children with string-value equal to Introduction

  6. XQuery • document("books.xml")//chapter/title • Finds all titles of chapters in document books.xml • document(bib.xml")//book[publisher = "Addison-Wesley”AND @year > "1991"] • Finds all books in document bib.xml published by Addison-Wesley after 1991 • <results> { FOR $t IN distinct(document("prices.xml")/prices/book/title) LET $p := avg(document("prices.xml")/prices/book[title=$t]/price) WHERE (document("bib/xml")/book[title=$t]/publisher) = "Addison-Wesley" RETURN <result> { $t } <avg> { $p } </avg> </result> } </results> • Returns the title and average price of all books published by Addison-Wesley

  7. XML documents as trees book year title authors chapter chapter ... 2000 XML author author head section section ... Bill Jake History head section ... ... <book year=“2000”> <title> XML </title> <authors> <author> Bill </author> <author> Jake </author> </authors> <chapter> <head> History </head> <section> <head> … </head> <section> … </section> </section> <section> … </section> </chapter> <chapter> … </chapter> </book> Order of nodes is important

  8. XML documents as trees <book year=“2000”> <title> XML </title id=“id1”> <authors> <author> Bill </author> <author> Jake </author> </authors> <chapter> <head> History </head> <section> <head> … </head> <section> … </section idref=“id1”> </section> <section> … </section> </chapter> <chapter> … </chapter> </book> Order of nodes is important book year title authors chapter chapter ... 2000 xml author author head section section ... Bill Jake History head section ... ...

  9. Executing queries • How does one execute a complex query: • Parse the query (i.e. break it down to basic operations). • Let a query optimizer devise a corresponding physical query plan. • Execute the required basic operations combining the intermediate results as you go. • The most common basic operations are: • Finding nodes satisfying a given predicate on their value. • Finding nodes satisfying a given structural relationship.

  10. XML databases XML is semi-structured; data items may have missing elements or multiple occurrences of the same element.It may even not have a DTD. • Native semi-structured databases • X-Hive, Lore • RDBMS • Oracle • SQL-Server • DB2 All added support for XML

  11. Semi-structured XML databases • There aren’t many around • Store XML files plus indexes • Usually build (and store) most or all of the tree • Usually solve path expressions by pointer-chasing

  12. LOREAn example of a native semi-structured database

  13. Lore - sample database Select x From DBGroup.Member x Where exists y in x.age: y<30

  14. Lore - data model Called the Object Exchange Model • The data model is a graph (though the reference edges are marked as such). • Each vertex is an object with a unique object identifier. • Atomic objects have no outgoing edges and contain values (like strings, gifs, audio etc.) • All other objects may have outgoing edges. • Tag-Names (labels) are attached to the edges, not the vertices. • Objects may optionally have aliases (names). As is obvious this is just another view of our XML tree

  15. Lore - indexes • Vindex (value index) - implemented as a B+-tree • Supports finding all atomic objects with a given incoming edge label satisfying a given predicate. • Lindex (label index) - implemented using extendible hashing • Supports finding all parents of a given object via an edge with a given label. • Bindex (edge index) • Supports finding all parent-child pairs connected via a given label. This is useful for locating edges with rare labels. • In addition there are some other indexes (not important to us). Note that we need more indexes than in a relational database

  16. Lore - statistics (partial list) • For each labeled path p of length <= k (usually k=1): • The total number of instances of p, denoted |p| • The total number of distinct objects reachable via p,denoted |p|d • The total number of l-labeled edges going out of p,denoted |p l| • The total number of l-labeled edges coming into p,denoted |p l|

  17. Lore - path expressions (simplified) • Simple path expressions • x.l y • Path expressions • an ordered list of simple path expressions • x.l y, y.l2 z • Path expressions logical plan: x.B y, y.C z, z.D v

  18. Lore - basic physical operators(slightly edited) • Scan(father, label, son) • Finds all the sons of a given father (through a given label). Does pointer-chasing • Lindex(father, label, son) • Finds all the fathers of a given son (through a given label). Uses the Lindex • Bindex(label, father, son) • Finds all the father-son pairs connected by a given label. Uses the Bindex • Vindex(label, operator, value, atomic-object) • Finds all the the atomic objects with a given label incoming label satisfying the given predicate. Uses the Vindex • Name(alias, node) • Verifies that the specified node has the given alias.

  19. Lore - physical path subplans x and y are unbound y is bound x and y are unbound • The estimated hit-rate (per x) of scan(x, “C”, y) is: (|B C| / |B|d) • The estimated hit-rate (per y) of Lindex(x, “C”, y) is: (|C B| / |C|d)

  20. Lore - sample logical plan Select x From DBGroup.Member x Where exists y in x.age: y<30 • Glue nodes are pivot points, they recursively evaluate the cost of evaluating their sons in left-right or right-left order.

  21. Lore - sample physical subplans • (a) corresponds to a possible left-right plan of the top “glue” • (b) corresponds to a possible left-right plan of the right “glue” • (c) corresponds to a possible right-left plan of the right “glue” • (d) corresponds to a possible right-left plan of the top “glue”, using (c)

  22. Lore - path expressions strategies A higher level view of path expressions solving • Top-Down • Look for all Member objects in DBGroup and for each one look for Age subobjects with a value < 30. uses scan • Bottom-up • Look for all atomic objects with value < 30 and for each one walk up the tree using only Age-labeled followed by Member-labeled edges. uses Vindex and then Lindex • Hybrid • Do Top-Down part of the way and Bottom-Up part of the way. Select x From DBGroup.Member x Where exists y in x.age: y<30

  23. Lore - path strategies (continued) • Top-Down is better when there are few paths satisfying the required structure, but many objects satisfying the predicate. • Bottom-Up is better when there are a few objects satisfying the predicate but many paths satisfying the required structure. • Hybrid is better when the fan-out degree (going down), increases at the same time the fan-in degree (going up) does.

  24. DB2An example of a RDBMS support of XML

  25. DB2 - XML support • XML column • An entire XML document is stored as a column in a table. • may be XMLCLOB, XMLVARCHAR or XMLFile. • You define which XML elements or attributes should be extracted to indexed columns in side tables. • UDF’s are provided for inserting, updating and selecting fragments of a document. • XML collection • Compose an XML document from existing DB2 tables. • Decompose an XML document and retrieve some of it into a set of DB2 tables. • Basically a conversion mechanism. • Stored procedures automate most of the work.

  26. DB2 - a nice diagram...

  27. DB2 - example Data Access Definition

  28. DB2 - example DAD(continued)

  29. DB2 - searching XML documents • Well, whatever is in the side tables is queried using SQL. • What about things not in any side table? • A loosely coupled IR engine (part of the DB2 Text Extender) is called using a UDF to take care of this. • The UDF’s use a syntax compatible with XPath.

  30. DB2 - conclusions (in a nutshell) • Pros • Integrated solution which automates a lot of work. • We can ask queries that mix data from XML and the regular database tables (aka “web-supported database queries” and “database-supported web queries”). • Cons • One has to manually define the mappings between the XML documents and the tables. • Is it fast enough?

  31. On Supporting Containment Queries in RDBMS Zhang, Naughton, DeWitt, Luo, Lohman ACM SIGMOD 2001

  32. Article goals • Given that a lot of XML data is (and will probably be) stored in RDBMS which is the best way to support containment queries? • Using a loosely coupled IR engine? OR • Using the native tables and query mechanisms of the RDBMS?

  33. Structural relationships in trees book book pre-order post-order year year title title authors authors chapter chapter 2000 2000 XML XML author author author author head head section section Bill Bill Jake Jake History History head head Note that x is a descendant of y if and only if:preorder(x) > preorder(y) and postorder(x) < postorder(y) y is the father of x if in addition: level(x) = level(y) + 1 1 15 2 4 6 11 2 4 9 14 3 5 7 9 12 14 1 3 6 8 11 13 8 10 13 15 5 7 10 12

  34. Structural relationships in XML • The previous observations are true even if we look at any monotone functions of the preorder and the postorder numbers. • The start and end position of an element in an XML document are exactly such monotone functions. In other words we can use a small extension of the regular IR inverted-index to also solve structural relationships! Note that we have a problem of adapting the numbers if the document changes.

  35. The inverted indexes • An Elements index (E-Index): Holds for each XML element, the docno, begin, end and level of every occurrence of that element. • A Text index (T-Index): Holds for each text word, the docno, wordno and level of every occurrence of the word.

  36. Experiment plan Compare the following two systems: • An inverted list engine supporting containment queries on XML data. • The engine was built (due to lack of a commercial one). • The code was written in C++ and the inverted-indexes were stored in a B+-tree with each list stored as a record. • Each list is in ascending order of docno, begin (or wordno). • An in-house algorithm was developed for evaluating simple containment queries. • A full RDBMS approach (tried DB2 7.1 and SQL-Server 7.0) • The E-index and T-index are stored as the following tables: ELEMENTS(term, docno, begin, end, level)TEXTS(term, docno, wordno, level) Note that we do not use the IR engine of the RDBMS.

  37. Using the inverted indexes tables x y x y y x E//"T” select * from ELEMENTS e, TEXTS t where e.term = ’E’ and t.term = ’T’ and e.docno = t.docno and e.begin < t.wordno and t.wordno < e.end E="T" select * from ELEMENTS e, TEXTS t where e.term = ’E’ and t.term = ’T’ and e.docno = t.docno and e.begin + 1 = t.wordno and t.wordno + 1 = e.end In a similar fashion we solve Elements only queries, father-son, and words distance queries. (how will this look for E//E ?)

  38. Experiment setup The data sets:

  39. Experiment setup (continued) The queries are all simple queries of the form: E//T, E//E, E/T or E/E

  40. Experiment results

  41. Results analysis • Why did DB2 perform better in QS4, QD4 and QG5? Remember that each list in the inverted engine is stored as one record! • Why did DB2 perform worse in all the other queries? • Bad optimizer decisions? • Is I/O more expensive (locking, security, etc.)? • Other factors? • It turns out that the queries are CPU-bound! Further investigation found out that it was the merge algorithm.

  42. DB2 merge algorithms When joining on: a.docno = d.docno and a.begin < d.wordno and d.wordno < a.end • Standard Merge-Join only uses the a.docno = d.docno predicate (since it does one comparison, using one index per table), and applies the rest of the condition on each matching couple. • Hash-Join only uses the a.docno = d.docno predicate (since it can not handle inequalities anyway), and thus performs similarly to the classical merge join. • Index nested-loop join looks, for each row in the outer table, for all rows in the inner table index that lie between a start-key and astop-key. Assuming the outer table is ELEMENTS and the inner table is TEXTS: • start-key: term = value and docno = outer.docno and wordno > outer.begin • end-key: term = value and docno = outer.docno and wordno < outer.end

  43. The Multi-Predicate Merge Join Alist Dlist doc begin end 5 7 20 5 14 19 5 21 28 5 22 27 5 29 31 5 32 40 doc begin 5 2 5 23 5 24 5 33 5 37 5 42 begin-desc = Dlist->first node; OutputList = NULL; for (a = Alist->firstNode; ; a = a->nextNode) { d = begin_desc; while (d.docno < a.docno) d = d->nextNode; if (a.docno < b.docno) continue; while (d.begin <= a.begin) d = d->nextNode; begin_desc = d; while (d.begin < a.end) { // implies d.end < a.end if (a.docno < b.docno) break; append (a,d) to OutputList; d = d->nextNode; } }

  44. Comparison of the merge algorithms It seems like the NLJ algorithm will usually compare less items, BUT • It has to spend time on index seeks! • It uses random access so cache utilization is poor.

  45. MPMGJN & traditional joins - statistics Note: DB2 did not choose NLJ for QG4

  46. Structural Joins: A Primitive for Efficient XML Query Pattern Matching Al-Khalifa, Jagadish, Koudas, Patel, Srivastava, Wu ICDE 2002

  47. Structural-Join algorithms • Tree-Merge-Anc (aka MPMGJN) • Tree-Merge-Desc • Stack-Tree-Desc • Stack-Tree-Anc The ?-?-Anc algorithms produce the output sorted by the ancestors. The ?-?-Desc algorithms produce the output sorted by the descendants. The sorting variant to use depends on the way an optimizer chooses to compose a complex query. Based on the (docId, startPos, endPos, level) information of XML elements and attributes. Given two lists of potential ancestors and potential descendants, both in ascending order of docId+startPos, the following structural join algorithms are presented:

  48. Tree-Merge-Anc begin-desc = Dlist->first node; OutputList = NULL; for (a = Alist->firstNode; ; a = a->nextNode) { d = begin_desc; while (d.startPos <= a.startPos) d = d->nextNode; begin_desc = d; while (d.startPos < a.endPos) { // implies d.endPos < a.endPos if (a.level +1 != d.level) continue; // father-son append (a,d) to OutputList; d = d->nextNode; } } Note: For ease of exposition, we assume that Alist and Dlist have the same docId.

  49. Analysis of Tree-Merge-Anc Alist Dlist a1 a2 begin end a1 1 4n a2 2 4n-1 a3 3 4n-2 . . an n 3n+1 begin d1 n+1 d2 n+3 d3 n+5 . . dn 3n-1 a3 ... an d1 d2 d3 dn ... • Ancestor-Descendant structural relationships: • O(|Alist| + |Dlist| + |OutputList|) • Since first while loop increases d, and second while loop increases output or a. • Father-Son structural relationships: • O(|Alist| * |Dlist|) Can sub-sorting on levelNum help ?

  50. Tree-Merge-Desc begin-anc = Alist->first node; OutputList = NULL; for (d = Dlist->firstNode; ; d = d->nextNode) { a = begin_anc; while (a.endPos <= d.startPos) a = a->nextNode; begin_anc = a; while (a.startPos < d.startPos) { if (a.level +1 != d.level) continue; // father-son if (d.endPos < a.endPos) append (a,d) to OutputList; a = a->nextNode; } } Note: For ease of exposition, we assume that Alist and Dlist have the same docId.

More Related