Typical database systems receive queries to retrieve information from data sources managed by the database system. In a relational database system these data sources are typically organized into a series of tables. Queries are received in a standard format such as SQL.
Most databases use an optimizer that attempts to generate an optimal query execution plan. Execution of a query uses system resources such as for example CPU usage and I/O count. Many optimizers generate an optimal query execution plan on the assumption that all resources of the database system are available during query execution time.
Many times this assumption is incorrect, especially for queries running in a production data warehouse environment. The optimal plan for these systems depends on the current workload of the system. This workload, especially during busy times of the processing day, typically is resource constrained at one of the major resources. Since these resource constraints can change, the assumption of a query having uncontested access to all the available resources is often incorrect.
Depending on the availability of the resources, different plans can produce significantly better execution times and overall system throughput.
Described below is a technique for generating two or more execution plans for an SQL query within a database system. The system has two or more resources. A first resource utilization profile is defined by associating a first set of numerical utilization values respectively with two or more of the resources. The utilization values represent utilization of the resources. A first execution plan is generated that is optimal assuming utilization of the resources specified in the first resource utilization profile. The technique defines at least one further resource utilization profile by associating at least one further set of numerical utilization values respectively with two or more of the resources, the further utilization values representing utilization of the resources. At least one further execution plan is generated that is optimal assuming utilization of the resources specified in the further resource utilization profile(s).
Also described below is a method of selecting for use a stored execution plan for an SQL query within a database system. The system has two or more resources. Respective current resource utilization values are defined for two or more of the resources. The resource utilization values represent utilization of the resources. A plurality of execution plans are maintained for the SQL query. The plans have associated plan resource utilization values. The current resource utilization values are compared with the plan resource utilization values. One of the maintained execution plans is selected based at least partly on the result of the comparison.
Other types of database systems, such as object-relational database management systems (ORDMS) or those built on symmetric multi-processing (SMP) platforms are also suited for use here.
The data warehouse 100 includes one or more processing modules 1051 . . . N that manage the storage and retrieval of data in data-storage facilities 1101 . . . N. Each of the processing modules 1051 . . . N manages a portion of a database that is stored in a corresponding one of the data-storage facilities 1101 . . . N. Each of the data-storage facilities 1101 . . . N includes one or more disk drives.
The system stores data in one or more tables in the data-storage facilities 1101 . . . N. The rows 1151 . . . Z of the tables are stored across multiple data-storage facilities 1101 . . . N to ensure that the system workload is distributed evenly across the processing modules 1051 . . . N. A parsing engine 120 organizes the storage of data and the distribution of table rows 1151 . . . Z among the processing modules 1051 . . . N. The parsing engine 120 also coordinates the retrieval of data from the data-storage facilities 1101 . . . N over network 125 in response to queries received from a user at a mainframe 130 or a client computer 135 connected to a network 140. The database system 100 usually receives queries and commands to build tables in a standard format, such as SQL.
In one example system shown in
As illustrated in
Finally, the parser 205 runs an optimizer (block 320). Optimizer 320 attempts to generate an optimal query execution plan. Whether or not a plan is optimal will depend on the current consumption of two or more resources included in the database system. One of these resources is CPU usage, another resource is available memory, another resource is network usage and a further resource is I/O count.
As will be described below, the optimizer generates two or more different execution plans. Each of the plans is optimal given different resource utilization profiles. Each resource utilization profile defines a different set of actual expected run time resource availabilities.
The optimizer generates different plans, each plan appropriate for a different set of actual run time resource availabilities. The appropriate plan is chosen for execution by considering the actual resources available at the instance when the plan is ready to be dispatched for execution.
One example is where an optimizer considers memory as one important resource. The optimizer would generate two different plans, plan A and plan B.
Plan A is generated by the optimizer assuming that x MB of memory is available. Plan B is also generated by the optimizer assuming that less than x MB of memory is available.
The decision to use plan A or plan B is made at execution time using a rule such as the following:
if there is x MB of memory available then use plan A else use plan B.
In practice a complicated system will have two or more and usually several resources under consideration. The optimizer defines a first resource utilization profile that associates a first set of numerical utilization values respectively with two or more of the resources, the utilization values representing utilization of the resources. The optimizer also defines at least one further resource utilization profile by associating at least one further set of numerical utilization values respectively with two or more of the resources, the further utilization values representing utilization of the resources.
Where there are several utilization profiles, one preferred form technique of the optimizer is to generate a decision matrix. At run time, the plan closest to the actual current system resource profile is chosen from the matrix. An important feature of the technique is that the choice as to which plan to select is made at the time of execution. This is especially important in systems where the parsing and execution of queries occurs at different points in time. This is true of almost all systems that parse and optimize a query to determine an execution mechanism.
In one technique, each resource of the database system is assigned a numerical utilization value.
As shown in
A utilization value of 10 in the matrix 500 indicates an assumption that ten percent of the resource is available. Similarly, a value of 30 indicates 30 percent of resource available. A value of 100 indicates that 100 percent of the resource is available.
In matrix 500, plan A would be chosen when the run time system is heavily CPU bound. Where there is heavy CPU usage, a plan that assumes only ten percent of the CPU resource is available would be preferred over other plans that assume a greater percentage of CPU resource is available.
Similarly, plan C shown in matrix 500 would be chosen at times when the run time system is heavily I/O bound as plan C assumes only ten percent of I/O is available. Plan B in matrix 500 would be chosen when the system resource use is balanced. This assumes that each of resources 505, 510, 515 and 520 are each approximately fifty percent utilized.
The numerical utilization values making up each resource profile could be generated by the optimizer or could be user defined. For example, in one form the optimizer generated profiles are based on the optimizer's assumption of resource availabilities, while performing the query optimization. The user can override these values, for cases where certain types of plans are preferred. This could be the case for low priority queries that should not utilize too much of a known bottleneck resource.
In some cases different resource profiles will generate the same plan. This will happen more often with simple queries where there are fewer choices of plans.
Once the plans have been defined it is then necessary to select one of the stored execution plans to satisfy the SQL query. The choice of plan is made at run time. The first step is to define respective current resource utilization values for two or more of the resources.
In one technique, the current utilization of one or more of the resources is computed for the past n seconds. The value of n is called the SYSTEM REACTION TIME (SRI). The value of n is predefined and is small when it is desired to have a fast reacting run time system. Alternatively the value of n can be selected as a large number for a slow reacting run time system. It is envisaged that the value of n is initially set at a default value of between 1 second and 60 seconds. The value is able to be adjusted by a system administrator.
A resource index value (RVI) is then assigned to one or more of the resources. For example, if the utilization value of a resource is between 0 and 10 percent during the system reaction time (SRT), then that resource is assigned a resource index value (RVI) of 100. If the utilization value is between 91 and 100 during the system reaction time (SRT) then that resource is assigned a resource index value (RVI) of 10.
It will be clear from
The next step is to compare the current resource utilization values, for example the RVI values, with the plan resource utilization values shown and described above. One example of the comparison step includes a calculation such as:
In the equation above, RUV1 is the resource utilization value for a first resource in the matrix and RVI1 is the calculated resource index value for that first resource. The respective absolute differences between the respective RVI and RUV values are calculated and summed to generate a cost for an individual plan.
One of the plans is then selected based at least partly on this comparison between resource index values and resource utilization values. In one example the plan with the smallest calculated cost from the equation above is chosen for execution. This is based on an assumption that all resources forming part of the calculation are equally important.
Depending on the amount of the available optimization time, the optimizer is able to generate multiple plans for evaluation at run time, each plan generated assuming a different resource utilization profile.
Assuming a decision matrix such as that shown in
The cost or sum of each of plans A, B and C is calculated as follows:
The calculated cost for plan A is 70, the calculated cost for plan B is 140 and the calculated cost for plan C is 250. Since the sum for plan A is the lowest, this plan will generally be selected as the plan to execute given the current resource utilization shown in
It will be appreciated that in a variation of the above technique a low utilization value could instead signal low utilization and hence high availability. A high utilization value would signal high utilization and hence low availability. RVI values would have an inverse relationship with utilization.
The text above describes one or more specific embodiments of a broader invention. The invention also is carried out in a variety of alternative embodiments and thus is not limited to those described here. Those other embodiments are also within the scope of the following claims.