# Patent

## Title
Interleaving query transformations for XML indexes

## Abstract

During query optimization, XML index and view merge transformations may be interleaved.

## Background
RELATED APPLICATIONS
This application is related to U.S. Pat. No. 6,427,123, entitled Hierarchical Indexing For Accessing Hierarchically Organized Information In A Relational System, filed on Feb. 18, 1999, the contents of which are herein incorporated by reference in their entirety.
This application is related to U.S. Pat. No. 7,051,033, entitled Providing A Consistent Hierarchical Abstraction Of Relational Data, filed on Sep. 27, 2002, the contents of which are herein incorporated by reference.
This application is related to U.S. patent application Ser. No. 10/260,381, entitled Mechanism To Efficiently Index Structured Data That Provides Hierarchical Access In A Relational Database System, filed on Sep. 27, 2002, the contents of which are herein incorporated by reference.
This application is related to U.S. patent application Ser. No. 10/884,311, entitled Index For Accessing XML Data, filed on Jul. 2, 2004, the contents of which are herein incorporated by reference in their entirety.
This application is related to U.S. application Ser. No. 10/944,170 entitled Efficient Query Processing of XML Data Using XML Index filed on Sep. 16, 2004, the contents of which are herein incorporated by reference.
FIELD OF THE INVENTION
The present invention relates to database systems, and in particular, to optimization of queries executed by a database system.
BACKGROUND
Relational and object-relational database management systems (hereafter “relational DBMS”) store information in tables of rows in a database. To retrieve data, queries that request data are submitted to a database server, which computes the queries and returns the data requested.
Query statements submitted to the database server should conform to the syntactical rules of a particular query language. One popular query language, known as the Structured Query Language (SQL), provides users a variety of ways to specify information to be retrieved.
Relational DBMSs may be enhanced to store XML data and are able to handle queries that use XML semantics to specify database operations, using XML query languages, such as XQuery and XPath. XML Query Language (“XQuery”) and XML Path Language (“XPath”) are important standards for a query language, and can be used in conjunction with SQL to express a large variety of useful queries. The term XML query is used to refer to queries that conform to SQL and/or XQuery, XPath, and/or another XML language standard, including proprietary dialects of XQuery, XPath, SQL, or XML standard languages. XML queries include SQL queries that embed XQuery and XPath expressions, XQuery queries that are not embedded within an SQL statement. When a relational DBMS receives a XML query, the database server may perform a XML rewrite to refer to the underlying base tables and database objects that store the XML data.
Query Optimizer
A query submitted to a database server is evaluated by a query optimizer. Based on the evaluation, the query optimizer generates an execution plan that defines operations for executing the query. Typically, the query optimizer generates an execution plan optimized for efficient execution. When a query optimizer evaluates a query, it determines various candidate execution plans” and selects an optimal execution plan.
During query optimization, different kinds of query transformations are performed. The query may be transformed into one or more semantically equivalent queries. A query is semantically equivalent to another when the queries request (or declare) the same results; computation of either should return the same result. A query may be computed more efficiently once transformed.
There are various kinds of transformations. For example, view merging is a type of transformation in which a subquery within the FROM clause (“inline view”) of the “outer query” is removed and is merged into the outer query to produce a semantically equivalent query.
The term kind or type of transformation, as used herein, refers transformations that are rewritten in a particular way or using specific types of rewrite operations.
Another type of rewrite is an XML rewrite. Typically, XML data is stored in underlying relational structures referred to as base structures. An XML rewrite rewrites expressions based on XML semantics (e.g. XPath and XQuery) into an expression that references the base structures.
A type of XML rewrite is an XML index rewrite. An XML index rewrite rewrites an XML query to refer to one or more structures of an XML index. An XML index is a “logical index” which indexes a collection of XML documents. A logical index contains multiple structures that are cooperatively used to access a collection of XML documents. A logical index includes a path table, which contains information about the hierarchies of nodes in a collection of XML documents and may contain the node values of the nodes. Among the columns or attributes of the path table is a column that stores the values of nodes, a pathid column that stores the path (in the form of a path id) of nodes, and a dewey_key column, which contains order keys that represent a hierarchical position of a node in a document, e.g. the order key value 3.21.5 specifies 5th child of 21st child of 3rd child of root.
An example of a XML index is described in Index For Accessing XML Data.
Order of Transformations
When determining how to optimize a query, many transformations and/or combinations of transforms can be applied. Applying a transformation consumes computer resources; doing this for all or even a proportion of all combinations of query transformations of a query may create a cost that is significant compared to the cost of computing the original query, if not more. To reduce costs of transforming queries, different types of transformations are applied in a predetermined order or sequence, referred to herein as a transformation sequence. The order in which the types of transformation are applied in a transformation sequence may be based on the way the kinds of transformations interact.
For example, an XML rewrite may create in an inline view, which can be merged using view merging. Thus, in a transformation sequence, an XML rewrite of a query is applied before view merging so that the view merge merges the inline view generated by the XML rewrite.
In general, a query optimizer generates optimized execution plans when the query optimizer is able to perform more kinds of transformations under more kinds of conditions.
The approaches described in this section are approaches that could be pursued, but not necessarily approaches that have been previously conceived or pursued. Therefore, unless otherwise indicated, it should not be assumed that any of the approaches described in this section qualify as prior art merely by virtue of their inclusion in this section.



