System and method for loading a cache with query results

A system and method for avoiding accessing a remote database twice, once during query execution and again to retrieve objects identified by the query, when an application requires objects on which to operate. During query plan generation in response to a request for database objects from an application, a query optimizer inserts cache operators into the candidate plans, and then a cost-benefit analysis is undertaken to identify the best plan. The best plan is then used to execute the query, with the cache operators causing objects identified during query execution to be cached locally to the requesting application as the query is being executed, thereby avoiding requiring the application to access the database after query execution.

BACKGROUND OF THE INVENTION

1. Field of the Invention

The present invention relates to facilitating the efficient querying of remote databases and subsequent use of query results by a querying application program.

2. Description of the Related Art

Relational database systems store large amounts of data, including business data that can be analyzed to support business decisions. For example, a relational database system can be used to store sales data including information on sales by time period, product, product line, geographic area, and so on, which can be usefully presented in response to a query to support a particular business decision. It will readily be appreciated that data can be presented in numerous ways to support a decision, depending on the particular decision (and, hence, query) being made.

Often, to generate information to support a decision or for other reasons an application program is invoked by a user, with the application program in turn querying a database query engine for data objects that the application program will require to support the user's purposes. In response, a query optimizer of the query engine generates an efficient query plan, and then the query engine accesses the database in accordance with the query plan. In executing the query, it is frequently the case that a large amount of data must be processed.

In executing the query plan, the query engine determines which columns from which database tables to read, and then if necessary buffers the columns to support query execution. After query execution, the buffer typically is emptied and a results set is returned to the application program. The results set ordinarily includes identifications of objects, referred to by the shorthand term of “.oid”, that satisfy the query. Using the results set, the application program then requests the objects from the database using the .oid set returned by the query engine. Accordingly, the skilled artisan will recognize that the database is accessed twice for data—once by the query engine, and subsequently by the application program to retrieve the objects identified by the query engine.

The application program and database query engine can be local to the user computer, or they can be implemented by so-called “middleware” with which the user computer communicates via a computer network, such as a local area network (LAN) or wide area network (WAN). In any event, the database itself has in the past been locally accessible to the application program and query engine, rendering the necessity of having to access the database twice somewhat inconsequential, even for data-intensive database query operations.

As recognized by the present invention, however, databases increasingly are remote from query execution and application execution “middleware”. For example, many databases on the world wide web are now accessible via the Internet. Because the data transfer rates of wide area networks such as the Internet are relatively low, accessing remote databases is relatively expensive. Indeed, accessing remote databases twice for a single operation can lead to unacceptably long delays, and in the context of data-intensive query processing and application execution, can be disastrously long. The present invention has recognized the above-noted problem and has provided the solution set forth below.

SUMMARY OF THE INVENTION

A general purpose computer is programmed according to the inventive steps herein to locally cache, during query execution, objects identified as a result of the query execution, for subsequent use of the objects by an application program. The invention can also be embodied as an article of manufacture—a machine component—that is used by a digital processing apparatus and which tangibly embodies a program of instructions that are executable by the digital processing apparatus to execute the present logic. This invention is realized in a critical machine component that causes a digital processing apparatus to perform the inventive method steps herein.

A database is remote from the computer and is accessible thereto, and logic is executable by the computer for receiving a query request and, in response to the query request, accessing the database. The logic also includes retrieving object data from the database to execute a query. During the retrieving act, objects from the database are stored in the local cache, such that the objects in the local cache subsequently can be accessed using the application program.

In a preferred embodiment, the logic executed by the computer includes generating at least one query plan in response to the query request. As intended herein, the query plan includes at least one query execution operator, with the query plan being characterizable by a plan tree defining a top and a bottom. At least one cache operator is then inserted into the query plan. As disclosed in detail below, the cache operator includes a first parameter specifying objects in an input stream to the cache operator to be copied into the cache and a second parameter specifying which data attributes to be passed through to a next operator in the query plan. Preferably, for at least some query plans, a cost and a benefit of including at least one cache operator in the plans is determined. Furthermore, a plan is selected to be executed, based on the cost/benefit analysis.

