Presentation is loading. Please wait.

Presentation is loading. Please wait.

Efficient Querying of XML Data Using Structural Joins.

Similar presentations


Presentation on theme: "Efficient Querying of XML Data Using Structural Joins."— Presentation transcript:

1

2 Efficient Querying of XML Data Using Structural Joins

3 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

4 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.

5 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

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

7 XQuery document("books.xml")//chapter/title –Finds all titles of chapters in document books.xml document(bib.xml")//book[publisher = "Addison-Wesley” > "1991"] –Finds all books in document bib.xml published by Addison- Wesley after 1991 { 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 { $t } { $p } } –Returns the title and average price of all books published by Addison-Wesley

8 XML documents as trees XML Bill Jake History … … … Order of nodes is important book yeartitleauthorschapter 2000XMLauthorheadsection BillJake author Historyheadsection...

9 XML documents as trees XML Bill Jake History … … … Order of nodes is important book yearauthorschapter 2000xmlauthorheadsection BillJake author Historyhead... title section

10 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.

11 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

12 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

13 LORE An example of a native semi-structured database

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

15 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

16 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

17 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 |

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

19 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.

20 Lore - physical path subplans x and y are unboundy is boundx 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 )

21 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.

22 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)

23 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

24 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.

25 DB2 An example of a RDBMS support of XML

26 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.

27 DB2 - a nice diagram...

28 DB2 - example Data Access Definition

29 DB2 - example DAD (continued)

30 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.

31 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?

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

33 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?

34 Structural relationships in trees book yeartitleauthorschapter 2000XMLauthorheadsection BillJake author Historyhead pre-order book yeartitleauthorschapter 2000XMLauthorheadsection BillJake author Historyhead post-order 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

35 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.

36 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.

37 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.

38 Using the inverted indexes tables 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 ?) x y x y x y

39 Experiment setup The data sets:

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

41 Experiment results

42 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.

43 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 a stop-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

