Method and Apparatus for Determining SQL Execution Plan
20170242884 · 2017-08-24
Inventors
Cpc classification
International classification
Abstract
A method and an apparatus for determining a structured query language (SQL) execution plan are provided to optimize determining of the SQL execution plan and improve execution efficiency of the SQL execution plan. The SQL execution plan corresponds to at least one relation table. During an N.sup.th iteration, the method includes obtaining a first iteration parameter generated after a first plan tree is executed on the at least one relation table during an (N−1).sup.th iteration, where N is a natural number greater than 1, establishing a second plan tree according to the first iteration parameter, and determining the first plan tree or the second plan tree as the SQL execution plan when a difference between the second plan tree and the first plan tree is not greater than a first threshold.
Claims
1. A method for determining a structured query language (SQL) execution plan, wherein the SQL execution plan corresponds to at least one relation table, and wherein during an N.sup.th iteration, the method comprises: obtaining a first iteration parameter generated after a first plan tree is executed on the at least one relation table during an (N−1).sup.th iteration, wherein N is a natural number greater than 1; establishing a second plan tree according to the first iteration parameter; and determining the first plan tree or the second plan tree as the SQL execution plan when a difference between the second plan tree and the first plan tree is not greater than a first threshold.
2. The method according to claim 1, wherein the first threshold is 0, and wherein determining the first plan tree or the second plan tree as the SQL execution plan comprises determining the first plan tree or the second plan tree as the SQL execution plan when the second plan tree is the same as the first plan tree.
3. The method according to claim 1, wherein the first iteration parameter comprises relation tuples in any one or more relation tables corresponding to the SQL execution plan.
4. The method according to claim 1, wherein when the SQL execution plan corresponds to at least two relation tables, the first iteration parameter further comprises relation tuples in a result set obtained after a join operation is performed on any one or more relation tables in the at least two relation tables.
5. The method according to claim 1, wherein after determining the first plan tree or the second plan tree as the SQL execution plan, the method further comprises recording a state of the SQL execution plan as a determined state.
6. The method according to claim 1, wherein after establishing the second plan tree, the method further comprises: executing the second plan tree; and recording a second iteration parameter generated when the second plan tree is executed.
7. The method according to claim 6, wherein the second iteration parameter comprises relation tuples in any one or more relation tables corresponding to the SQL execution plan.
8. The method according to claim 6, wherein when the SQL execution plan corresponds to at least two relation tables, the second iteration parameter further comprises relation tuples in a result set obtained after a join operation is performed on any one or more relation tables in the at least two relation tables.
9. The method according to claim 6, further comprising: updating the first iteration parameter according to the second iteration parameter when the difference between the second plan tree and the first plan tree is greater than the first threshold; and setting the second plan tree during the N.sup.th iteration as a first plan tree during an (N+1).sup.th iteration.
10. The method according to claim 9, wherein before updating the first iteration parameter, the method further comprises establishing the second plan tree different from the first plan tree according to the first iteration parameter when the second iteration parameter is the same as the first iteration parameter.
11. The method according to claim 10, wherein after establishing the second plan tree different from the first plan tree, the method further comprises: recording an execution time for executing the second plan tree; and setting the second plan tree with shortest execution time during a plurality of N iterations as the SQL execution plan when N is greater than a second threshold.
12. The method according to claim 11, wherein after setting the second plan tree with the shortest execution time during the plurality of N iterations as the SQL execution plan, the method further comprises recording a state of the SQL execution plan as a determined state.
13. The method according to claim 9, wherein before updating the first iteration parameter, the method further comprises: recording an execution time for executing the second plan tree; and setting the second plan tree with shortest execution time during a plurality of N iterations as the SQL execution plan when N is greater than a third threshold.
14. The method according to claim 13, wherein after setting the second plan tree with the shortest execution time during the plurality of N iterations as the SQL execution plan, the method further comprises recording a state of the SQL execution plan as a determined state.
15. The method according to claim 9, wherein updating the first iteration parameter comprises: obtaining a union set of the second iteration parameter and the first iteration parameter; and setting a result of the union set as the first iteration parameter.
16. The method according to claim 1, further comprising: establishing an initial plan tree according to a pre-stored estimation iteration parameter when an initial iteration is performed; executing the initial plan tree to obtain an initial iteration parameter; and updating the estimation iteration parameter according to the initial iteration parameter to obtain the first iteration parameter.
17. The method according to claim 16, wherein when the SQL execution plan corresponds to at least two relation tables, the initial iteration parameter further comprises relation tuples in a result set obtained after a join operation is performed on any one or more relation tables in the at least two relation tables.
18. The method according to claim 16, wherein updating the estimation iteration parameter comprises: obtaining a union set of the initial iteration parameter and the estimation iteration parameter; and setting a result of the union set as the first iteration parameter.
19. The method according to claim 16, wherein before the initial iteration is performed, the method further comprises: querying whether a state of the SQL execution plan is a determined state; and executing the SQL execution plan when the state of the SQL execution plan is the determined state.
20. An apparatus for determining a structured query language (SQL) execution plan, wherein the SQL execution plan corresponds to at least one relation table, and wherein the apparatus comprises: a memory comprising instructions; and a processor coupled to the memory, and wherein during an N.sup.th iteration, the instructions cause the processor to be configured to: obtain a first iteration parameter generated after a first plan tree is executed on the at least one relation table during an (N−1).sup.th iteration, wherein N is a natural number greater than 1; establish a second plan tree according to the first iteration parameter; and determine the first plan tree or the second plan tree as the SQL execution plan when a difference between the second plan tree and the first plan tree is not greater than a first threshold.
Description
BRIEF DESCRIPTION OF DRAWINGS
[0086] To describe the technical solutions in the embodiments of the present application more clearly, the following briefly describes the accompanying drawings required for describing the embodiments.
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
DESCRIPTION OF EMBODIMENTS
[0095] The following clearly describes the technical solutions in the embodiments of the present application with reference to the accompanying drawings in the embodiments of the present application. The described embodiments are merely some but not all of the embodiments of the present application.
[0096] In addition, the terms “first” and “second” are merely intended for a purpose of description, and shall not be understood as an indication or implication of relative importance or implicit indication of a quantity of indicated technical features. Therefore, a feature limited by “first” or “second” may explicitly or implicitly include one or more of the features. In the description of the present application, “multiple” means two or more than two, unless otherwise specified.
[0097] A method for determining an SQL execution plan that is provided in the embodiments of the present application may be applied in a phase for determining the SQL execution plan. A core principle of the solution is as follows. Setting an iteration method to establish a plan tree based on an iteration parameter generated each time a plan tree is actually executed in order to obtain an optimal SQL execution plan.
[0098] Further, in other approaches, a server receives an SQL statement. Generally, when an SQL execution plan is determined, a plan tree is directly established according to an estimation iteration parameter (that is, an eigenvalue obtained using a sampling method or an iteration method) that is pre-stored in a database of the server, and the plan tree is used as an SQL execution plan. However, because the estimation iteration parameter may be inaccurate, the determined SQL execution plan may be inaccurate.
[0099] Therefore, in the method for determining an SQL execution plan that is provided in the embodiments of the present application, a plan tree may be cyclically established and executed using an iteration method, and iteration parameters generated during actual plan tree execution processes are recorded in order to update an eigenvalue obtained using a sampling method or an estimation method in a previous iteration process. Further, in a next iteration process, a plan tree is re-established using the updated iteration parameter until plan trees established in two adjacent iteration processes are the same, and the plan tree is used as the SQL execution plan.
[0100] During each iteration process, a plan tree (referred to as a first plan tree in the embodiments of the present application) established during a previous iteration process needs to be compared with a plan tree (referred to as a second plan tree in the embodiments of the present application) established during a current iteration process. In addition, the second plan tree is established according to an iteration parameter (referred to as a first iteration parameter in the embodiments of the present application) updated during the previous iteration process, that is, a second plan tree established during each iteration process is established based on the first iteration parameter updated by executing the first plan tree. Therefore, after iterations are performed for multiple times, a finally obtained plan tree does not depend on an estimation iteration parameter already collected in a database, but the plan tree is established according to an iteration parameter recorded each time the plan tree is actually executed during the iteration process in order to obtain a relatively accurate SQL execution plan, and improve execution efficiency of the SQL execution plan.
[0101] To describe in detail the method for determining an SQL execution plan that is provided in the embodiments of the present application, the following explains some professional terms that may be involved in the embodiments of the present application.
[0102] First, for a clear description of the iteration process involved in the embodiments of the present application, it is assumed that a previous iteration is an (N−1).sup.th iteration, and a current iteration is an N.sup.th iteration. Then, a plan tree established during the (N−1).sup.th iteration is a first plan tree, and a plan tree established during the N.sup.th iteration is a second plan tree, where N is a natural number greater than 1.
[0103] The SQL execution plan corresponds to at least one relation table in the database. It is assumed that the SQL execution plan corresponds to M relation tables, where M is a natural number greater than 1.
[0104] It is assumed that the first iteration parameter may include a first eigenvalue and/or a second eigenvalue, that is, at least one of the first eigenvalue or the second eigenvalue. The first eigenvalue indicates relation tuples in any one or more relation tables corresponding to an SQL execution plan, and the second eigenvalue indicates relation tuples in a result set obtained by performing a join operation on any one or more relation tables in the at least two relation tables when the SQL execution plan corresponds to at least two relation tables. In the embodiments of the present application, the first eigenvalue is represented by N.sub.(i), and the second eigenvalue is represented by N.sub.(i,j,k). N.sub.(i) is a relation tuple of an i.sup.th relation table, and N.sub.(i,j,k) is a relation tuple of a result set obtained after a join operation is performed in a sequence of an i.sup.th relation table, a i.sup.th relation table, and a k.sup.th relation table.
[0105] Execution of the SQL execution plan by the server may further include determining a filter criterion of the SQL execution plan. For example, the filter criterion is to count all boys taller than 180 centimeters. In this case, the first eigenvalue may be further used to indicate a relation tuple, which meets the filter criterion, in the i.sup.th relation table.
[0106] Similarly, the second iteration parameter involved in the embodiments of the present application also includes a first eigenvalue and/or a second eigenvalue. A difference is that the first iteration parameter refers to an iteration parameter updated during an (N−1).sup.th iteration, and the second iteration parameter refers to an iteration parameter generated when a second plan tree is executed during an N.sup.th iteration. A manner of updating the first iteration parameter is described in detail in the embodiments of the present application.
[0107] The eigenvalue may further include a quantity of unique distinct values in a field in the relation table, and/or a set (MCV) of a column whose quantity of occurrences in the relation table exceeds a second threshold.
[0108] In the embodiments of the present application, further, if a second plan tree established during an N.sup.th iteration is different from a first plan tree established during an (N−1).sup.th iteration, but a second iteration parameter generated when the second plan tree is executed is the same as a first iteration parameter generated when the first plan tree is executed, it is considered that a variation needs to be performed on a current SQL execution plan such that a better SQL execution plan can be further obtained.
[0109] As shown in
[0110] Step 101: Obtain a first iteration parameter generated when a first plan tree is executed.
[0111] Because both a plan tree established during an iteration process and a first iteration parameter generated at the end of the iteration process can be saved, during an N.sup.th iteration process, a first plan tree established during an (N−1).sup.th iteration and the foregoing first iteration parameter can be obtained.
[0112] Step 102: Establish a second plan tree according to the first iteration parameter.
[0113] For example, when N=2, a second plan tree can be established according to a first iteration parameter obtained when a first iteration is completed.
[0114] Step 103: Execute the second plan tree, and record a second iteration parameter generated when the second plan tree is executed.
[0115] Further, in step 103, the second plan tree established in step 102 is executed, and a second iteration parameter generated when the second plan tree is executed is recorded.
[0116] Similarly, the second iteration parameter also includes N.sub.(i) and/or N.sub.(i,j,k).
[0117] Step 104: Compare the second plan tree established during an N.sup.th iteration with the first plan tree established during an (N−1).sup.th iteration in order to determine whether the second plan tree is the same as the first plan tree.
[0118] It should be noted that, the foregoing (N−1).sup.th iteration is an initial plan tree established during the first iteration when N=2.
[0119] It should be understood that, for example, in step 104, the second plan tree established during the N.sup.th iteration is compared with the first plan tree established during the (N−1).sup.th iteration in order to determine whether the second plan tree is the same as the first plan tree. In some embodiments, step 104 further includes comparing the second plan tree established during the N.sup.th iteration and the first plan tree established during the (N−1).sup.th iteration with a threshold in order to determine whether a difference between the second plan tree and the first plan tree is not greater than the threshold. The threshold may be a fixed value that is preset or obtained by means of training, or may be a dynamic value that is constantly updated in a system. This is not limited. For example, comparing the steps for executing the second plan tree with the steps for executing the first plan tree in order to determine whether a quantity of different steps is not greater than one, or comparing the steps for executing the second plan tree with the steps for executing the first plan tree in order to determine whether a percentage of different steps is not greater than 99%. This is not limited. In the subsequent steps, any description refers to comparing the first plan tree and the second plan tree to determine whether the first plan tree is the same as or similar with the second plan tree has the same meaning as the description of step 104, and details are not described herein again. Generally, an obtained SQL execution plan is a sub-optimal execution plan when the threshold is not 0. However, it should be understood that more operation time and resources are saved in a process of determining the sub-optimal execution plan when compared with a process of determining an optimal execution plan. In some embodiments, for example, in an application scenario in which a requirement for the operation time and the resources is relatively high, the sub-optimal execution plan is used. Perform step 105 if the second plan tree established during the N.sup.th iteration is different from the first plan tree established during the (N−1).sup.th iteration.
[0120] Step 105: Update the first iteration parameter using the second iteration parameter.
[0121] Further, a first eigenvalue and/or a second eigenvalue that are/is different from a first iteration parameter and that are/is in a second iteration parameter can be added to the first iteration parameter, that is, a union set of the first iteration parameter and the second iteration parameter is obtained, to obtain an updated first iteration parameter such that during an (N+1).sup.th iteration, a second plan tree is re-established using the updated first iteration parameter. Because the updated first iteration parameter is a first eigenvalue and/or a second eigenvalue obtained when the second plan tree is actually executed, the second plan tree established using the updated first iteration parameter is more accurate.
[0122] Step 106: Update the first plan tree with the second plan tree, and increase a quantity of iterations by 1.
[0123] In this case, a second plan tree generated during this iteration process is used as a first plan tree during a next iteration process until a second plan tree established during an iteration process is the same as a first plan tree established during a previous iteration process.
[0124] In addition, a quantity of iterations may be increased by 1 in order to facilitate a repeated execution of steps 101 to 106. Perform step 107 if the second plan tree established during the N.sup.th iteration is the same as the first plan tree established during the (N−1).sup.th iteration.
[0125] Step 107: Set the second plan tree as an SQL execution plan.
[0126] Further, in step 107, when the second plan tree established during the N.sup.th iteration is the same as the first plan tree established during the (N−1).sup.th iteration, it indicates that after a quantity of N iterations, all first eigenvalues and second eigenvalues are included in the first iteration parameter. Therefore, in this case, the second plan tree established according to the first iteration parameter is optimal, and the second plan tree can be used as the SQL execution plan.
[0127] In this case, a correspondence between the second plan tree and a related SQL statement for executing the second plan tree can be saved. When the SQL statement is received again, the server can directly obtain the second plan tree, that is, the SQL execution plan from a corresponding storage unit.
[0128] In some embodiments, similar to step 104, step 107 further includes that if a second plan tree established during an N.sup.th iteration and a first plan tree established during an (N−1).sup.th iteration are not greater than a threshold, the second plan tree or the first plan tree is used as an SQL execution plan. It should be understood that, in this embodiment, both the first plan tree and the second plan tree are sub-optimal plan trees, and either the first plan tree or the second plan tree can be selected as the SQL execution plan.
[0129] It should be understood that, in some embodiments or in some iteration cycles used as examples in some embodiments, step 103, step 105, and step 106 are optional steps.
[0130] In addition, optionally, an execution time of a second plan tree can be recorded in each iteration cycle. When a quantity of iterations exceeds a predetermined threshold, that is, when an iteration algorithm is not converged within a time threshold, the foregoing second plan tree with the shortest execution time in all iteration cycles is selected as the SQL execution plan.
[0131] Optionally, after the second plan tree is used as the SQL execution plan, a state of the SQL execution plan is set to a determined state in the server, that is, an optimized SQL execution plan is determined. In a subsequent step, when the state of the SQL execution plan is the determined state, a process during which the SQL execution plan is determined does not need to be repeated, thereby improving execution efficiency of the server.
[0132] Further, based on the method for determining an SQL execution plan that is shown in
[0133] Step 201: Query whether a state of the SQL execution plan is a determined state.
[0134] Further, an identifier of a relation table corresponding to the SQL execution plan is carried in an SQL statement. For example, a received SQL statement includes select count(*) from dba, that is, search a relation table whose identifier is dba for a quantity of lines of a dba relation table.
[0135] Because a same SQL statement may be repeatedly executed by a server in a period of time, when an SQL execution plan related to an SQL statement is generated for the first time, a correspondence between the SQL execution plan and the SQL statement can be saved. In this way, when an SQL statement is received again, it can be found, by means of query, whether an SQL execution plan corresponding to the SQL statement exists, that is, whether a state of the SQL execution plan is a determined state.
[0136] For example, a variable Optimization can be set, and is used to indicate whether an SQL execution plan corresponding to a received SQL statement is stored.
[0137] In this case, the server can query whether the variable Optimization corresponding to the SQL statement is 1, and if Optimization=1, it indicates that an SQL execution plan corresponding to the received SQL statement is stored, and the server can directly obtain the SQL execution plan from a corresponding storage unit.
[0138] On the contrary, if Optimization≠1 that is, a state of the SQL execution plan is a non-determined state, it indicates that no SQL execution plan corresponding to the received SQL statement is stored, that is, the SQL statement is executed for the first time, and in this case, the following steps 202 to 213 are performed.
[0139] Step 202: If the SQL execution plan is in a non-determined state, initialize a variable to be used during a subsequent iteration process.
[0140] In step 202, if Optimization≠1 in this case, a variable to be used during an iteration can be initialized. For example, a quantity N of iterations is set to 1, the Optimization is set to 0, and a Variation is set to 0.
[0141] Step 203: Establish an initial plan tree according to a pre-stored estimation iteration parameter when an initial iteration is performed.
[0142] After the initialization operation in step 202 is completed, a first iteration process is started. In this case, an initial plan tree can be established according to an estimation iteration parameter stored in statistical information.
[0143] Because a plan tree is not established during the first iteration, an actual iteration parameter cannot be collected from a process during which a plan tree is executed. Therefore, in this case, the other approach is followed, that is, an initial plan tree T.sub.0 is established using an estimation iteration parameter stored in the statistical information.
[0144] Step 204: Execute the initial plan tree to obtain an initial iteration parameter generated when the initial plan tree is executed, and update the estimation iteration parameter using the initial iteration parameter.
[0145] A difference from the other approach is that, in step 204, the server can execute the initial plan tree established in step 203, and an initial iteration parameter generated when the initial plan tree is executed is recorded, that is, N.sub.(i) and/or N.sub.(i,j,k). In this way, the estimation iteration parameter in step 203 can be updated with the initial iteration parameter generated when the initial plan tree is executed.
[0146] For example, it is assumed that the SQL statement in step 201 carries identifiers of two relation tables, that is, t1 and t2. In step 203, estimation iteration parameters for establishing an initial plan tree further include N.sub.(t1), N.sub.(t2), and N.sub.(t2,t1). However, in step 204, initial iteration parameters generated when an initial plan tree is executed may include N.sub.(t1), N.sub.(t2), and N.sub.(t1,t2). In this case, an iteration parameter that is in the initial iteration parameters and that is different from the estimation iteration parameters can be updated into the estimation iteration parameters, that is, the updated estimation iteration parameters are N.sub.(t1), N.sub.(t2), N.sub.(t1,t2), and N.sub.(t2,t1), and the updated estimation iteration parameters are used as the foregoing first iteration parameters in order to establish a second plan tree during a subsequent (N−1).sup.th iteration process using the updated first iteration parameters.
[0147] Further, for the second to the last iteration processes, refer to the following steps 205 to 213. Further, an N.sup.th iteration process is used herein as an example for illustration purposes.
[0148] Step 205: During an N.sup.th iteration, obtain a first plan tree established during an (N−1).sup.th iteration.
[0149] Because a plan tree established during each iteration process can be saved, during an N.sup.th iteration, a first plan tree established during an (N−1).sup.th iteration can be obtained.
[0150] Step 206: Determine whether a variation needs to be performed on the SQL execution plan.
[0151] That is, whether the first iteration parameter is the same as the second iteration parameter is determined. A variation needs to be performed on the SQL execution plan if the first iteration parameter is the same as the second iteration parameter, or a variation does not need to be performed on the SQL execution plan if the first iteration parameter is different from the second iteration parameter. It should be understood that, in this step, N>1. N=2 is used herein as an example for description. After the first iteration process in steps 203 and 204, during a second iteration, it can be first determined whether a variation needs to be performed on an SQL execution plan, that is, whether a Variation is equal to 1.
[0152] If Variation≠1, step 207 is performed, or if Variation=1, step 208 is performed.
[0153] Step 207: If a variation does not need to be performed on the SQL execution plan, establish a second plan tree according to a first iteration parameter updated during the (N−1).sup.th iteration.
[0154] Further, if the first iteration parameter generated when the first plan tree is executed is different from the second iteration parameter generated when the second plan tree is executed, the first iteration parameter is updated using the second iteration parameter. In the second iteration parameter, a first eigenvalue and/or a second eigenvalue that is different from the first iteration parameter can be added to the first iteration parameter, and an updated first iteration parameter is obtained, that is, a union set of the first iteration parameter and the second iteration parameter is obtained, and a result of the union set is used as an updated first iteration parameter.
[0155] That is, if Variation≠1, a variation does not need to be performed on the SQL execution plan, and a second plan tree is established according to the first iteration parameter updated during the (N−1).sup.th iteration.
[0156] For example, when N=2, if Variation≠1, a second plan tree is established according to a first iteration parameter updated during a first iteration (that is, the first iteration parameter obtained in step 204).
[0157] Step 208: If a variation needs to be performed on the SQL execution plan, establish a second plan tree different from the foregoing first plan tree according to a first iteration parameter updated during the (N−1).sup.th iteration.
[0158] Further, if Variation=1, that is, a variation needs to be performed on the SQL execution plan, a second plan tree can also be established according to a first iteration parameter updated during the (N−1).sup.th iteration. However, a difference from step 207 is that the established second plan tree is different from a first plan tree established during the (N−1).sup.th iteration.
[0159] For example, a plan tree established during a first iteration is an initial plan tree. Then, when N=3, if Variation=1, a second plan tree is established using a first iteration parameter updated during a second iteration, and it is ensured that the second plan tree is different from a first plan tree established during the second iteration.
[0160] It should be understood that, the server can establish a second plan tree according to a first iteration parameter. In this case, the second plan tree is considered by the server as an optimal plan tree that meets a requirement of the first iteration parameter, but may not be an actually optimal plan tree. When a variation needs to be performed on the SQL execution plan, the server can be prohibited from generating the foregoing second plan tree (that is, the second plan tree that is the same as the first plan tree) considered by the server as the optimal plan tree, but generate another second plan tree that is different from the first plan tree.
[0161] Step 209: Execute the second plan tree, and record a second iteration parameter generated when the second plan tree is executed.
[0162] Further, in step 209, the second plan tree established in step 207 or step 208 is executed, and a second iteration parameter generated when the second plan tree is executed is recorded.
[0163] Similarly, the second iteration parameter also includes N.sub.(i) and/or N.sub.(i,j,k).
[0164] Step 210: Compare the second plan tree established during the N.sup.th iteration with the first plan tree established during the (N−1).sup.th iteration in order to determine whether the second plan tree is the same as the first plan tree.
[0165] It should be noted that, when N=2, a first plan tree established in the foregoing (N−1).sup.th iteration, that is, during a first iteration, is the initial plan tree established in step 203.
[0166] Step 211: If the second plan tree established during the N.sup.th iteration is different from the first plan tree established during the (N−1).sup.th iteration, compare the first iteration parameter generated when the first plan tree is executed with the second iteration parameter generated when the second plan tree is executed in order to determine whether the first iteration parameter is the same as the second iteration parameter, that is, to determine whether a variation needs to be performed on the SQL execution plan, step 206 is performed, and step 212 is performed only when the second plan tree established during the N.sup.th iteration is the same as the first plan tree established during the (N−1).sup.th iteration.
[0167] Step 212: If the second plan tree established during the N.sup.th iteration is the same as the first plan tree established during the (N−1).sup.th iteration, set the second plan tree as the SQL execution plan.
[0168] Further, in step 212, when the second plan tree established during the N.sup.th iteration is the same as the first plan tree established during the (N−1).sup.th iteration, it indicates that after a quantity of N iterations, all first eigenvalues and second eigenvalues are included in the first iteration parameter. Therefore, in this case, the second plan tree established according to the first iteration parameter is optimal, and the second plan tree can be used as the SQL execution plan.
[0169] In addition, a correspondence between the second plan tree and the SQL statement is saved, and a variable Optimization is set to 1. In this way, when the SQL statement is received again, if Optimization=1, it indicates that an SQL execution plan corresponding to the received SQL statement is stored such that the server can directly obtain the SQL execution plan from a corresponding storage unit.
[0170] In addition, on a basis of steps 201 to 212, steps 301 to 303 can be further added. For example, as shown in
[0171] Step 301: Record an execution time for executing the second plan tree during the N.sup.th iteration.
[0172] Step 302: Determine whether a quantity N of iterations is greater than a first threshold, and whether a variation needs to be performed on the SQL execution plan.
[0173] Step 303: If the quantity N of iterations is greater than the threshold, and a variation needs to be performed on the SQL execution plan, set a second plan tree with the shortest execution time during all N iterations as the SQL execution plan.
[0174] Further, in step 209, that is, when the second plan tree is executed, an execution time spent in executing the second plan tree can be recorded.
[0175] Further, after step 209 and before step 210, step 302 is performed, that is, whether a current quantity N of iterations is greater than the threshold and whether a variation needs to be performed on the SQL execution plan are determined.
[0176] Further, if the quantity N of iterations is greater than the threshold, and a variation needs to be performed on the SQL execution plan, that is, N is greater than the threshold, and Variation=1, step 303 is performed, that is, the second plan tree with the shortest execution time during the N iteration processes recorded in step 301 is used as the SQL execution plan in step 201.
[0177] In this way, for SQL execution plans on which a variation is already performed during iteration processes, a plan tree that is the same as a plan tree obtained during a previous iteration may never be obtained by means of iteration. Therefore, to avoid an infinite loop, it can be considered that when a quantity N of iterations is greater than a threshold, during passed N iteration processes, a second plan tree with the shortest execution time is an optimal plan tree, and then the second plan tree is used as the SQL execution plan.
[0178] Correspondingly, if it is determined, in step 302, that a current quantity N of iterations is less than the threshold, and/or a variation does not need to be performed on the SQL execution plan, in this case, an iteration is continually performed according to steps 210 to 212 until the quantity N of iterations is greater than the threshold and a variation does not need to be performed on the SQL execution plan.
[0179] In addition, when an execution time for executing the second plan tree during each iteration is recorded, for example, an execution time for executing the second plan tree during an N.sup.th iteration process is T1, in this case, an execution time T2 for executing the second plan tree during an (N−1).sup.th iteration process can be compared with T1. If T1 is less than T2, the relatively short execution time T1, and a second plan tree corresponding to the relatively short execution time T1 are saved. In this way, the saved second plan tree is a second plan tree with the shortest execution time during N.sup.th iteration processes, and an execution time for executing the second plan tree during each iteration and a corresponding second plan tree do not need to be recorded, thereby saving storage resources.
[0180] This embodiment of the present application provides a method for determining an SQL execution plan. An N.sup.th iteration process is used as an example. First, a first iteration parameter updated during an (N−1).sup.th iteration is obtained, for example, it is assumed that the first iteration parameter includes a first eigenvalue and/or a second eigenvalue, where the first eigenvalue is used to indicate a relation tuple of an i.sup.th relation table, and the second eigenvalue is used to indicate a relation tuple in a result set obtained after a join operation is performed on at least two relation tables. Then, a second plan tree is established using the first iteration parameter, and the second plan tree is used as the SQL execution plan when the second plan tree is the same as a first plan tree. A finally obtained second plan tree in the solution does not depend on an estimation iteration parameter already collected in a database, but is established according to a first iteration parameter updated during an iteration process. Further, the second plan tree is used as an SQL execution plan when a second plan tree is the same as a first plan tree such that a relatively accurate SQL execution plan can be determined, thereby improving execution efficiency of the SQL execution plan.
[0181]
[0182] Further, as shown in
[0183] For example, the first threshold is 0, and correspondingly, determining the first plan tree or the second plan tree as the SQL execution plan when a difference between the second plan tree and the first plan tree is not greater than a first threshold includes determining the first plan tree or the second plan tree as the SQL execution plan when the second plan tree is the same as the first plan tree.
[0184] For example, the first iteration parameter includes relation tuples in any one or more relation tables corresponding to the SQL execution plan.
[0185] For example, when the SQL execution plan corresponds to at least two relation tables, the first iteration parameter further includes relation tuples in a result set that is obtained after a join operation is performed on any one or more relation tables in the at least two relation tables.
[0186] Further, as shown in
[0187] For example, the recording unit 5 is further configured to record a state of the SQL execution plan as a determined state.
[0188] For example, the second iteration parameter includes relation tuples in any one or more relation tables corresponding to the SQL execution plan.
[0189] For example, when the SQL execution plan corresponds to at least two relation tables, the second iteration parameter further includes relation tuples in a result set that is obtained after a join operation is performed on any one or more relation tables in the at least two relation tables.
[0190] Further, as shown in
[0191] The establishment unit 2 is further configured to establish a second plan tree different from the first plan tree according to the first iteration parameter when the second iteration parameter is the same as the first iteration parameter.
[0192] The recording unit 5 is further configured to record an execution time for executing the second plan tree.
[0193] The determining unit 3 is further configured to set the second plan tree with the shortest execution time during all N iterations as the SQL execution plan when the quantity N of iterations is greater than a second threshold.
[0194] The determining unit 3 is further configured to set the second plan tree with the shortest execution time during all N iterations as the SQL execution plan when the quantity N of iterations is greater than a third threshold.
[0195] The recording unit 5 is further configured to record a state of the SQL execution plan as a determined state.
[0196] The establishment unit 2 is further configured to establish an initial plan tree according to a pre-stored estimation iteration parameter when an initial iteration is performed.
[0197] The execution unit 4 is further configured to execute the initial plan tree to obtain an initial iteration parameter generated when the initial plan tree is executed.
[0198] The update unit 6 is further configured to update the estimation iteration parameter using the initial iteration parameter to obtain the first iteration parameter.
[0199] Further, as shown in
[0200] The query unit 7 is configured to query whether the state of the SQL execution plan is the determined state.
[0201] The determining unit 3 is further configured to execute the SQL execution plan when the state of the SQL execution plan is the determined state.
[0202] The update unit 6 is further configured to obtain a union set of the second iteration parameter and the first iteration parameter, and set a result of the union set as the first iteration parameter.
[0203] As shown in
[0204]
[0205] The memory 13 is configured to store a computer execution instruction. The processor 11 is connected to the memory 13 using the communications bus 12. When an apparatus for determining an SQL execution plan runs, the processor 11 executes the computer execution instruction stored in the memory 13 such that the apparatus for determining an SQL execution plan performs the method for determining an SQL execution plan in any one of
[0206] Specific functions of the foregoing obtaining unit 1, the establishment unit 2, the determining unit 3, the execution unit 4, the recording unit 5, the update unit 6, and the query unit 7 of
[0207] The processor 11 may be a general central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits that are configured to control program execution of the solutions of the present application.
[0208] The communications bus 12 may include a path for transferring information between the foregoing components. The communications interface 14 is configured to communicate with another device or communications network, such as an Ethernet, a radio access network (RAN), or a wireless local area network (WLAN) using any apparatus of a transceiver type.
[0209] The memory 13 may be a read-only memory (ROM) or another type of static storage device that can store static information and an instruction, or a random access memory (RAM) or another type of dynamic storage device that can store information and an instruction, or may be an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM), another compact disc storage device, an optical disc storage device (such as a compact disc, a laser disc, an optical disc, a digital versatile disc, or a BLU-RAY DISC), a disk storage medium, another disk storage device, or any other medium that can be used to carry or store expected program code in form of an instruction or a data structure and that can be accessed by a computer. This is not limited herein. The memory 13 may exist independently, and is connected to the processor using the bus. The memory 13 may also be integrated into the processor.
[0210] The memory 13 is configured to store the application program code that is used to execute the solutions of the present application, and the application program code is executed under the control of the processor 11. The processor 11 is configured to execute the application program code stored in the memory 13.
[0211] In a specific implementation, in an embodiment, the processor 11 may include one or more CPUs, such as a CPU0 and a CPU1 in
[0212] In a specific implementation, in an embodiment, the computer device 100 may include multiple processors, for example, the processor 11 and a processor 18 in
[0213] In a specific implementation, in an embodiment, the computer device 100 may further include an output device 15 and an input device 16. The output device 15 communicates with the processor 11, and can display information in multiple manners. For example, the output device 15 may be a liquid crystal display (LCD), a light emitting diode (LED) display device, a cathode-ray tube (CRT) display device, or a projector. The input device 16 communicates with the processor 11, and can receive an input from a user in multiple manners. For example, the input device 16 may be a mouse, a keyboard, a touchscreen device, or a sensor device.
[0214] The foregoing computer device 100 may be a general-purpose computer device or a special-purpose computer device. In a specific implementation, the computer device 100 may be a desktop computer, a portable computer, a network server, a personal digital assistant (PDA), a mobile phone, a tablet computer, a wireless terminal device, a communications device, a built-in device, or a device with a structure similar to that in
[0215] This embodiment of the present application provides a method for determining an SQL execution plan. An N.sup.th iteration process is used as an example. First, a first iteration parameter updated during an (N−1).sup.th iteration is obtained, for example, the first iteration parameter includes a first eigenvalue and/or a second eigenvalue, where the first eigenvalue is used to indicate a relation tuple of an i.sup.th relation table, and the second eigenvalue indicates a relation tuple in a result set obtained after a join operation is performed on at least two relation tables. Then, a second plan tree is established using the first iteration parameter, and the second plan tree is used as the SQL execution plan when the second plan tree is the same as a first plan tree. A finally obtained second plan tree in the solution does not depend on an estimation iteration parameter already collected in a database, but is established according to a first iteration parameter updated during an iteration process. Further, the second plan tree is used as an SQL execution plan when a second plan tree is the same as a first plan tree such that a relatively accurate SQL execution plan can be determined, thereby improving execution efficiency of the SQL execution plan.
[0216] It may be clearly understood by a person skilled in the art that, for the purpose of convenient and brief description, division of the foregoing function modules is taken as an example for illustration. In actual application, the foregoing functions can be allocated to different function modules and implemented according to a requirement, that is, an inner structure of an apparatus is divided into different function modules to implement all or some of the functions described above. For a detailed working process of the foregoing system, apparatus, and unit, refer to a corresponding process in the foregoing method embodiments, and details are not described herein.
[0217] In the several embodiments provided in this application, it should be understood that the disclosed system, apparatus, and method may be implemented in other manners. For example, the described apparatus embodiment is merely an example. For example, the module or unit division is merely logical function division and may be other division in actual implementation. For example, a plurality of units or components may be combined or integrated into another system, or some features may be ignored or not performed. In addition, the displayed or discussed mutual couplings or direct couplings or communication joins may be implemented using some interfaces. The indirect couplings or communication joins between the apparatuses or units may be implemented in electronic, mechanical, or other forms.
[0218] The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one position, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the objectives of the solutions of the embodiments.
[0219] In addition, functional units in the embodiments of the present application may be integrated into one processing unit, or each of the units may exist alone physically, or two or more units are integrated into one unit. The integrated unit may be implemented in a form of hardware, or may be implemented in a form of a software functional unit.
[0220] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, the integrated unit may be stored in a computer-readable storage medium. Based on such an understanding, the technical solutions of the present application essentially, or the part, or all or some of the technical solutions may be implemented in the form of a software product. The software product is stored in a storage medium and includes several instructions for instructing a computer device (which may be a personal computer, a server, or a network device) or a processor to perform all or some of the steps of the methods described in the embodiments of the present application. The foregoing storage medium includes any medium that can store program code, such as a universal serial bus (USB) flash drive, a removable hard disk, a ROM, a RAM, a magnetic disk, or an optical disc.
[0221] The foregoing descriptions are merely specific implementation manners of the present application, but are not intended to limit the protection scope of the present application. Any variation or replacement readily figured out by a person skilled in the art within the technical scope disclosed in the present application shall fall within the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.