In one cache operator placement embodiment, in at least one plan a cache operator is placed high in the plan relative to the respective plan tree. In this embodiment, the cache operator is pushed down in the plan relative to the respective plan tree through at least one non-reductive query operator. In another embodiment, a cache operator is placed low in the plan relative to the respective plan tree, and the cache operator is moved up in the plan relative to the respective plan tree through at least one of: a leaf query operator, and a pipelining operator.

In still a third cache operator placement embodiment, the logic includes identifying at least one candidate collection of objects in at least one query plan and then, for at least a portion of a candidate collection, inserting a cache operator for the portion above the plan relative to the respective plan tree. A cost/benefit estimation is then performed, and plans are pruned as a result. As envisioned herein, the cost of a cache operator of a plan is defined to be proportional to a cardinality of an input stream to the plan. In contrast, the benefit of a cache operator of a plan is defined to be proportional to a minimum of: the cardinality, an output having a most selective local predicate, and an output having a most selective join predicate.

In another aspect, a computer-implemented method includes receiving, from a local application program, a query request for objects stored in a remote database. A local cache is accessible to the local application program. The method also includes storing objects in the local cache while executing a query in response to the query request. Moreover, the method includes accessing the objects in the local cache using the application program. With this inventive method, the application program is not required to access the database to retrieve the objects after query execution.

In still another aspect, a computer program device includes a computer program storage device readable by a digital processing apparatus, and a program on the program storage device. The program includes instructions that can be executed by the digital processing apparatus for performing method acts for caching objects in accordance with query results during query execution. As set forth further below, the program includes computer readable code means for receiving a request for objects from an application program at a local computer site, it being understood that the objects are stored in a database remote from the local site. Computer readable code means generate at least one query plan to identify objects in the database satisfying the request, and then means execute the query plan. Additionally, code means cause at least some objects identified by the means for executing to be copied into a cache at the local computer site contemporaneously with the executing step undertaken by the means for executing. Consequently, the application program can access the objects in the cache without accessing the database after the query plan is executed.

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS

Referring initially toFIG. 1, a system is shown, generally designated10, for caching query results from a database12during query execution for subsequent use of the results by an application program13that is remote from the database12and that is accessible by a client or user computer14. The database12may reside, for example, in a Web server12A or other location remote from the user computer14and be accessible via a wide area network, such as but not limited to the Internet. In one intended embodiment, the computer14may be a personal computer made by International Business Machines Corporation (IBM) of Armonk, N.Y., or the computer14may be any computer, including computers sold under trademarks such as AS400, with accompanying IBM Network Stations. Or, the computer14may be a Unix computer, or OS/2 server, or Windows NT server, or IBM RS/6000 250 workstation or an IBM laptop computer.

As shown inFIG. 1, the computer14is electrically connected to one or more input devices16, e.g., a mouse or keyboard, which can be manipulated by a user of the system10to generate requests for the application program13to execute a program requiring data objects from the database12. The results of the application execution can be output via an output device20such as a printer or monitor that are conventionally coupled to the computer14.

In accordance with the present invention, associated with the application program13is a query engine22with query optimizer24for determining how to efficiently retrieve the requested data, based on user-defined predicates. In accordance with the present logic, the query optimizer24generates candidate query plans in response to a request for database objects from the application program13, and then evaluates the candidates to select a best plan. The best plan is then used by the query engine22to execute the query on the database12. As intended herein, during query execution objects from the database12that satisfy the query are copied into a cache26, for access and use of the objects by the application program13.

As shown inFIG. 1, in one intended embodiment the application program13, cache26, and query engine22are hosted on a single local site, such as a so-called “middleware” computer28or on a server computer, with the client computer14being separate from but in communication with the middleware computer28. Alternatively, the client computer14can be integrated into the middleware computer28. In any case, the database12is remote from the application program13, query engine22, and cache26as shown.