BRIEF DESCRIPTION OF THE DRAWINGS
The present invention is illustrated by way of example, and not by way of limitation, in the figures of the accompanying drawings and in which like reference numerals refer to similar elements and in which:
FIG. 1 is a diagram of computer system that may be used in an implementation of an embodiment of the present invention.



DETAILED DESCRIPTION
In the following description, for the purposes of explanation, numerous specific details are set forth in order to provide a thorough understanding of the present invention. It will be apparent, however, that the present invention may be practiced without these specific details. In other instances, well-known structures and devices are shown in block diagram form in order to avoid unnecessarily obscuring the present invention.
During query optimization, view merge transformation and XML index rewrite are interleaved. Types of transformations are interleaved when one of the types is performed and then performed again after performing the other of the types.
For example, a view merge and XML index rewrite are interleaved when a view merge is applied, followed by applying an XML index rewrite to the query, and then followed by applying a view merge. Each query transformation may yield a transformed query that is even more efficient to compute.
At least two query transformations may be described herein as both being applied to a query. This is a convenient way of expressing that a first of the at least two transformations is applied to a query to generate a first transformed query, and then the second of the at least two transformations is applied to the first transformed query to generate a second transformed query. In fact, one or more transformations may be applied between the first and second transformations.
For example, when a view merge and an XML index rewrite are described as being applied to a query, the view merge is applied to generate a first transformed query, which may be further transformed to produce one or more subsequent transformed queries, and then an XML index rewrite is applied to the first transformed query or the last of the one of the one or more subsequent queries.
Techniques for interleaving XML index rewrite and view merge transformations are illustrated in the context of a repository of XML data, described below.
Illustrative Operating Environment
Queries are submitted to and rewritten by a relational DBMS that can function as a repository of XML documents. The repository stores and manages access to resources, including in particular, XML documents. Similar to a hierarchical file system, XML documents in a repository are organized according to a hierarchy referred to herein as a repository hierarchy. Each XML document may be located, identified, or addressed by tracing a path, referred to herein as a repository path, through the hierarchy to the XML document. For a given XML document, a repository path begins at a root directory and proceeds down a hierarchy of directories to eventually arrive at the directory that contains the XML document.
Within the repository, each XML document may be represented by and stored/contained within a row of an object-relational table referred to herein as a repository table. For example, a repository table may contain a column of the type XMLType. For a given row in the repository table, a representation of an XML document is stored in the column.
A XML repository provides access to XML data in the repository by responding to XML queries. Such queries are optimized by a query optimizer, which may transform the queries as described below.
Illustration of Interleaving
An approach for interleaving view merge and XML index rewrite are illustrated using the following query QI.














QI=



 select extract(value(T), ‘/c/d’)



 from xmltable(‘for $doc in



   ora:view(“/public/bal/asha”)/a/b return $doc’) T;









