Query optimization system and method6546381Abstract A query optimization system and method are particularly suitable for generating a least cost query plan expressible on a plurality of heterogeneous database schemas that are restructuring views of each other. The query optimization system receives a query against one of the tables and converts it to a plurality of alternate queries, each formatted for the particular schema of a different one of the plurality of heterogeneous tables. In generating the alternate queries, the system may utilize SchemaSQL view definitions and may employ restructuring operators expressing and capable of conducting a restructuring of a table into a restructuring view of the table. A middleware system involving a canonical schema and a virtual canonical table may also be employed to express a mapping between restructuring views for purposes of query conversion. The alternate queries may be costed and optimized and a query plan returned that has a least cost or service time involved. Claims What is claimed and desired to be secured by United States Letters Patent is: Description BACKGROUND OF THE INVENTION
TABLE 1
select distinct A
from risk-analysis -> S, risk-analysis::S -> A
where A < > "date" and A < > "xaction"
(Q1)
select distinct T.agent
from research-> S, research::S T
(Q2)
select distinct A
from traders-> A
(Q3)
select distinct T.agent
from profit-loss::buysell T
(Q4)
SchemaSQL Query: "List names of all agents" on the various databases: Q1 on risk-analysis; Q2 on research; Q3 on traders; and Q4 on profit loss. SQL queries allow variable declaration over the tuples in a relation. In contrast, SchemaSQL permits the declaration of variables that can range over (1) names of the relations in a database, (2) names of the attributes in a relation, and (3) values appearing in a column corresponding to a given attribute in a relation in addition to tuple variables allowed in SQL. Variable declarations in SchemaSQL follows the same syntax as <range> <var> in SQL, where <var> is any identifier. Table 1 shows the query "List names of all agents" expressed as SchemaSQL queries on the brokerage databases of FIG. 3. The simplest of them all is the query Q4 on the profit-loss database. In query Q4, T is a tuple variable that ranges over the buysell relation. The SchemaSQL syntax requires specifying the database name of the relation. Apart from this minor variation, query Q4 is also a SQL query. Query Q3 on the traders database, on the other hand, is a SchemaSQL query that makes use of a relation name variable to list the names of all the agents. Note that the agent names appear as relation names in the traders database. In query Q3, the declaration traders.fwdarw.A declares A as a variable that ranges over the relation names. In the research database, the agent names appear under the agent column across all the stock relations. Query Q2 captures this by declaring a relation variable, and A as an attribute name variable that ranges over the attribute names of the stock relations (risk-analysis::S.fwdarw.A) with the provision that it does not range over the column names that are not agent names (captured by the where clause). Besides querying, SchemaSQL also has the ability to define restructuring views of the data in databases that catapult data values to schema name positions and vice verse. For example, the brokerage firm databases of FIG. 1 can all be represented as SchemaSQL views of one another. In the following section of the restructuring operators are introduced and defined using generic SchemaSQL view definitions. Restructuring Operators FIG. 4 contains schematic flow charts (a) through (d) illustrating embodiments of the basic operation of the operators 72 of FIG. 2. Flowchart (a) illustrates a fold operation 210. Flowchart (b) illustrates an unfold operation 230. Flowchart (c) illustrates a split operation 250, and flowchart (d) illustrates aniunfold operation 270. The fold operator 74 can be characterized as folding column names into column data. The column names in the input relation on which the fold operator acts appear as column values in the output relation. For example, the SchemaSQL view definition V2 of table 2 below captures the ibm relation of the research database as a view of the ibm relation of the risk-analysis database. The fold operator 74 in one embodiment accomplishes a fold operation 210 of FIG. 4. Referring to FIG. 4, the fold operation 210 will be discussed in terms of an input table and a transformed output table. The fold operation 210 starts at step 212 and progresses to a step 214 in which the processor 16 receives a SchemaSQL or other suitable view definition 82 embodying instructions for implementing the fold operation. At a step 216, in accordance with the view definition 82, a new relation is created based upon the input table and is provided with an additional column obtained from the create view clause. At a step 218, column names from the input table are placed in the new column as column data. At a step 220, new rows are created to contain the data within previously within the columns which are being folded. At a step 222, the folded column data is placed in the new rows. At a step 224, the operation ends. The risk-analysis department table 172 and research department table 180 of FIG. 3 illustrate one example of the fold operation 210. Applying the fold operation 210 to the risk-analysis department table 172 results in the reformation to the research department table 180. Specified column names 202 of the risk-analysis department table 172 are folded into column data 206 in the research department table 180. The fold operation 210 can also be expressed in a particular example as a generic SchemaSQL definition in which, C1, C2, etc., denote distinct individual column names, <C> denotes a set of column names, and X, Y, T, A, etc. denote distinct variables. The view definition is as follows:
create view db::rel' (C1, C2, <C>) as
select X, Y, T.<C>
from db::rel T, db::rel-> X, T.X Y
where preserveConditions (X)
In this definition, C1, C2 are not equivalent to <C>. We call C1 the foldOn column and C2 the foldBy column. The set of columns <C> is called the PRESERVE-d columns. The preserveConditions (X) statement ensures that the PRESERVE-d columns indeed get preserved in the output schema. The unfold operator 78 is the inverse of the fold operator 74. The unfold operator 78 transports the column values in the input relation into column name positions in the output relation in an information preserving manner. For example, the relation ibm of the risk-analysis database in FIG. 3(i) is an unfold-ed representation of the relation ibm of the research database, FIG. 3(ii). This is because agents appear as column values in the latter database, and appear as column names in the former. The unfold operator 78 may be characterized by the unfold operation 230 of FIG. 4. The unfold operation 230 starts at a step 232 and progresses to a step 234 where the processor 16 receives a SchemaSQL or other suitable view definition embodying instructions for implementing the unfold operation. In accordance with the view definition, at a step 236, column data from a specified column of the input relation are transformed into column names and placed in the output relation. At a step 238, data from rows in which each data transformed to attributes appeared is placed in rows with data from a specified column of the input relation appearing as column data for the new columns of the output relation. The unfold operation ends at a step 240. The risk-analysis department table 172 and research department table 180 of FIG. 3 also illustrate an example of the unfold operation 230. Applying the unfold operation 230 to the research department table 180 results in a reformation to the risk-analysis department table 172. The unfold operation 230 results in specified column values in the input relation, the research department table 180 being transported into column name positions in the output relation, the risk-analysis department table 172. The unfold operation 230 can also be expressed in a particular example as a generic SchemaSQL definition in which, C1, C2, etc., denote distinct individual column names, <C> denotes a set of column names, and X, Y, T, A, etc. denote distinct variables. The view definition is as follows:
create view db::rel' (X, <C>) as
select Y,T.<C>
from db::rel T, T.C1 X, T.C2 Y
In this relation, C1, C2 are note equivalent to <C>. We call C1 the unfoldOn column and C2 the unfoldBy column. The set of columns represented by <C> are the preserve-d columns. In the example V1 in Table 2 below, the unfoldOn column is agent and the unfoldBy column is value. Date and xaction are the preserve-d columns. The split operator restructures a specified relation into a set of relations whose names are obtained from specified column values in the original relation. For example, the agent007 and agent 008 relations 188, 190 of the traders database in FIG. 3(iii) is a split rendering of the stock_trades relation of FIG. 9. One embodiment of the manner of operation of the split operator 76 is illustrated by the split operation 250 FIG. 4. The split operation 250 starts at a step 252 and progresses to a step 254 in which the processor 16 receives a SchemaSQL or other suitable view definition embodying instructions for implementing the split operation. In accordance with the view definition, at a step 256, the new relations that are to be created are specified in the view definition, which may be entered as a parameter by a user. At a step 258, specified column names are transformed to relation names. At a step 262, rows corresponding to the input relation column data placed as column names in the output relation are placed into the output relation. The split operation 250 ends at a step 264. The traders department table 186 and profit-loss department table 192 of FIG. 3 also illustrate an example of the split operation 250. Applying the split operation 250 to the profit-loss department table 192 results in a reformation to the traders department table 186. The split operation 250 results in a restructuring of the buysell relation 194 into a set of relations ag007 (188), and ag008 (190) whose names are obtained from column values 206 in the original relation, buysell 194. The split operation 250 can also be expressed in a particular example as a generic SchemaSQL definition in which, C1, C2 etc., denote distinct individual column names, <C> denotes a set of column names, and X, Y, T, A, etc. denote distinct variables. The view definition is as follows: create view db::X(!C?) As select R.!C? From db::rel R, R.C1 X In this relation, we call C1 the splitOn column. All the relations in db not affected by the split operator are called the preserve-d relations. In the example view definition V3 in Table 2 below, agent is the splitOn column. Assuming that there are no other relations in db, the set of preserve-d relations is the empty set. A unique characteristic of the split operator, compared to the other restructuring operators and the classical relational algebra operators is that, it takes a single relation as input and generates a set of relations as output. The split operator is the only operator in the present invention that produces a set of relations as output. Due to the nature of the split operator, a restructuring-view in general can be a set of relations in a database. The unite operator 80 is the inverse of split operator 76. The unite operator has the effect of combining several relations conforming to the same schema into a single relation, so that the relation names of the input relations appear in the data position in the transformed schema. For example, the stock_trades relation of FIG. 9 is a result of the unite operator applied on the agent relations of the traders database in FIG. 3(iii). The unite operator 80 in one embodiment performs the unite operation 270 of FIG. 4. The unite operation 270 starts at a step 272. At a step 274, the processor 16 receives a SchemaSQL or other suitable view definition embodying instructions for implementing the split operation. In accordance with the view definition, at a step 276, an output relation is created with a new column with a column name specified by the user, possibly through a parameter call or in response to a prompt. At a step 278, specified relation names from the input relation are placed in the output relation in the new column. At a step 280, data rows for each occurrence of the new column name from the input relation are placed in the output relation. The unfold operation 270 ends at a step 282. The traders department table 186 and profit-loss department table 192 of FIG. 3 also illustrate an example of the unite operation 270. Applying the unite operation 270 to the traders department table 186 results in a reformation to the profit-loss department table 192. The unite operation 270 results in a combining of specified relations, ag007 (188) and ag008 (190) into a single relation, buysell 194, so that the relation names ag007, ag008 appear in the data position 206 in the transformed profit-loss department table 192. The unite operation 270 can also be expressed in a particular example as a generic SchemaSQL definition in which, C1, C2, etc. denote distinct individual column names, <C> denotes a set of column names, and X, Y, T, A, etc. denote distinct variables. The view definition is as follows: create view db::rel' (C1, <C>) select X, T.<C>p1 from db.fwdarw.X, X T where preserveConditions (X) In this view definition, we call C1 the uniteOn column. We call the complement of the set of relations in the database on which unite acts as the preserve-d relations. The preserveConditions (X) conditions ensure that the PRESERVE-d relations indeed get preserved in the output database. In example V4 of Table 2 below, the uniteOn column is agent. All relations of the traders database participate in the unite operator. So the set of PRESERVE-d relations is the empty set.
TABLE 2
create view
risk-analysis::ibm (date, xaction, A) as
select I.date, I.xation, V
from research::ibm I, I.agent A, I.value V
(V1)
create view
research::ibm (date, xaction, agent, value) as
frorn risk-analysis::ibm I,
risk-analysis::ibm->A, I.A V
where A!= `date` and A!= `xaction`
(V2)
create view
traders::A(date, stock, xaction, value) as
select R.date, R.stock, R.xaction, R.value
from db::stock_trades R, R.agent A
(V3)
create view
db::stock_trades (date, stock, agent, xaction, value) as
select T.date, T.stock, A, T.xaction,
T.value
from traders->A, A T
(V4)
Examples Illustrating the Restructuring Operations Table 3 is a listing of the restructuring transformation among the databases 51 of FIG. 3. The preserve information for Split and Unite is the empty set in this example.
TABLE 3
Source (db/rel) Restructuring Expression Destination
research::ibm unfold on agent by value risk-analysis::ibm
preserve date, xaction
risk-analysis::ibm fold on agent by value research::ibm
preserve date, xaction
profit-loss::buysell fold on xaction by value traders
preserve date, stock, agent;
split on agent
traders unite on agent; p r o f i t -
unfold on xaction by value loss::buysell
preserve date, stock, agent
research unite on stock; traders
split on agent
MDBS Architecture FIG. 5 represents one embodiment of a MDBS data management architecture 300 suitable for use with the query optimization system 50 of FIG. 1. Also shown in FIG. 5 is a schema restructuring module 322 for creating restructuring views 316, 318, 320 from one or more base tables 302, 304, 306. Whereas the restructuring views 170 of FIG. 3 are naturally occurring, FIG. 5 illustrates an arrangement that may occur within data warehouses. One such system is IBM's Datajoinerer.TM.. Datajoiner is a heterogeneous database system that provides a single database image of multiple databases and provides transparent access to tables at remote databases through user defined aliases that can be accessed as local tables. DataJoiner is also a fully functional relational database system. Queries submitted to the MDBS are optimized using a cost based optimizer that has knowledge of the remote optimizer capabilities to generate an optimal global plan. In FIG. 5, the schema restructuring module 322 is used to transform the base tables 302, 304, 306 into restructuring views 316, 318, 320. In so doing, a mapping 308, 310 is first generated capable of transforming the base tables into the canonical schema. From the canonical schema 312, 314, the base tables 302, 304, 306 are transformed into restructuring views 316, 318, 320. The system is preferably managed by a system such as Datajoiner which implements the query optimization system 50 of FIG. 2 to promote rapid querying and analysis of data within the base tables 302, 304, 306. Conversion Operations FIG. 6 illustrates one manner of implementing the CS2RV conversion module 104 of FIG. 2. FIG. 6 depicts a CS2RV operation 330 which starts at a step 332. At a step 334, the a processor 16 executing the operation 330 receives and reads a query Q.sub.c, a query on the canonical schema (e.g. the query 112 of FIG. 2). At a step 336, a schema mapping 85 such as the mappings 308, 310 of FIG. 5, which may be SchemaSQL view definition, is received by the processor 16 and read. The CS2RV operation 330 branches at a query step 338. At step 338, the CS2RV operation 330 references the particular type of the mapping received at step 336. The operation 330 successively processes each unfold and split operation and unites them at the end. Thus, the operation 330 at block 338 checks the statements in the schema mapping 85, and if the next successive statement involves an Unfold operator 78, the CS2RV operation progresses on to a step 340. If the mapping involves a Split operator 76, the CS2RV operation 330 progresses to a step 348. At the step 340, the CS2RV operation 330 once again branches. The CS2RV operation 330 checks whether the mapping references an UNFOLDON OR UNFOLDBY attribute. If so, the CS2RV operation proceeds to a step 342. At step 342, a catalog query is issued that is preferably a query against metadata (relation and column names) references in the schema mapping 85. One example of a suitable catalog query is:
select columname
from syscolumns
where rename = `rel` and columname != <preserved
columns> and pred(columname)
where pred(columname) is a condition derived from a condition of the form Pred(unfoldon) relevant to the tuple variable, present in the where clause. At a step 344, the CS2RV operation 330 modifies the query Q.sub.c to generate new queries. The modification in one embodiment takes the form: Let col1, col2 be two distinct elements in the output of step (1). For every distinct pair of tuple variables that reference the unfoldon attribute in Q, replace the occurrence of the attributes with `col1` and `col2`. For every pair of corresponding occurrences of the unfoldBy attribute in Q, replace it with col1 and col2. At a step 346, the CS2RV operation 330 generates a union query Q.sub.1 that is a union of all the queries generated in step 344. Returning to step 348, if the CS2RV operation 330 branches to step 348, a check is made to see if the query 112 has the occurrence of the spliton column. If so, at a step 350, a catalog query is issued. The catalog query may be of the form:
select relname
from systrelations
where pred(rename)
where pred(rename) is a condition derived from a condition of the form pred(spliton) present in the where clause of Q. At a step 354, for each element rname in the output of the query in step 350, a query is generated by modifying Q.sub.c in the following manner: replace every occurrence of spliton attribute in Qc with `rname`; replace the reference to rel in the from clause of Qc to rname. At a step 356, a union query Q.sub.2 is generated that is a union of all the queries generated in step 354. At a step 358, the CS2RV operation returns either the union query Q1 or the union query Q2, depending upon the branch at step 338. At a step 359, the operation 330 checks to see if any more statements containing unfold or split operators exist. If so, the operation 330 returns to the block 336 and repeats. If no further mapping statements exist, at a step 360, the results previous iterations of the operation 330, if any, are added to a final result and united. At a step 360, the operation 330 ends. One embodiment of the CS2RV operation 330 is illustrated in Example 1: Consider the canonical table stock trades of FIG. 9, and the query "List the dates and value of ibm stocks sold by ag007 such that the value exceeds the value of ibm stocks sold by ag008 on the same day" (Query Q1 of Table 1 above) expressed against the stock_trades table of FIG. 9.
select A.date, A.value
from stock_trades A, stock trades B
where A.stock = `ibm` and A.agent = `ag007` and
B.stock = `ibm` and B.agent = `ag008` and
A.stock = B.stock and A.date = B.date and
A.xaction = B.xaction and
A.xaction = `sell` and A.value > B.value
Our algorithm will translate this query against the risk-analysis database in the following manner. Note that the transformation is an unfold allowed by a split. The unfoldOn attribute is agent and the unfoldBy attribute is value. The catalog query of step 1 will generate a unary relation consisting of all the agent names. Step 2 will generate the SQL query Q1 of FIG. 7 in a mechanical fashion. Based on our algorithm, the split transformation will induce the rewrite shown as query Q2.
Query Q2
Select A.date, A.ag007
from ibm A, ibm B
where A.date = B.date and
A.xaction = B.xaction and
A.xaction = `sell` and
A.ag007 > B.ag008
Note that the algorithm factors in the predicates A.stock=`ibm` and B.stock=`ibm` while generating the SQL query. Finally, query Q2 will get rewritten into:
select A.date, A.ag007
from ibm A
where A.xaction = `sell` and A.ag007 > A.ag008.
EXAMPLE 1 FIG. 7 illustrates one manner of implementing the RV2CS conversion module 102 of FIG. 2. FIG. 7 depicts a RV2CS operation 370 which starts at a step 372. At a step 374, the processor 16 executing the operation 330 receives and reads a query Q.sub.RV, a query on a restructuring view 54, 56, 58 or base table 62 (e.g. the query 66 of FIG. 2). At a step 376, a schema mapping 85 such as the mappings 308, 310 of FIG. 5, which may be a SchemaSQL view definition, is received by the processor 16 and read. The RV2CS operation 370 branches at a query step 378. At step 378, the RV2CS operation 370 references the particular type of the mapping for each statement received at step 376. If the mapping involves a Fold operator 74, the RV2CS operation 370 progresses on to a step 380. If the mapping involves a Unite operator 80, the RV2CS operation 330 progresses to a step 398. At step 380, the FOLDON attribute is obtained and defined to be colfoldon. At a step 382, the FOLDBY attribute is obtained and defined to be colfodlby. At a step 384, a canonical relation canrel is obtained. At a block 386, the operation 370 loops for every occurrence in the select clause and/or the where clause of a PRESERVE-d attribute a in the query Q. For every loop, at a step 388, the occurrence is replaced with a ta.colfodlby. At a step 389 in the loop, a declaration canrel Ta is added to the from clause. At a step 390, conditions in the form of Ta.coldfoldon=`a` are added to the where clause. At a step 392, the operation 370 loops to repeat for every pair of distinct non-PRESERVE-d attributes a, b in the query Q. Within the loop, a step 394 adds a condition to the where clause of the form `Ta.<preserved attributes>.` At a step 396, the resulting query is returned. When the operation 370 identifies a unite operator and branches to the step 398, a UNTON attribute is obtained and defined to be uniteon. At a step 400, the operation 370 loops for every declaration in the from clause of a non-PRESERVE-d relation `rel t` in Q. Within the loop at a step 402, `rel T` is replaced with `canrel T.` At a step 404 within the loop, conditions are added to the where clause of the form "T.uniteon=`rel.`" At a step 406, the resulting query is returned. At a step 408, the RV2CS operation 370 ends. Base Table Query Generation FIG. 8 is a schematic flow chart illustrating one manner of operation of the base table query conversion module 105 of FIG. 2. As discussed, when the query 66 is posed against one of the restructuring views 54, 56, 58, rather than against a base table 52, or when other base tables exist within the MDBS 55 (e.g., as in the MDBS 300 of FIG. 5), the query 66 is preferably converted to a query 116 against the base table before being converted to queries 120 on the others of the restructuring views. In one embodiment, the conversion operation 410 begins at a step 412 and progresses to a step 414 in which the operation 410 consults a catalog of metadata within the schema mapping 85 to identify the canonical schemas corresponding to the restructuring-views referenced in the query 66. At a step 416 mapping information between the restructuring-view and its corresponding canonical schema is obtained. At a step 418, the RV2CS conversion module 102 is employed to generate the canonical query 112. As discussed above, the RV2CS conversion module 102 may operate in the manner described above for the RV2CS operation 370 of FIG. 7. The information obtained in steps 412 and 414 is referenced by the RV2CS conversion module 102 in step 418. At a step 420, the operation 410 checks to see if the canonical schema is defined as a view on a base table, and if so, references to the canonical query 112 are replaced with view definitions 82. At a step 422, the thusly generated base query 116 is returned. At a step 424, the operation 410 terminates. Example 2 illustrates the usage of the base table query generation operation 410 of FIG. 8: Consider the scenario where the brokerage firm of our example involving FIG. 3 contains another base table agent_trades 428 (of FIG. 9) that has the schema (date, agent, xaction, stock, value, commission). For the sake of simplicity, we assume agent_trades is a single table, but in real life it may be a join of two base tables. The canonical table stock trades can be expressed as the following simple view on the agent trades table. create canonical table stock_trades (date, agent, xaction, stock, value) as select date, agent, xaction, stock, value from agentTrades Now, consider the query "List the dates and value of ibm stocks sold by ag007 such that the value exceeds the value of ibm stocks sold by ag008 on the same day" (from Table 1) expressed against the traders dept. database 186 of FIG. 3. The user query is the query UQ of Table 4 below. The RV2CS operation translates query UQ to query CQ on the canonical- schema. Since the stock_trades relation, we make use of this view definition to rewrite the above query to a query on the base table. The resulting query is the query BQ in Table 4. EXAMPLE 2
TABLE 4
select A.date, A.value
from ag007 A, ag008 B
where A.stock = `ibm` and
A.xaction = `sell` and A
B.stock = `ibm` and
B.xaction = `sell` and
A.date = B.date and
A.value > B.value
(UQ)
select A.date, A.value
from agentTrades A, agentTrades B
where A.stock = `ibm` and A.xaction = `sell` and
B.stock = `ibm` and B.xaction = `sell` and
A.agent = `ag007` and B.agent = `ag00B` and
A.date = B.date and A.value ? B.value
select A.date, A.value
(BQ)
select A.date, A.value
from stock_trades A, stock_trades B
where A.stock = `ibm` and
A.xaction = `sell` and
B.stock = `ibm` and
B.xaction = `sell` and
A.agent = `ag007` and
B.agent = `ag008` and
A.date = B.date and
A.value > B.value
(CQ)
Query submitted by the user. CQ: Translated query on the canonical schema. BQ: The user query expressed on the base tables. Canonical Schema The restructuring operators of the previous section, by blurring the distinction between data and meta-data, provide a framework where seamless querying of both data and schema is possible. From a practical perspective, the need for querying schema components arises because the tokens that the application treats as data appears as a schema component in the database. The notion of canonical schema introduced in this section is based on the observation that if all the objects of query-able interest are modeled as data, the application can express its queries in any first-order query language (such as SQL) and would not need the capability for metadata querying. Thus, canonical schema is a central component in our query processing architecture. The canonical schema of the present invention is the same as the first-order schema presented in Miller R. J., Using Schematically Heterogeneous Structures, published in: In Proceedings of the ACM SIGMOD Conference, pages 189-200, Seattle, Wash., May 1998. We first define the canonical schema and then present a result that brings out the power of the restructuring operators. Definition of Canonical Schema. Given a set of queries Q, a relational schema S is called a canonical schema relative to Q if all queries Q can be expressed as first-order queries on S. For example, the schema of the relation Stock_trades of FIG. 9 is a canonical schema relative to the queries we have considered so far in this paper because all objects of query-able interest, namely date, stock, agent, xaction, and value, are modeled as data. We now present the following theorem that establishes the power of the restructuring algebra. Let V be a restructuring-view, Q be a set of queries on V, and S be a canonical schema relative to Q. There exist expressions .tau., .tau.' consisting only of the restructuring operators such that for every instance V.sub.I on V, (1) .tau.(V I)=S.sub.I is an instance of S, (2) .tau.' (S.sub.I)=V.sub.I and (3) S.sub.I satisfies the following property: .A-inverted.Q.epsilon.Q, there exists a first-order query Q' on S such that Q(V.sub.I).ident.Q ' (S.sub.I). Proof Sketch: There are two parts of this proof The first part proves that the transformation has the ability to restructure a schema to a canonical schema and back. The proof is based on the observation that the operators retain the canonical schema information every step of the way. The second part proves that the transformation is performed in an information preserving manner. The proof for the this draws on the semantics of SchemaSQL. The details are presented in Miller. The present invention makes use of the above result to formulate operations for restructuring-views based query processing and optimization. For example, the operations CS2RV 330 and RV2CS 370 are based on the ability to translate queries on the restructuring-views to the canonical schema and to translate queries on the canonical schema back to queries on the restructuring-views. FIG. 9 shows one embodiment of a canonical table 426 presented as a representative example of the canonical table 92 of FIG. 2. The canonical table 426 is entitled stock_trades. The stock_trades canonical table 426 of FIG. 9 is generated based upon the restructuring views tables 52, 54, 56, 58 of FIG. 2. The agent trades table 428 of FIG. 9 is used in examples herein. Method of Operation of Query Optimization System FIG. 10 shows one embodiment of a method 520 of operation of the schema mapping module 70 and query processing module 100 of FIG. 2. FIG. 14 shows one embodiment of a method 500 of operation of the query optimization module 150 of FIG. 2. The method 520 of FIG. 10 starts at a step 522. At a step 524, a base table is provided. In the depicted embodiment of FIG. 2, the base table is the table 52 of the MDBS 55. At a step 526, restructuring views are provided. In FIG. 1, the restructuring views are the tables 54, 56, 58. As discussed above, the MDBS 55 could be provided with all naturally occurring databases 51, in which case, we do not necessarily refer to a base table. As discussed for the MDBS 300 of FIG. 5, multiple base tables may exist, and the restructuring views may be artificially constructed as part of a data warehouse, and may be created with a schema restructuring module 322. At a step 528, a schema mapping 85 is preferably generated mapping the restructuring involved between the heterogeneous schemas 60 of the base table(s) and the restructuring views. The schema mapping 85 may, in one embodiment take the form of SchemaSQL views, as described, and may employ operators such as the restructuring operators 72 of the present invention. At a step 530, a canonical schema is preferably identified for the mapping of the base table to the restructuring views, preferably with a module such as the canonical schema module 75. The canonical schema may take the form of a canonical table 92, one example of which is the stock_trades relation 426 of FIG. 9. At a step 532, an original query 66 is generated. The query 66 may be posed against the base table 52, the restructuring views 54, 56, 58, or against the canonical table, which may or may not be a view, and which may or may not be materialized, as discussed. At a step 534, the query 66 may be translated to a query 116 on the base tables. In so doing, the base table query conversion module 105 may be employed, and may be used in the manner described above for FIG. 8. Step 534 need not be employed, of course, if query 66 is posed against the bast table 52 as shown by the dashed line 65, and where other base tables are not included within the query optimization system 50. At a step 536, the canonical map table 132 may be generated. The canonical map table 132 is preferably generated using the canonical map table generation module 106, which may employ an operation such as a canonical map table generation operation 450 shown in FIG. 12 and discussed in greater detail below. One example of a canonical map table 430 is shown in FIG. 11. As discussed, the canonical map table 112 is preferably used as a reference by the RV2CS operation 102 and the CS2RV operation 104 and by the query optimization module 150. As part of the canonical map table generation, at a step 538, self-joins in the query 66 are preferably identified. The occurrences of the self-joins are included in the canonical map table as in a manner to be described. At a step 540, a restructuring views map table 134 is preferably generated. The restructuring views map table 134 is preferably an adjunct to the canonical table map table 132 and is used in a similar fashion, as discussed. One example of a restructuring views map table 438 is shown in FIG. 11. One embodiment of a method 470 of generation of the restructuring views map table 134 is shown in FIG. 13 and is discussed below. At step 542, the base table query or queries 116 are translated to a canonical query 112. The translation or conversion is in one embodiment conducted by the RV2CS algorithm, which preferably operates as described above for FIG. 7. Step 542 may need to be repeated where there are multiple base table queries, e.g., as a result of step 534 and situations such as in FIG. 5 where multiple base tables 302, 304, 306 and canonical schemas 312, 314 exist. At as step 544, the canonical query 112 is translated to alternate queries 120 on the restructuring views 52, 54, 56. The translation or conversion is in one embodiment conducted by the CS2RV algorithm, which preferably operates as described above for FIG. 6. Step 544 may be repeated for each of the restructuring views 52, 54, 56 in the MDBS 55. At a step 546 the alternate queries 120 are returned together with the generated map tables 130. The returned queries may include the original query 66 and one or more queries 116 on the base tables, as well as queries 120 posed against one or more of the restructuring views 54, 56, 58. At a step 548, the method 520 ends. Map Tables and Map Table Generation In the query processing module 100 of the present invention, the translated query 116 on the base tables is used to generate data structures referred to herein as the map tables 130. For a discussion of conventional Map Tables, reference is made to S. Chaudhuri, R. Krishnamurthy, S. Potamianos, and K. Shim, Optimizing Queries with Materialized Views, In Proceedings of the IEEE Conference on Data Engineering, March 1995. Map Tables store the plan alternatives for subexpressions in the input query and are used by the query optimizer to evaluate the various possible ways of executing the query. The present invention constructs the Map Table by identifying the portions of the query that can be answered by querying the canonical schema and/or the restructuring-views. The present invention adapts the Map Table generation algorithm of Chaudhuri for this purpose. Herein, the Chaudhuri Map Table algorithm is referred to as the CKPS algorithm. Unlike the CKPS algorithm that has one Map Table, the present invention preferably maintains two Map Tables--the canonical Map Table 132 and the restructuring-views Map Table 134 to store the plan alternatives information. The canonical Map Table 132 is similar to the Map Table of Chaudhuri with the exception that the predicates applied to the quantifiers are preferably stored in the table along with the quantifiers. The restructuring-views Map Table 134, on the other hand, is a new table introduced as part of the present invention. It has two columns, an ID column 440 and a restructuring view query column 442 in which information on the plan alternatives involving restructuring-views is stored. We describe the canonical Map Table generation in this section. Below, we describe how the restructuring-views Map Table 134 is generated from the canonical Map Table 136. The canonical Map Table 136 is generated by identifying phrases from the query 66 that can be replaced with phrases of the query 112 on the canonical schema 92. The canonical Map Table 132 has three columns, an ID column 432, a delete query column 434, and a canonical query column 436. The ID column 432 is a unique identifier for each phrase entry. The delete query column 434 corresponds to a subexpression in the original query 66. The canonical query column 436 is the added query phrase that corresponds to an equivalent query 112 on a canonical schema 92 that can be used to replace the corresponding phrase from the original query 66. Unfold and Its Impact The example in Table 1 illustrates a subtlety involving the unfold operator 78. The simple selection query Q1 of Table 1 on the unfold-ed table translates to a self-join query on a fold-ed table in queries Q2, Q4. In other words, a self-join query on the canonical table 92, under the right conditions, can be translated into a selection query on a restructuring-view. However, the traditional Map Table algorithm does not consider the possibility of replacing a self-join with a single table access query. We account for this in the canonical map table generation operation 450 of FIG. 12 by identifying self-join queries and adding a corresponding entry to the canonical Map Table 132. FIG. 11 illustrates the entries in the Canonical Map Table 430 for the query (on table agent_trades) of Example 2. The first two entries indicate that the query on the agent_trades table 428 (of FIG. 9) can be replaced with a query on the stock_trades table 426 (of FIG. 9). The third entry in the table is created by analyzing the first two entries in the Map Table 430. The entries in the Map Table 430 of FIG. 11 correspond to: (1) The canonical table stock trades have unfold-ed counterparts in the risk-analysis database; (2) The delete query on id's 1 and 2 have selections on the column agent; and (3) The user query UQ of Table 4 has a join on the column date and xaction. FIG. 12 is a schematic flow chart diagram illustrating a canonical map table generation operation 450. The operation 450 starts at a step 452. At a step 454 the SQL query 116 on the base table 52 and/or one of the restructuring views 54, 56, 58 is received and read by the processor 16. At a step 456, schema mappings of the canonical schema 92 to the base table 52 or restructuring views 54, 56, 58 is received and read by the processor 16. At a step 458, the canonical schemas are treated as virtual materialized views, and the CKPS map table algorithm (discussed above) is applied to generate a basic canonical map table. At a step 460 map identifier and predicates on the canonical schema are stored in the map table. At a step 462, the operation 450 loops recursively and performs recursively until fix point the step 464. The step 464 first asks if the query portion being examined can be answered by querying CS. If so, step 464 adds a new row to the canonical table corresponding to the CS query portion. Step 464 may take the form of the following operation: Let CS (R) denote the canonical schema, Q.sub.del (R) denote the delete query, and Q.sub.can (R) denote the canonical query on CS (R), of a row R in the MapTable. If two rows R1 and R2 on the canonical MapTable satisfy the following criteria: (1) CS (R1) =CS (R2)=CS, and unfold-ed restructuring-view is defined on CS with unfoldon column unfoldon, unfoldBy column unfoldby, and a set of preserved columns preserve-set, and (2) Q.sub.del (R1) and Q.sub.del (R2) are selection(s) on unfoldon column, and (3) input query Q has a join condition between literals of Q.sub.del (R1) and Q.sub.del (R2) on the preserve-set columns, then Add a new row R3 to the canonical MapTable: Q.sub.del (R3)=Q.sub.del (R1), Q.sub.del (R2), join condition between literals of Q.sub.del (R1) and Q.sub.del (R2) on the preserve-set columns; Qcan(R3)=Q.sub.can (R1), Q.sub.can (R2), join condition between literals of Q.sub.can (R1) and Q.sub.can (R2) on the preserve-set columns. At a step 466, the canonical table 132 is returned. At a step 468, the operation 450 ends. Each entry of the canonical MapTable generated by the operation 450 has information to replace a portion of the query 66 with a query 112 on the canonical schema 92. Since the canonical schema is a virtual materialized view `proxy-ing` for the restructuring-views which contain the actual data, these queries in turn have to be translated into queries on the restructuring-views which contain the actual data, these queries in turn have to be translated into queries on the restructuring-views. This is accomplished via the CS2RV algorithm of FIG. 6. For a given query, there may be multiple translations involving restructuring-views since more than one restructuring-view may map to the same canonical schema. These alternatives are captured in the restructuring-views MapTable. This table has two entries, the first entry is the map ID 440 that identifies a corresponding entry in the canonical MapTable 132 and the second entry is an equivalent query on the restructuring-view(s). FIG. 13 describes how the restructuring-views MapTable is generated. In the restructuring views map table 438 of FIG. 11, the first four entries are the alternatives for processing the first entry in the canonical MapTable. The four entries following that in the restructuring-views table are the alternatives for the second entry in the canonical MapTable. Note how the third entry of the canonical MapTable gets translated-the CS2RV operation of FIG. 6 converts the self-join query to a simple select query because of the unfold restructuring. The query graph, the canonical MapTable and the restructuring-views MapTable are sent to the plan enumeration phase of the query optimization. FIG. 13 illustrates one embodiment of a restructuring views map table generation operation 470. The operation 470 starts at a step 472 and proceeds to a step 474 where the canonical map table is received and read by the processor 16. At a step 476, the schema mappings 85 mapping the canonical schema 92 to the restructuring views 54, 56, 58 is received and read by the processor 16. At a step 478 the operation 470 loops and repeats for each entry with a common map identification number in the ID column 432 of the canonical table 132. The loop steps include steps 480, 482, and 484. At a step 480, the mapping information in the schema mapping 85 is queried to identify the set of restructuring views for the canonical schema. At a step 482, for each selfjoin query in the canonical map table, the restructuring view with the unfold-ed column is considered. At a step 484, the operation 470 loops and repeats steps 486 and 490 for each restructuring view rv.sub.j. At step 486, the CS2RV conversion module 104 together with the attendant operation of FIG. 6 are used to generate equivalent query Q' on each restructuring view rv.sub.j, such that CQ.sub.m (CS).ident.=Q'(rv.sub.j). At step 490, the entry <m, Q'(rv.sub.j)> is added to the restructuring views map table. At a step 492, the generated restructuring views map table 134 is returned. At a step 494 the operation 470 ends. Query Optimization FIG. 14 illustrates one method 500 of operation of the query optimization module 150 of FIG. 2. The method 500 may be used independently of the method 450 of FIG. 10 or may be performed in conjunction with the method 450 of FIG. 10. The method 500 begins at a start step 502. At a step 504, the processor 16 receives and reads the query 116 on the base table(s) 52. Preferably, the query has been optimized with standard query manipulation processes prior to being submitted. These query manipulation processes typically include processes such as parsing. At a step 506, the processor 16 receives and reads the alternate queries 120 on the restructuring views 54, 56, 58. At a step 507, the processor 16 optionally receives and reads one or more queries against a materialized view that is not a restructuring view of other tables in the MDBS. The query optimization system 50 of the present invention is flexible in that if materialized views are present, queries on the materialized views may be generated in manners known in the art and returned together with or in place of the queries 120 on the restructuring views. Similarly, the query 116 on the base tables may be read in or not, where applicable. At a step 508, the map tables 132, 134 are read by the processor. At a step 510, the plan enumeration module 154 is preferably employed to generate a number of alternative query plans. The operation of one embodiment of a plan enumeration module is described below. At a step 512, the alternative query plans generated at step 510 are costed to identify the query plan executable at a least cost. The costing of each submitted query plan is conducted by the costing module 152 to determine which of the query plans has the lowest cost. The costing is conducted in a manner that is well known in the art. The map tables 132, 134 may be consulted for this purpose. The identified least cost query or combination of queries is identified at a step 512 and is submitted to the plan enumeration module 154. At a step 514, the query plan 160 with the least cost is identified, and at a step 516, the optimized query plan 160 is returned by the query optimization module 150, converted to machine code with the code generation module 156, and submitted to the database system 65 for servicing. At a step 517, the optimized query plan 160 is executed on the appropriate table 55, and the query result 68 is returned to the user through the user interface 64. At a step 518 the method 500 ends. The optimized query plan 160 preferably contains instructions native to the host database system or systems 65 to consult a catalog or index within the database system 65 and take the appropriate steps to scan the appropriate tables for the data sought to be retrieved. Plan Enumeration and Costing The query optimizer in one embodiment takes the query graph, canonical MapTable, and restructuring-view MapTable as input and produces the best query plan using a dynamic programming model. The plan enumeration algorithm is in one embodiment based on the Starburst cost-based optimizer technology. At each state of the query optimization phase in a Starburst style optimizer, the plan of a smaller subexpression is used to build the query plan for a larger subexpression. Ordinarily, the query optimizer, during the plan enumeration algorithm, generates the best plan for a subexpression in the query. The best plan of a smaller subexpression is used to build the query plan for a larger subexpression. Ordinarily, the query optimizer during plan enumeration would consider the various plan alternatives such as index scan and table scan for single table access plans and merge join and nested loop join for join plans. The present invention modifies the optimizer to also consider plan alternatives on the restructuring-views, for those subexpressions that had equivalent query alternatives on the restructuring-views. The portion of the plan enumeration algorithm that generates the plan alternatives on the restructuring views is detailed by the Plan Enumeration Operation Algorithm: Input: A subexpression in the query being planned, the canonical and restructuring-view MapTable. Output: Best query plan BP with the lowest cost BC Generate query plan PBT with cost CBT on the base tables for the input query subexpression Initialize best query plan BP=PBT, and best cost BC=CBT Determine if the portion of the query planned matches any entry in the canonical MapTable If a matching entry is found in the canonical
Maptable{
for each entry in the restructuring views
MapTable corresponding to the canonical MapTable
entry {
generate query plan PRV with cost CRV for the
query on the restructuring view
If (BC > CRV) then BC = CRV; BP = PRV;
{
{
return BP and BC
Plan Enumeration Operation Algorithm When generating a query plan for a query subexpression, the plan enumerator consults canonical MapTable to determine if there are equivalent queries on the canonical schemas. If one or more entries are found, query plans are generated for the entries in the restructuring-views MapTable corresponding to those entries in the canonical MapTable with the same map identifiers. For instance, in our running example query of Example 2, when planning access to the agentTrades table, the plan enumerator will consider four plan alternatives wit hmap identifier 1 in the restructuring-views MapTable, with predicates agent=`ag007` and stock=`ibm`. The plan enumeration algorithm as before, considers all alternatives, and generates the best query plan based on cost. This enumeration process is entirely cost-based, and the query plan that accesses data from the restructuring-view is chosen only when the cost of the query plan on the restructuring-view has the most optimal cost. Queries with Aggregation The operations described in the previous sections, consider only single block SPJ queries and do not consider aggregation. In this section we describe how we handle aggregate queries. For queries that contain aggregates, in addition to constructing the canonical MapTable for the various subexpressions that contain SPJ (select, project, join) queries, we make use of the usability criteria described by Srivastava et. al. (D. Srivastava, S. Dar, S. Jagadish, and A. Levy, Answering Queries with Aggregation Using Views, proceedings of the 22.sup.nd International Conference on Very Large Data Bases, September 1996.) to identify equivalent queries on the canonical schemas. We translate the user query on the canonical schema and add an entry into the canonical Map Table. Aggregation queries on the base tables may get transformed into simple SPJ queries on the canonical schema. This becomes important in the next phase. When generating the restructuring-views Map Table, if the query on the canonical schema does not have aggregation, the CS2RV algorithm converts it to a query on the restructuring view. However, if the query on the canonical schema performs aggregation, and if the restructuring-view under consideration is obtained by performing a schema transformation on one or more of the grouping columns, no alternative query is generated using that restructuring-view. Plan alternatives are generated only for those restructuring-views that can be mapped to the canonical schema without requiring any restructuring operation on any of the grouping columns. During plan enumeration, the join enumeration phase remains the same as discussed above for plan enumeration and costing. However, we generate additional plan alternatives when generating plans for the group by operation. The canonical MapTable is consulted and if equivalent queries on the restructuring-views exist, new plan alternatives with queries on the restructuring-views is generated. Again the best query plan is then chosen based on cost. From the above-discussion, it should be readily apparent that the query optimization system and method of the present invention provides the advantages of increased query optimization, especially in multiple data base systems in which tables which are restructuring views are present. In fact, the inventors in a paper, Subramanian, Subbu N., Vankataraman, Shivakumar, Query Optimization Using Restructuring-Views, in Paper # AMERICA 181, November, 1998, document substantial performance gains. The query optimization system is also compatible with and takes advantage of existing technology and may be implemented to be transparent to the user. The present invention may be embodied in other specific forms without departing from its spirit or essential characteristics. The described embodiments are to be considered in all respects only as illustrative and not restrictive. The scope of the invention is, therefore, indicated by the appended claims rather than by the foregoing description. All changes which come within the meaning and range of equivalency of the claims are to be embraced within their scope.
|
Same subclass Same class Consider this |
||||||||||