With the above overview of the present architecture in mind, it is to be understood that the present logic is executed on the architecture shown inFIG. 1in accordance with the flow charts discussed below. The flow charts herein illustrate the structure of the logic of the present invention as embodied in computer program software. Those skilled in the art will appreciate that the flow charts illustrate the structures of logic elements, such as computer program code elements or electronic logic circuits, that function according to this invention. Manifestly, the invention is practiced in its essential embodiment by a machine component that renders the logic elements in a form that instructs a digital processing apparatus (that is, a computer) to perform a sequence of function steps corresponding to those shown.

In other words, the logic may be embodied by a computer program such as the application program13and/or query engine22with optimizer24that are executed by a processor within the computer14as a series of computer-executable instructions. These instructions may reside, for example, in RAM of the computer14or on a hard drive30or optical drive of the computer14, or the instructions may be stored on a DASD array, magnetic tape, electronic read-only memory, or other appropriate data storage device. In an illustrative embodiment of the invention, the computer-executable instructions may be lines of compiled C++compatible code.

Now referring toFIG. 2, the overall steps of the present logic can be seen. Commencing at block32, a query is received by the query engine22from the application program13in response to a user desire for information as indicated by appropriate input from the input device16. Then, at block34the query optimizer24generates candidate query plans with cache operators placed in the plans in accordance with the logic disclosed below. Proceeding to block36, a DO loop is entered for the candidate query plans. At block38of the DO loop, a cost and benefit of each plan is determined, and at block40the plan under test is pruned as necessary based on the cost/benefit determination at block38.

If it is determined at decision diamond42that the last candidate plan has not been tested, the DO loop continues by looping back to block38for the next plan. Otherwise, the DO loop exits to block44, wherein the query is executed in accordance with the best one of the plans as determined by the cost/benefit analysis of block38. This cost/benefit analysis can entail conventional cost/benefit analysis undertaken by query optimizers as well as the cache operator analysis shown further below.

During execution of the query, objects that satisfy the query are stored in the local cache26, and are not automatically discarded after the query results (typically, a list of objects by oid) are generated, in contrast to conventional database query buffering operations. Consequently, the logic can move to block46to operate on the objects using the application13, without resorting to a second (i.e., post-query) access of the remote database12.

FIGS. 3-6show the details of three alternative methods used by the query optimizer24for inserting cache operators into query plans to cause objects to be copied to the local cache26during query execution. Starting at block48ofFIG. 3, the select clause of the query is rewritten to replace all occurrences of “oid” (“object identification”) with a filler asterisk that indicates that all columns of that collection of objects should be selected. This recognizes that, since most applications generally require entire objects, caching only portions of objects is not as useful as caching entire objects. The query is optimized conventionally at block50.

Proceeding to block52, cache operators are generated for each candidate collection in the plan. By “candidate collection” is meant a collection of objects the oids of which are returned as part of the query result, i.e., objects whose oid columns are part of the query's SELECT clause.

In accordance with the present invention, each cache operator generated at block52has two parameters. One parameter specifies which objects of the input stream (i.e., the data input to the cache operator based on the position of the cache operator in the plan) to copy into the local cache26. A second parameter of a cache operator specifies which columns of the input stream to the operator should be passed through to the next operator in the plan, i.e., which columns should not be projected out of the plan. Accordingly, while a cache operator causes entire objects to be copied to cache, the cache operator, for query execution purposes, also identifies which columns of objects are needed for the next query operator in the query plan.

Proceeding from block52to block54, in the embodiment shown inFIG. 3, cache operators, after being generated, are placed high in a candidate query plan at block54. To better understand the logic ofFIG. 3, brief reference is made toFIG. 8, which shows a graph of an exemplary query plan. As shown, the graph can be represented in a tree-like structure having bottom-most leaf nodes, designated R and S1, query operators such as the labelled joins, and cache operators such as the cache operator labelled “cache (R)”, with the graph defining a top “return” node. In the example shown, the nodes R, S1, and S2represent tables of data, the connecting lines represent join predicates, and the cache operator labelled “cache (R)” represents an operator for caching objects from the table R. Accordingly, in the example shown and in accordance with the above disclosure, the cache operator has been inserted at the location shown after having determined that objects from the table R constitute a candidate collection.