For purposes of exposition, queries that are used to illustrate rewrites of queries, such as query QI, are rewritten in a form that may not completely conform to standard or proprietary forms of SQL/XML, and XPATH, and XQUERY, and may omit expressions whose presence in the illustration are not needed for understanding an embodiment of the present invention. Query expressions that deviate from language syntax shall be explained and/or are apparent to those of ordinary skill in the art.
Query QI is an XML query that contains an inline view in the form of an xmltable operator in the from clause. The argument of the xmltable operator is a XQuery query expression. The query expression refers to the XQuery function ora:view, which is supported by Oracle Corporation. The view expression ora: view (“/public /bal/asha/”) /a/b specifies to return as a sequence of items, the nodes identified by the XPath expression /a/b within a collection of XML documents, where the collection is stored within the repository path/public/bal/asha/. In the repository, each of the XML documents is contained in a row of table resource_table in association with a resource path. The xmltable operator returns, for each of the items in the sequence returned for ora:view, a row having an XMLType column that contains an item in the sequence.
Rewrite View
The query optimizer next rewrites the inline view in QI, resulting in query QI1, as follows.














QI1=



 select extract(value(T), ‘/c/d’)



 from table(select extract(value(T1), ‘/a/b’)



      from resource_table T1



      where equals_path(T1, ‘/public/bal/asha’)=1...)



   T









The xmltable operator, with its XQuery expression argument, are replaced with a table operator and a subquery argument, the subquery being select extract(value(T1), ‘/a/b’) from resource_table T1 where equals_path(T1, ‘/public/bal/asha’)=1 . . . . The subquery in effect replaces the ora:view expression. This transformation may be referred to either as a XQuery transformation because an XQuery expression has been transformed or as a view conversion because a view expression has been replaced by an equivalent expression.
View Merge
In the next transformation, a view merge is performed to yield query QI2, as follows.














QI2=



 select extract(value(T), ‘/c/d’)



 from resource_table T1, table(select extract(value(T1),



  ‘/a/b’)



              from T1) T



 where equals_path(T1, ‘/public/bal/asha’)=1









Under a transformation sequence, in which an XML index rewrite must be performed before a view merge, an XML index rewrite would not have been applied to QI2. However, when XML rewrite and view merge are interleaved, an XML index rewrite may follow the view merge, as shown below, to yield a possibly more efficient to compute transformed query. This transformed query is even further optimized by applying yet another view merge, as follows.
XML Rewrite and View Merge
In the next transformations, an XML index rewrite is performed followed by a view merge. Applying an XML index rewrite to QI2 yields QI3, as follows.

















QI3=




 select extract(value(T), ‘/c/d’)




 from resource_table T1, table(select *




               from index_path_table T2




               where T2.pathid = ‘/a/b’...)




   T




 where equals_path(T1, ‘/public/bal/asha’)=1... .










The XML index rewrite replaces the subquery table argument of QI2 with a subquery of table index_path_table, the path table of the XML index. As the table operator is an inline view, there is another opportunity for further optimization by applying a view merge. Applying a view merge to query QI3 yields query QI4, as follows.














QI4=



 select extract(make-node(T2), ‘/c/d’)



 from repos_table T1, index_path_table T2



 where equals_path(T1, ‘/public/bal/asha’)=1 and



  T2.pathid = ‘/a/b’............... .









Finally, yet another XML index rewrite can be applied to QI4 to yield a possibly more efficient query QI5, as follows.














QI5= select make-node(T3)



  from repos_table T1, index_path_table T2,



  index_path_table T3



  where equals_path(T1, ‘/public/bal/asha’)=1 and



    T2.pathid = ‘/a/b’ ............... and



    T2.dewey_key < T3.dewey_key <



  maxchild(T2.dewey_key)



    T3.pathid like ‘%/c/d’...









Hardware Overview

