Database system

Abstract: A database system holds data in the form of a sequence of records, each record comprising one or more fields. The database can be interrogated by a search query, which specifies a particular logical combination of comparisons to be performed on specified fields of each record. Before the search commences, the search query is compiled to produce an optimised sequence of search code. Each comparison operation is assigned a cost, reflecting the cost in time to retrieve the required fields and to perform the comparisons, and is also assigned a probability, indicating the probability that the comparison will produce a true result. Each logical operation in the search query is then processed, to find the order of handling its arguments that gives the minimum expected cost, and the arguments are re-arranged into that order.

Description: Background to the Invention This invention relates to database systems. More specifically, the invention is concerned with a method and apparatus for handling queries in such a system. In general, a database system stores data in the form of records, each record consisting of a number of fields. One way of accessing data in such a system is to search the database for records that match a specified search query. The search query may consist of a particular logical combination of comparison operations to be performed on specified fields of each record, a record being retrieved only if the logical combination results in a "true" value for that record. A problem with such an arrangement is that, if a query is very complex, each record will take a long time to process, and hence the execution of the search queries will be very slow. US-A-4829427 discloses an optimiser and code generator which includes an implementation dependent scan analyzer and an implementation-independent optimizer and code generator. The scan analyzer receives data concerning the query from the optimizer and code generator and responds by providing cost data about the cost of various alternative methods of accessing data and with implementation-dependent portions of the compiled query code. The optimizer and code generator uses the cost data to optimize the query and incorporates the implementation-dependent portions into the compiled query code which it produces. S Hiyoshi et al "Hierarchical Optimization Strategy for Query Evolution", NEC Research and Development, No 72, January 1984, Tokyo, JP, pages 48-55, teaches that access path selection methods in the practical relational database management system can be classified into two categories. The first category assigns priority to the tuple access mechanism and the query condition form and selects an access path according to the priorities, the first category being able to select an access path with low overhead time. The second category estimates access path cost from database statistics and query characteristics and selects minimum cost path, the second category being able to select a low cost access path. However, neither of these two documents teaches any solution to the above-mentioned problem of speeding up the time taken to process each individual record. The object of the present invention is to alleviate this problem. Summary of the Invention According to the invention there is provided a database system as defined in Claim 1 comprising means (12) for storing a database comprising a sequence of records, each record comprising one or more fields, means (11) for interrogating the database with a search query comprising a logical combination of comparisons to be performed on each record, and means (20) for creating a syntax tree of said search query, the syntax tree consisting of a set of nodes interconnected by branches, each node representing either a logical operation (AND, OR) or a comparison; characterised in that said database system further comprises: (i) means for assigning a cost value and a probability value to each node in said syntax tree, wherein the cost value of a comparison node is equal to the cost of accessing the required field or fields for the comparison, plus the cost of executing the comparison, the probability value of a comparison node is an estimate of the probability that the comparison will yield a true result, this estimate being made on the basis of past experience or having a default value, the cost value of a logic node is an expected cost, derived from the cost values and the probability values of the branches of that node and from the order in which the branches are evaluated, and the probability value of a logic node is derived from the probability values of the branches of that node; (ii) and means for rearranging the order of the branches of each logic node in said syntax tree, using the cost and probability values of the branches of that logic node, to minimise the cost value for that logic node. It can be seen that by optimizing the search query in this way, the processing time for each record is reduced, and hence the overall execution speed of the query is increased. It should be noted that the optimization of the search query is performed once only, for example as part of a query compilation process before the search is actually executed. Hence, the optimization process does not incur any cost at run time, when the search is executed. Brief Description of the Drawings Figure 1 shows a data processing system embodying the invention. Figure 2 is a flow chart of a compiler for optimizing a search query. Figure 3 is a syntax tree representing a search query. Figure 4 shows a compiler subroutine in greater detail. Description of an Embodiment of the Invention One data processing system in accordance with the invention will now be described by way of example with reference to the accompanying drawings. Overview of the system Referring to Figure 1, the data processing system comprises a host computer 10 and a search processor 11. The search processor is connected to a number of disc drive units 12 by way of a bus 13. The disc drive units 12 hold a relational database comprising one or more files. Each file consists of a sequence of records, each comprising one or more fields. Each field represents either a numerical value or text, and may be of fixed length or variable length. In the case of a fixed length field, the length of the field is the same in all records, whereas in the case of a variable length field it may vary from record to record. The length of the variable length field is computable, for example from a length code at the start of the field. In operation, the host computer 10 performs the main processing workload of the system, consisting of one or more applications programs 14. An application program may generate search queries, specifying searches to be performed on the data base. Each search query is processed by means of a query compiler 15, to generate a sequence of search instructions which are passed to the search processor 11 for execution. A search query consists of a logical combination of comparison operations to be performed on each record in a file. For example, a query may be in the form ((C1 AND C2) OR C3) AND (C4 OR C5) where C1 -- C5 represent individual comparison operations. Each comparison may involve comparing a specified field of the record with a predetermined search key, or may involve comparing two different fields within the record. Query compiler Referring now to Figure 2, this shows the query compiler 15 in greater detail. The first step 20 of the query compiler is to analyse the query, to create a syntax tree, representing the logical structure of the query. For example, the query ((C1 AND C2) OR C3) AND (C4 OR C5) would produce the tree structure as shown in Figure 3. The syntax tree consists of a set of nodes, interconnected by branches. Each node represents either a comparison (C1, C2 etc) or a logical operation (AND, OR). Each node is represented by the compiler as a data structure containing the following items: OPERATOR TYPE (AND, OR, COMPARISON) NUMBER OF BRANCHES (i.e. sub-nodes) LEFTMOST BRANCH (pointer to sub-node) OTHER BRANCHES (pointers to sub-nodes) The next step 21 of the query compiler is to call a SET COSTS subroutine. The set costs subroutine assigns a cost and a probability to each node. The cost assigned to a node is an estimate of the expected cost (in processing time) of establishing whether that node will yield a true or false result. The probability assigned to a node is the probability of a true result. As will be described in greater detail later, the cost of a logic node (AND, OR) depends on the costs and probabilities of its branches and on the order in which the branches are evaluated. The SET COSTS subroutine sorts the branches into the order that gives the least cost, rearranging the pointers in the syntax tree if necessary to ensure that the branches are evaluated in this order. (It is assumed that the branches of a logic node are always evaluated in order, starting from the leftmost branch). If a node has n branches, then there are n! ways of ordering the branches, and the SET COSTS subroutine determines the optimum permutation. The next step 22 of the query compiler is to call a GEN CODE subroutine. This subroutine generates instructions for fetching the specified field or fields and performing the specified comparisons. It also inserts JUMP instructions so as to link these comparisons together in the specified logical structure. For example, the instructions generated to implement the syntax tree structure shown in Figure 3 are of the following form. C1 IF FALSE GO TO LABEL : a C2 IF TRUE GO TO LABEL : b :a C3 IF FALSE GO TO LABEL : d :b C4 IF TRUE GO TO LABEL : c C5 IF FALSE GO TO LABEL : d :c RETURN "TRUE" :d RETURN "FALSE" Compiler routines for generating code for performing logical combinations of operations are known as such, and so it is not necessary to describe the GEN CODE subroutine in any further detail. SET COSTS Subroutine Referring now to Figure 4, this shows the SET COSTS Subroutine in greater detail. This is initially called in respect of the root node, and then calls itself recursively, so as to scan the whole of the syntax tree, so as to process each node in the tree. (40). The first action is to test the node type and to branch according to whether it is a comparison node or a logic node. (41). In the case of a comparison node, the next step is to assign a cost value and a probability value to the node. The cost of a comparison node is equal to the cost of accessing the required field(s) for the comparison, plus the cost of executing the comparison. The cost of accessing a field depends on the structure of the record. In particular, it generally takes longer to access a field that is preceded by one or more variable length fields, since it is necessary to access all those preceding fields in sequence on order to locate the start of the required field. In general, the cost of accessing a field can be estimated by a + nb where a and b are constants and n is the number of preceding variable length fields. The cost of executing a comparison depends on the comparison type. Simple comparisons such as "equals" "greater than" and "less than" can be performed rapidly and hence are assigned low cost values. More complex string comparisons such as "contains" take much longer and hence are assigned relatively higher cost values. The probability value assigned to the comparison node is an estimate of the probability that the comparison will yield a "true" result. This estimate is made on the basis of past experience of the outcomes of comparisons. In the absence of any information about previous outcomes, predetermined default values are used. Typical values are as follows: Comparison type Probability Not equal 0.99 Greater than 0.5 Greater than or equal 0.5 Less than 0.5 Less than or equal 0.5 Equal 0.01 Starts with 0.01 Contains 0.1 NOT contains 0.9 Null 0.05 NOT null 0.95 Between 0.05 In set 0.25 (42). If the node is a logic node, the next step in the SET COSTS routine is to make a recursive call to itself, for each branch from the node. This will result in each of the sub-nodes being processed by SET COSTS, so as to assign costs and probabilities to those sub-nodes. It will be appreciated that if any of the sub-nodes is itself a logic node, this will lead to further nested calls to SET COSTS, and so on, until a comparison node is reached. (43). When all the branches of the logic node have been processed, the branches of the node are re-arranged to minimise the cost of the logic node, and cost and probability values are assigned. For a logic node, the cost depends on the costs and probabilities of its branches, and on the order in which those branches are evaluated. For simplicity, the case where there are only two branches will be considered here. The extension of three or more branches will be clear. AND Nodes Consider an AND node having two branches with costs c1, c2 and probabilities p1, p2. If branch 1 is evaluated first, then the expected cost is c1 + p1. c2 whereas is branch 2 is evaluated first, the expected cost is c2 + p2. c1 The SET COSTS routine determines which of these two possible orders of evaluation gives the least cost and, if necessary, swaps over the pointers in the syntax tree structure, so that the branch with the least cost becomes the leftmost branch and hence is evaluated first. The cost assigned to the node is this least cost value. Assuming that the results of the comparisons are statistically independent, the probability assigned to the AND node is p1. p2. OR node Consider an OR node having two branches with costs c1, c2 and probabilities p1, p2. If branch 1 is evaluated first, the expected cost is: c1 + (1 - p1) c2. if, on the other hand, branch 2 is evaluated first, the expected cost is: c2 + (1 - p2) c1. As before, SET COSTS determines the order of evaluation which gives the least cost, and swaps over the pointers if necessary, to ensure that the least cost branch is evaluated first. Assuming the comparisons are statistically independent, the probability assigned to the OR node is: p1 + p2 - p1. p2.