After placing the cache operators in the candidate plan as discussed, the query optimizer24moves to block56to push cache operators down through the tree-like structure of the query graph below non-reductive operators. Cache operators are not pushed down below reductive operators, however. As intended by the present invention, a non-reductive operator is an operator that does not filter out any objects, such as a sort operator and certain functional joins for which integrity constraints guarantee that all objects satisfy the join predicate. A formal definition of non-reductive predicates is set forth in Carey et al., “On Saying Enough Already in SQL”,Proc. of the ACM SIGMOD Conf. on Management of Data, pp. 219-230, May, 1997.

It is to be appreciated that the logic shown inFIG. 3is based on the principle that all relevant objects should be cached and no irrelevant objects should be cached. Further, it is to be appreciated that the push-down step of block56reduces the cost of the query. For example, suppose a cache operator is pushed down below a sort query operator. The cost of the sort is reduced because the sort operator works on “thin” tuples, i.e., tuples having had projected out (by the cache operator) those columns that are not needed for the sort but that might have been added as a result of the rewrite step at block48. At the same time, no irrelevant objects are copied into the cache.

FIG. 4shows a second cache operator placement method. At block58, query plans are optimized conventionally, and then at block60, a DO loop is entered for each leaf node of the plan. At decision diamond62of the DO loop, it is determined whether a leaf operator accesses a candidate collection. If it does, the logic moves to block64to expand the list of attributes returned from the candidate collection to include the attributes of all objects, recalling that it is desirable to cache entire objects, not just parts of them. Next, at block66a cache operator is placed just above the leaf operator under test.

From block66, or from decision diamond62if the leaf operator under test does not access a candidate collection, the logic moves to decision diamond68to determine whether the DO loop is at an end. If it is not the logic loops back to decision diamond62to process the next leaf node. In contrast, after all leaf nodes have been processed the logic exits the DO loop at block70, wherein cache operators inserted at block66are pulled up through the tree-like query graph past so-called “pipelining” operators such as filters or nested-loop joins to thereby reduce the number of false cache insertions without increasing the cost of the query.

As understood herein, while bothFIGS. 3 and 4illustrate useful logic, the logic ofFIGS. 3 and 4assume that the extra cost incurred by plans with cache operators is always outweighed by the benefits of caching for future application use. The present invention observes that under some circumstances, the logic shown inFIGS. 5-7can improve system performance by estimating costs and benefits for various cache operator placement proposals generated by the query optimizer24.

Commencing at block72inFIG. 5, the query optimizer24is modified for undertaking the logic shown inFIGS. 6 and 7as follows. As an example, assume that query optimizer24is modified from a so-called bottom-up dynamic programming optimizer as described in, e.g., Selinger et al., “Access Path Selection in a Relational Database System”,Proc. of the ACM SIGMOD Conf. on Management of Data, pp. 23-34, May 1979. Such an optimizer generates candidate query plans in three phases, first by planning for single collection accesses, second by planning for joins, and then by adding operators for aggregation, ordering, unions, etc. Along the way a set of plan properties is kept recording what has been done to generate the plan. At the end of each of the above-described phases the optimizer prunes, i.e., eliminates, all but the least expensive plans.

With the above in mind, at block72the effect of cache operators on a plan's properties is defined in accordance with the above-disclosed cache operator parameters. Next, at blocks74and76the above-mentioned first and second phases are respectively modified such that some alternate plans are generated at each phase with cache operators. In some dynamic programming optimizers, each of the steps at blocks74and76require adding a rule to the optimizer. Also, in the modification to the access phase at block74the new rule generates some plans for getting all attributes of the objects in the collection (thereby rendering so-called “thick” plans) if the collection is one whose oid column is selected by the query, i.e., is a candidate collection. And, the rule generates extra plans having a cache operator placed above each thick plan. Likewise, in the join planning phase modification at block76, cache operators are caused to be added on top of each round of joining.