FIG. 1 is a block diagram that illustrates a computer system 100 upon which an embodiment of the invention may be implemented. Computer system 100 includes a bus 102 or other communication mechanism for communicating information, and a processor 104 coupled with bus 102 for processing information. Computer system 100 also includes a main memory 106, such as a random access memory (RAM) or other dynamic storage device, coupled to bus 102 for storing information and instructions to be executed by processor 104. Main memory 106 also may be used for storing temporary variables or other intermediate information during execution of instructions to be executed by processor 104. Computer system 100 further includes a read only memory (ROM) 108 or other static storage device coupled to bus 102 for storing static information and instructions for processor 104. A storage device 110, such as a magnetic disk or optical disk, is provided and coupled to bus 102 for storing information and instructions.
Computer system 100 may be coupled via bus 102 to a display 112, such as a cathode ray tube (CRT), for displaying information to a computer user. An input device 114, including alphanumeric and other keys, is coupled to bus 102 for communicating information and command selections to processor 104. Another type of user input device is cursor control 116, such as a mouse, a trackball, or cursor direction keys for communicating direction information and command selections to processor 104 and for controlling cursor movement on display 112. This input device typically has two degrees of freedom in two axes, a first axis (e.g., x) and a second axis (e.g., y), that allows the device to specify positions in a plane.
The invention is related to the use of computer system 100 for implementing the techniques described herein. According to one embodiment of the invention, those techniques are performed by computer system 100 in response to processor 104 executing one or more sequences of one or more instructions contained in main memory 106. Such instructions may be read into main memory 106 from another machine-readable medium, such as storage device 110. Execution of the sequences of instructions contained in main memory 106 causes processor 104 to perform the process steps described herein. In alternative embodiments, hard-wired circuitry may be used in place of or in combination with software instructions to implement the invention. Thus, embodiments of the invention are not limited to any specific combination of hardware circuitry and software.
The term “machine-readable medium” as used herein refers to any medium that participates in providing data that causes a machine to operation in a specific fashion. In an embodiment implemented using computer system 100, various machine-readable media are involved, for example, in providing instructions to processor 104 for execution. Such a medium may take many forms, including but not limited to, non-volatile media, volatile media, and transmission media. Non-volatile media includes, for example, optical or magnetic disks, such as storage device 110. Volatile media includes dynamic memory, such as main memory 106. Transmission media includes coaxial cables, copper wire and fiber optics, including the wires that comprise bus 102. Transmission media can also take the form of acoustic or light waves, such as those generated during radio-wave and infra-red data communications. All such media must be tangible to enable the instructions carried by the media to be detected by a physical mechanism that reads the instructions into a machine.
Common forms of machine-readable media include, for example, a floppy disk, a flexible disk, hard disk, magnetic tape, or any other magnetic medium, a CD-ROM, any other optical medium, punchcards, papertape, any other physical medium with patterns of holes, a RAM, a PROM, and EPROM, a FLASH-EPROM, any other memory chip or cartridge, a carrier wave as described hereinafter, or any other medium from which a computer can read.
Various forms of machine-readable media may be involved in carrying one or more sequences of one or more instructions to processor 104 for execution. For example, the instructions may initially be carried on a magnetic disk of a remote computer. The remote computer can load the instructions into its dynamic memory and send the instructions over a telephone line using a modem. A modem local to computer system 100 can receive the data on the telephone line and use an infra-red transmitter to convert the data to an infra-red signal. An infra-red detector can receive the data carried in the infra-red signal and appropriate circuitry can place the data on bus 102. Bus 102 carries the data to main memory 106, from which processor 104 retrieves and executes the instructions. The instructions received by main memory 106 may optionally be stored on storage device 110 either before or after execution by processor 104.
Computer system 100 also includes a communication interface 118 coupled to bus 102. Communication interface 118 provides a two-way data communication coupling to a network link 120 that is connected to a local network 122. For example, communication interface 118 may be an integrated services digital network (ISDN) card or a modem to provide a data communication connection to a corresponding type of telephone line. As another example, communication interface 118 may be a local area network (LAN) card to provide a data communication connection to a compatible LAN. Wireless links may also be implemented. In any such implementation, communication interface 118 sends and receives electrical, electromagnetic or optical signals that carry digital data streams representing various types of information.
Network link 120 typically provides data communication through one or more networks to other data devices. For example, network link 120 may provide a connection through local network 122 to a host computer 124 or to data equipment operated by an Internet Service Provider (ISP) 126. ISP 126 in turn provides data communication services through the world wide packet data communication network now commonly referred to as the “Internet” 128. Local network 122 and Internet 128 both use electrical, electromagnetic or optical signals that carry digital data streams. The signals through the various networks and the signals on network link 120 and through communication interface 118, which carry the digital data to and from computer system 100, are exemplary forms of carrier waves transporting the information.
Computer system 100 can send messages and receive data, including program code, through the network(s), network link 120 and communication interface 118. In the Internet example, a server 130 might transmit a requested code for an application program through Internet 128, ISP 126, local network 122 and communication interface 118.
The received code may be executed by processor 104 as it is received, and/or stored in storage device 110, or other non-volatile storage for later execution. In this manner, computer system 100 may obtain application code in the form of a carrier wave.
In the foregoing specification, embodiments of the invention have been described with reference to numerous specific details that may vary from implementation to implementation. Thus, the sole and exclusive indicator of what is the invention, and is intended by the applicants to be the invention, is the set of claims that issue from this application, in the specific form in which such claims issue, including any subsequent correction. Any definitions expressly set forth herein for terms contained in such claims shall govern the meaning of such terms as used in the claims. Hence, no limitation, element, property, feature, advantage or attribute that is not expressly recited in a claim should limit the scope of such claim in any way. The specification and drawings are, accordingly, to be regarded in an illustrative rather than a restrictive sense.