Claims: A database system comprising means (12) for storing a database comprising a sequence of records, each record comprising one or more fields, means (11) for interrogating the database with a search query comprising a logical combination of comparisons to be performed on each record, and means (20) for creating a syntax tree of said search query, the syntax tree consisting of a set of nodes interconnected by branches, each node representing either a logical operation (AND, OR) or a comparison; characterised in that said database system further comprises: (i) means for assigning a cost value and a probability value to each node in said syntax tree, wherein the cost value of a comparison node is equal to the cost of accessing the required field or fields for the comparison, plus the cost of executing the comparison, the probability value of a comparison node is an estimate of the probability that the comparison will yield a true result, this estimate being made on the basis of past experience or having a default value, the cost value of a logic node is an expected cost, derived from the cost values and the probability values of the branches of that node and from the order in which the branches are evaluated, and the probability value of a logic node is derived from the probability values of the branches of that node; (ii) and means for rearranging the order of the branches of each logic node in said syntax tree, using the cost and probability values of the branches of that logic node, to minimise the cost value for that logic node. A database system as defined in claim 1 wherein the cost value c3 and probability value p3 for an AND logic node whose first branch, which is evaluated first, has a cost value cl and a probability value pl and whose second branch, which is evaluated second, has a cost value c2 and a probability value p2 is such that c3 = c1 + p1 . c2 p3 = p1 . p2 and the cost value c4 and probability value p4 for an OR logic node whose first branch, which is evaluated first, has a cost value cl and a probability value pl and whose second branch, which is evaluated second, has a cost value c2 and a probability value p2 is such that c4 = c1 + (1 - p1) . c2 p4 = p1 + p2 - p1 . p2 . A database system according to either preceding claim further including means (22) for using the rearranged syntax tree, to generate instructions for processing each said record by fetching the required field or fields of the record and performing said comparisons in the order specified by the rearranged syntax tree.

Inventors: BROWN ANTHONY PETER GRAHAM, BROWN, ANTHONY PETER GRAHAM
Applicants: INT COMPUTERS LTD, INTERNATIONAL COMPUTERS LIMITED
Application year: 1990
Main classification: G06F17
IPC codes: G06F17, G06F12