It will readily be appreciated that many candidate plans having cache operators, in addition to conventionally-generated plans having no cache operators, can be generated by the optimizer24as a result of the above modifications. Accordingly, at block78the optimizer24is modified to undertake the below-described cost/benefit analysis for caching plans, in addition to its conventional cost/benefit analysis. Next, at block80the optimizer24is modified to prune expensive plans generated by the above process.

FIG. 6shows the logic by which an optimizer modified in accordance withFIG. 5can insert cache operators into query plans. Commencing at block82, candidate query plans are conventionally generated and a DO loop is entered for each. Proceeding to decision diamond84, for the plan under test it is determined whether any collection of the plan is a candidate collection. If it is, the logic moves to block86to enter a nested DO loop for all subsets of candidate collections. Proceeding to block88, an alternate plan is generated having all attributes of the subset. From block88the logic moves to block90to place a cache operator for each subset above the alternate plan. Then, a cost/benefit analysis is undertaken at block92in accordance with the logic shown in FIG.7.

After determining costs and benefits of the alternate plans, the logic moves to block94to prune expensive plans, if possible. In accordance with present principles, a plan is pruned if it is at least as expensive as some other plan that has equivalent or more general properties, with the survivors thus being either alternative (caching) plans or non-caching original plan. Ordinarily, a thick plan will be more general than a thin plan (and hence not be pruned in favor a similarly expensive thin plan), but under the following conditions a thick plan can be pruned in favor of a thin plan:Costthin≦Costthick+Costcachebest−Benefit, where
Costthin=cost of the thin plan, Costthick=cost of the thick plan, Costcachebest=minimum actual cost incurred to cache a collection, corresponding to the case wherein a cache operator is located directly above that join that results in the minimum number of output tuples from the collection, and Benefit is the benefit of caching as defined below. The above condition essentially represents the situation wherein the minimal possible cost for caching is more than the cost of the thin plan. Accordingly, under these conditions there is no point in keeping the thick plan, because caching is not a cost-effective operation.

Decision diamond96represents a test for determining whether the last subset of the candidate collections of the plan under test has been tested, and if not, the nested DO loop returns to block88with the next subset. When the nested DO loop for the plan under test is completed, the logic moves from decision diamond96to decision diamond98to determine whether the DO loop for the plans has been completed. If not, the DO loop returns to decision diamond84with the next plan; otherwise, the logic ends at state100.

Now referring toFIG. 7, the logic by which a cost/benefit analysis of a caching plan is made can be seen. Commencing at block102the cost of each cache operator in the plan is set to be proportional to the cardinality of the input to that cache operator, representing the time to copy objects into cache and project columns from the output stream.

In contrast, estimating the benefit of the cache operators of a plan is somewhat more involved. At block104of the preferred embodiment the effect of applying local predicates to base conditions is estimated using conventional optimizer formulae. Proceeding to block106, an inner is chosen for each join in the plan and then the optimizer24“applies” the join predicate to the inner join's output. Importantly, the optimizer models the joins and does not actually build plans.

From block106, the logic moves to block108to set a variable “O”, representing the output of each candidate collection for the query, to be equal to the minimum of one of three values: the initial cardinality of the input stream, the output of a collection after applying the most selective local predicate (i.e., non-join predicates that are specific to a particular collection), and the output of the collection after applying the most selective join predicate. Next, at block110the benefit is set equal to the product of O, F, and k, where F=an assumed fraction of objects in the query result (e.g., 80%), and k represents the time to fault in a non-cached object to the cache26from the database12. The costs and benefits of the cache operators determined above are then added to conventionally-determined plan costs and benefits to find the best plan.