## Claims

1. A method for rewriting a query that contains a XML query based-expression, the method comprising,
performing a first XML rewrite transformation on said query to generate a first inline view that replaces said XML query based-expression, said XML query based-expression being based on a XML query language that supports flow control commands and path expressions, said XML query based-expression including a path expression;
after performing said first XML rewrite transformation, performing a first view merge operation on said query to merge said first inline view within said query;
after performing said first view merge operation, performing an XML index rewrite on said query to generate a second inline view referencing database objects of an XML index and expressing an evaluation based on said path expression;
after performing said XML index rewrite transformation, performing a second view merge operation on said second inline view to merge said second inline view within said query; and
wherein the method is performed by one or more computing devices.

2. The method of claim 1, wherein the steps further include performing another XML rewrite transformation on said query after performing a second view merge operation.

3. The method of claim 2, wherein said another XML rewrite transformation is an XML index rewrite transformation.

4. The method of claim 1, wherein the steps further include performing an additional query transformation on said query between said performing a first XML rewrite transformation and performing a second view merge operation.

5. The method of claim 1, wherein the additional query transformation is not a view merge transformation and is not an XML rewrite transformation.

6. The method of claim 1, wherein said XML query language is XQuery.

7. A non-transitory computer-readable storage medium storing one or more sequences of instructions, said one or more instructions, which, when executed by one or more processors, causes the one or more processors to perform steps of:
performing a first XML rewrite transformation on said query to generate a first inline view that replaces said XML query based-expression, said XML query based-expression being based on a XML query language that supports flow control commands and path expressions, said XML query based-expression including a path expression;
after performing said first XML rewrite transformation, performing a first view merge operation on said query to merge said first inline view within said query;
after performing said first view merge operation, performing an XML index rewrite on said query to generate a second inline view referencing database objects of an XML index and expressing an evaluation based on said path expression; and
after performing said XML index rewrite transformation, performing a second view merge operation on said second inline view to merge said second inline view within said query.

8. The non-transitory computer-readable storage medium of claim 7, wherein the steps further include performing another XML rewrite transformation on said query after performing a second view merge operation.

9. The non-transitory computer-readable storage medium of claim 8, wherein said another XML rewrite transformation is an XML index rewrite transformation.

10. The non-transitory computer-readable storage medium of claim 7, wherein the steps further include performing an additional query transformation on said query between said performing a first XML rewrite transformation and performing a second view merge operation.

11. The non-transitory computer-readable storage medium of claim 7, wherein the additional query transformation is not a view merge transformation and is not an XML rewrite transformation.

12. The non-transitory computer-readable storage medium of claim 7, wherein said XML query language is XQuery.