44 The Multi-Predicate Merge Join begin-desc = Dlist->first node; OutputList = NULL; for (a = Alist->firstNode; ; a = a->nextNode) { d = begin_desc; while (d.docno nextNode; if (a.docno < b.docno) continue; while (d.begin 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; } doc begin end doc begin AlistDlist

45 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.

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

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

48 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:

49 Tree-Merge-Anc begin-desc = Dlist->first node; OutputList = NULL; for (a = Alist->firstNode; ; a = a->nextNode) { d = begin_desc; while (d.startPos 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.

50 Analysis of Tree-Merge-Anc 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|) a1 a2 a3 an d3d1d2dn 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 AlistDlist Can sub-sorting on levelNum help ?

51 Tree-Merge-Desc begin-anc = Alist->first node; OutputList = NULL; for (d = Dlist->firstNode; ; d = d->nextNode) { a = begin_anc; while (a.endPos 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.

52 Analysis of Tree-Merge-Desc Ancestor-Descendant and Father-Son structural relationships: –O(|Alist| * |Dlist|). –Works in linear time on most real data.... begin end a0 1 4n+2 a1 2 5 a2 6 9 a an 4n-2 4n+1 begin d1 3 d2 7 d3 11. dn 4n-1 DlistAlist a0 a3a1a2an d1d2d3dn...

53 Stack-Tree algorithms Motivation –A depth-first traversal of a tree can be performed in linear time, using a stack as large as the height of the tree. –An ancestor-descendant structural relationship is manifested as the ancestor appearing higher on the stack than the descendant. –Unfortunately, a depth-first traversal requires going over all the tree.

54 Stack-Tree-Desc a = Alist->first node; d = Dlist->first node; OutputList = NULL; while (lists are not empty) { e = (a.startPos < d.startPos) ? a : d; while (e.startPos > stack->top.endPos) stack->pop(); if (e == a) { // remember that e.startPos > stack->top.startPos stack->push(a); a = a->nextNode; } else // e == d for each a’ in stack { // Father-Son: If (stack->top.level + 1 = d.level) append(stack->top, d) append (a’, d) to OutputList; } d = d->nextNode; } Note: For ease of exposition, we assume that Alist and Dlist have the same docId.

55 Stack-Tree-Desc (father-son example) a1 d1 a2 d2.. an dn dn+1 dn+2. d2n d1d2n d2d2n-1 d3d2n-2 dndn+1 a1 a2 a3 an a1 (a1,d1) a2 (a2,d2) (an-1,dn-1) an (an,dn)(an,dn+1)(an-1,dn+2)...(a3,d2n-2)(a2,d2n-1)(a1,d2n) ? e.startPos > stack->top.endPos......

56 Analysis of Stack-Tree-Dec O(|Alist| + |Dlist| + |OutputList|) for ancestor-descendant as well as father-son structural relationships. –Each Alist element is pushed once and popped once, so stack operations take O(|Alist|). –The inner “for loop” outputs a new pair each time, so its total time is O(|OutputList|). –When doing father-son structural joins, we do not even have a “for loop”. The algorithm is non-blocking. IO complexity is O(|Alist|/P + |Dlist|/P + |OutputList|/P) where P is the page size. –Each input page is read just once (and output sent as soon as it is computed). –The stack is as large as the tree height, so it is very reasonable to assume that it fits in RAM.

57 Stack-Tree-Anc a = Alist->first node; d = Dlist->first node; OutputList = NULL; while (lists are not empty) { e = (a.startPos < d.startPos) ? a : d; while (e.startPos > stack->top.endPos) { temp = stack->pop(); if (stack->isEmpty()) { append temp->selfList to OutputList; append temp->inheritList to OutputList; } else { append temp->inheritList to temp->selfList; append temp->selfList to stack->top->inheritList; } if (e == a) { // remember that e.startPos > stack->top.startPos stack->push(a); a = a->nextNode; } else { // e == d for each a’ in stack { if(a’ == stack->bottom) append (a’, d) to OutputList; else append (a’, d) to selfList associated with a’ } d = d->nextNode; } } if (!stack->isEmpty()) flush the stack held lists to the output Note: For ease of exposition, we assume that Alist and Dlist have the same docId.

58 Stack-Tree-Anc (father-son example) a1 d1 a2 d2.. an dn dn+1 dn+2. d2n a1 (a1,d1) a2 (a2,d2) (an-1,dn-1) an (an,dn)(an,dn+1) (a3,d3),(a3,d2n-2)...(an,dn),(an,dn+1) (a2,d2n-1) (a1,d2n) d1d2n d2d2n-1 d3d2n-2 dndn+1 a1 a2 a3 an (an,dn), (an,dn+1) (a2,d2),(a2,d2n-1)...(an,dn),(an,dn+1) ? e.startPos > stack->top.endPos

59 Analysis of Stack-Tree-Anc O(|Alist| + |Dlist| + |OutputList|) For ancestor-descendant as well as father-son structural relationships. –Assuming the lists are maintained as linked lists with head and tail pointers. The algorithm is blocking (but only partially). IO complexity is O(|Alist|/P + |Dlist|/P + |OutputList|/P) where P is the page size. –We cannot assume that all the lists fit in RAM. –All that we do with lists (except output) is appending. –We can page out a list and we need only keep its tail in RAM. So we need two extra pages in memory per stack entry - still a reasonable assumption. –We only need to know the address of the head of a list. –Each list page is thus paged out at most once, and paged back in only for output.

60 Experiment workload Experimented with real XML data as well as synthetic data generated by IBM XML data generator (with similar results). Presented the results for the largest data set: 6.3 million elements (800Mb of data).

61 Experiment results Implemented the structural join algorithms, as well as bottom-up and top-down, on the TIMBER native XML query engine (built on top of SHORE). Bottom-up and top-down performed poorly: –Even on 10% of the data it took bottom-up seconds to run QS1, and seconds for top-down to do it. –It took less than 15 seconds for any of the join algorithms to complete QS1 on the full data set!

62 Experiment results (continued) Implemented the STJ-D as an application program interfacing to a commercial RDBMS through a set of cursors. Also ran the queries using the RDBMS join mechanisms. QS1: Combined: an index on startPos, endPos Small: up to 10% selectivity, Medium: up to 25%

63 Experiment results (continued)

64 Holistic Twig Joins: Optimal XML Pattern Matching Bruno, Koudas, Srivastava ACM SIGMOD 2002

65 Twig patterns book[title = “XML” AND year = 2000] book[title = “XML”]//author[Fn = “jane” AND Ln = “doe”] book year title authorschapter 2000 XMLauthor head section Lntitlesection... Fn jane doe author LnFn john moe author LnFn johndoe... XML... title XML year 2000 book title XML author Ln book janedoe Fn Twig patterns

66 Twig pattern matching Given a twig pattern Q and an XML database D, a match is a mapping from nodes in Q to nodes in D, satisfying: –Query node predicates are satisfied by their images. –The structural relationships between the query nodes are satisfied by their images. If Q has k nodes, the result may be represented by a relation with k columns.

67 Twig pattern matching approaches Decompose the twig into a series of binary structural joins, compute each (using STJ-D for example) and join the results. –Note that one may have intermediate results that are very big. Consider for example: book[title = “XML” ] Decompose the twig into a series of rooted path-expressions, compute each one independently and merge-join the results. –Note that one may have intermediate results that are very big (but only in different branches). Consider for example: book//author[Fn = “jane” AND Ln = “doe”] Decompose the twig into a series of rooted path-expressions, compute them simultaneously taking interdependencies into account, and merge-join the results.

68 PathStack-Desc go to start of all lists; OutputList = NULL; while (lists are not empty) { e = element with minimum startPos in all lists; i = the list e was taken from; advance list i; for(int j=1; j < numLists; j++) { while (e.startPos > stack j ->top.endPos) stack j ->pop(); } if (e is not from the leaf list) { // remember that for every stack e.startPos > stack->top.startPos stack i ->push(a, &stack i-1 ->top); // if the I-1 stack is not empty of course } else { // e is the path query leaf let (x 1, x 2, … x numLists-1 ) be the linked list whose head is the top of the numLists-1 stack. For each (y 1, y 2, … y numLists-1, e) such that for all j y j is below x j do: append (y 1, y 2, … y numLists-1, e) to OutputList ; } } Note: For ease of exposition, we assume that all lists have the same docId.

69 PathStack-Desc (example) a1 b1 a2 b2 c1 b3 c2 ? e.startPos > stack->top.endPos a1 b1 a2 b2 c1 b3 c2 b c a a1b1 a2b2 (a2,b2,c1) (a1,b2,c1) (a1,b1,c1)(a1,b3,c2) b3

70 PathStack-Desc experimental results Implemented the binary join algorithms, as well as the StackPath, in C++ using the file-system as the storage engine. Used a synthetic data set made of 1 million nodes with 6 different labels (A1, A2, …A6) uniformly distributed (no information regarding other parameters).

71 Final remarks What we did not do (partial list): –Look at using B+-Trees with the stack algorithms. –Look at the TwigStack algorithm. –Look at Kleen-closure evaluation. Conclusions: –There is a lot more work to be done by everybody.

72 Appendix: TwigStack (in a nutshell) getNext(q) returns a query node such that the head of its list satisfies: –It has the smallest startPos (L) of all the heads of its descendant and sibling lists. –It participates in a solution to the sub-query rooted at that query node. –If it is part of a solution involving its ancestors they were already read.

73 Appendix (continued) Note that as long as 09 succeeds we return the node whose head has the smallest startPos (L) of all the heads of lists in the sub-tree of q. When 09 fails we “float up” a node whose list head has the the smallest startPos (L) of all the heads of lists in its descendant or sibling lists. Once a node floats up, its father node’s list does not contain any more ancestors of its list head (otherwise 09 would not fail). Applying the same logic to the father and grandfather etc. leads us by induction to the conclusion that if it this node’s list head is part of a solution involving its ancestors, these ancestors are already out of their lists.

74 Appendix (continued) Both used ternary trees. –Left sub-tree in (a) has only A1=A2=A3=A4 paths. –Middle sub-tree in (a) has only A1=A5=A6=A7 paths. –Right sub-tree in (a) has solutions. Its size varies (8% to 24% of the tree). –(b): left has no A2 or A3, middle has no A4 or A5, right has no A6 or A7.

75 Bibliography Shu-Yao Chien, Zografoula Vagena, Donghui Zhang, Vassilis J. Tsotras, Carlo Zaniolo, “Efficient Structural Joins on Indexed XML Documents” Proc.of VLDB 2002 Shurug Al-Khalifa, H. V. Jagadish, Nick Koudas, Jingesh M. Patel, Divesh Srivastava, Yuqing Wu, “Structural Joins: A Primitive for Efficient XML Query Pattern Matching”, ICDE 2002 Nicolas Bruno, Nick Koudas, Divesh Srivastava, “Holistic Twig Joins: Optimal XML Pattern Matching”, ACM SIGMOD 2002 Shu-Yao Chien, Vassilis J. Tsotras, Carlo Zaniolo, Donghui Zhang, “Efficient Complex Query Support for Multiversion XML Documents”, Proc. of VLDB 2001 Jason McHugh, Jennifer Widom, “Query Optimization for XML”, Proc. of VLDB 1999 Chun Zhang, Jeffrey Naughton, David DeWitt, Qiong Luo, Guy Lohman, “On Supporting Containment Queries in Relational Database Management Systems”, ACM SIGMOD 2001 Quanzhong Li, Bongki Moon, “Indexing and Querying XML Data for Regular Path Expressions”, Proc. of VLDB 2001 IBM DB2 web site: site (on XPath and XQuery)

76


Download ppt "Efficient Querying of XML Data Using Structural Joins."

Similar presentations


Ads by Google