Database query optimization plan execution method, device, equipment and medium
By comparing the latest and benchmark query plans in database query optimization, and selecting a better plan for execution, the problem of execution plan instability caused by changes in query factors is solved, and the efficiency and performance of database queries are improved.
Patent Information
- Application Number
- CN202311572978.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-21
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2043-11-21
AI Technical Summary
In database query optimization, changes in query factors lead to instability in the execution plan, which may cause query performance degradation and make it difficult to maintain an optimized query plan when changes occur.
By using the query optimizer to generate the latest query plan when the target query statement enters the current query, and comparing it with the baseline plan of the previous query, a better plan is selected for execution to ensure that the baseline is used when the benchmark cannot be exceeded. Plan execution to avoid performance degradation and find more optimized plans after iterations.
Ensure the stability of the query plan, improve database query efficiency, avoid performance degradation caused by changes in query factors, and find a more optimized query plan after a certain number of iterations.
Smart Images

Figure CN117520380B_ABST
Abstract
Description
Technical Field
[0001] The present application is applicable to the technical field of database optimization, and in particular, relates to a plan execution method, device, equipment and medium for database query optimization. Background Art
[0002] Currently, in the database optimizer module, the cost-based optimizer (CBO) searches from all query execution plan spaces that are equivalent to a given query, selects the one with the lowest estimated cost as the best plan, and then executes the plan in the execution phase. For complex queries, the cost of searching all possible plan spaces is very high. Therefore, many optimizers use various means to reduce the cost of query optimization, for example, using heuristic methods to select inner joins with fewer join conditions to avoid greater cost estimation deviations caused by more join conditions, and finally form a stable and healthy plan, thereby reducing the risk of not forming an optimal plan.
[0003] For a query statement, changes in multiple factors (such as statistical information, system resources, etc.) will lead to changes in the final execution plan. The instability of the plan may cause the problem of performance degradation of the plan execution. Among them, the statistical information describes detailed information on multiple dimensions of the database object. CBO collects statistical information (such as selectivity, cardinality, etc.) and uses these statistical information to estimate the cost of various possible and different execution paths of the target query statement. However, traditional query optimizers estimate the cost value by tuning the cardinality. Therefore, if the real cost of the subquery is tuned towards the wrong estimate, some better plans may be generated in the adjacent plan space. If it is tuned in the opposite direction, some worse plans may be generated. Therefore, when the query factors change, how to make the change not cause query execution performance degradation, so as to execute a better and relatively stable plan becomes an urgent problem to be solved. Summary of the invention
[0004] In view of this, embodiments of the present application provide a plan execution method, apparatus, device and medium for database query optimization to solve the problem of how to make changes without causing query execution performance degradation, thereby executing a better and relatively stable plan.
[0005] In a first aspect, an embodiment of the present application provides a plan execution method for database query optimization, the plan execution method comprising:
[0006] When entering the current query based on the target query statement, use the query optimizer to generate the latest query plan;
[0007] Using the query plan executed by the target query statement in the previous query as a benchmark plan, comparing the latest query plan with the benchmark plan, and obtaining a comparison result;
[0008] When the comparison result shows that the benchmark plan is better, the current query is executed based on the benchmark plan; when the comparison result shows that the latest query plan is better, the current query is executed based on the latest query plan.
[0009] In a second aspect, an embodiment of the present application provides a plan execution device for database query optimization, the plan execution device comprising:
[0010] A plan generation module is used to generate a latest query plan using a query optimizer when entering the current query based on the target query statement;
[0011] A plan comparison module is used to use the query plan executed by the target query statement in the previous query as a benchmark plan, compare the latest query plan with the benchmark plan, and obtain a comparison result;
[0012] A plan execution module is used to execute the current query based on the benchmark plan when the comparison result shows that the benchmark plan is better, and to execute the current query based on the latest query plan when the comparison result shows that the latest query plan is better.
[0013] In a third aspect, an embodiment of the present application provides a computer device, comprising a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the processor implements the plan execution method described in the first aspect when executing the computer program.
[0014] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the plan execution method described in the first aspect is implemented.
[0015] The beneficial effects of the embodiments of the present application compared with the prior art are as follows: when entering the current query based on the target query statement, the present application uses a query optimizer to generate a latest query plan, takes the query plan executed by the target query statement in the previous query as a benchmark plan, compares the latest query plan with the benchmark plan to obtain a comparison result, and executes the current query based on the benchmark plan when the comparison result shows that the benchmark plan is better; and when the comparison result shows that the latest query plan is better, executes the current query based on the latest query plan, taking the plan at the time of the previous query as a benchmark, and executing the query with the benchmark plan when the benchmark cannot be surpassed, can ensure that the query plan will not develop in the direction of degradation, and at the same time can find a more optimized query plan after a certain number of iterations, thereby improving the efficiency of database queries. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0017] Figure 1 This is a schematic diagram of an application environment of a plan execution method for database query optimization provided in Example 1 of the present application;
[0018] Figure 2 It is a flowchart of a plan execution method for database query optimization provided in Example 2 of the present application;
[0019] Figure 3 It is a flowchart of a plan execution method for database query optimization provided in Example 3 of the present application;
[0020] Figure 4 It is a flowchart of a plan execution method for database query optimization provided in Embodiment 4 of the present application;
[0021] Figure 5 It is a flowchart of a plan execution method for database query optimization provided in Example 5 of the present application;
[0022] Figure 6 It is a flowchart of a plan execution method for database query optimization provided in Example 6 of the present application;
[0023] Figure 7 It is a flowchart of a plan execution method for database query optimization provided in Embodiment 7 of the present application;
[0024] Figure 8 This is an overall architecture diagram of an execution method provided in Example 7 of the present application;
[0025] Fig. 9 It is a structural diagram of a plan execution device for database query optimization provided in Embodiment 8 of the present application;
[0026] Fig.10 It is a structural diagram of a computer device provided in Example 9 of the present application. DETAILED DESCRIPTION
[0027] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.
[0028] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0029] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0030] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.
[0031] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0032] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0033] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0034] AI basic technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, etc. AI software technologies mainly include computer vision technology, robotics technology, biometrics technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0035] It should be understood that the size of the serial numbers of the steps in the following embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0036] In order to illustrate the technical solution of the present application, a specific embodiment is provided below for illustration.
[0037] The first embodiment of the present application provides a database query optimization plan execution method, which can be applied to Figure 1 In the application environment, the server communicates with the client, the server provides database query services, and the client triggers query tasks to the server. The client includes but is not limited to PDAs, desktop computers, laptops, ultra-mobile personal computers (UMPCs), netbooks, cloud computing devices, personal digital assistants (PDAs), and other devices. The computer device corresponding to the server can be implemented with an independent server or a server cluster consisting of multiple servers.
[0038] See also Figure 2, is a flow chart of a method for executing a plan for optimizing database query provided in the second embodiment of the present application. The method for executing a plan for optimizing database query is applied to Figure 1 The server in the example connects to the client to obtain the target query statement sent by the client. In addition, the server has the function of providing query services, that is, it has a query optimizer that generates query plans and an engine that executes query plans. Figure 2 As shown, the plan execution method for database query optimization may include the following steps:
[0039] Step S201, when entering the current query based on the target query statement, using the query optimizer to generate the latest query plan.
[0040] In the embodiment of the present application, the target query statement may refer to a statement used for database query. The query language corresponding to the target query statement needs to be a query language that can be processed by the server. The target query statement may be sent by the client or provided by other users. For example, a query statement based on Structured Query Language (SQL): "SELECT * FROM A, B, C WHERE A.1 = B.2 AND B.1 = C.1;".
[0041] The query optimizer is a component that performs internal optimization for user requests, generates (or reuses) execution plans and transmits them to the storage engine to operate data, and finally returns the results to the user. The query optimizer targeted in the embodiments of the present application can be a CBO or an optimizer based on machine learning. The query optimizer can generate an optimized query plan based on changes in query factors, and there is no limitation on this.
[0042] The current query is the query operation of this round triggered at the current time, wherein a round characterization starts from generating a query plan based on the target query statement (i.e., the above step S201) to executing the query plan corresponding to this round (i.e., the following step S203). If the current query is the first query in the entire iteration cycle, there is no query round based on the target query statement before the current query, and therefore, the following step S202 may not be executed. At this time, the query plan generated by the query optimizer can be used for the first query.
[0043] Optionally, the plan execution method further includes:
[0044] Detect whether the current query is the first query of the target query statement;
[0045] If it is detected that the current query is the first query of the target query statement, after using the query optimizer to generate a latest query plan, the current query is executed based on the latest query plan; or
[0046] If it is detected that the current query is not the first query of the target query statement, a step of using the query plan executed in the previous query of the target query statement as a benchmark plan is executed.
[0047] Among them, if it is not the first query, the following step S202 can be executed. If a preset benchmark plan is set in the server, when the current query is the first query, the step of comparing the latest query plan with the benchmark plan to obtain the comparison result in step S202 can also be executed.
[0048] Step S202: taking the query plan executed by the target query statement in the previous query as the benchmark plan, and comparing the latest query plan with the benchmark plan to obtain a comparison result.
[0049] In an embodiment of the present application, the previous query is the most recent query before the current moment corresponding to the current query. Both the previous query and the current query are queries executed based on the target query statement, that is, the query statement has not changed, but the statistical information, system resources and other factors corresponding to the query statement that affect the query results have changed.
[0050] The query plan finally executed by the previous query is the plan for obtaining the query result of the previous query. The query plan is used as the benchmark plan, and the above-mentioned latest query plan is compared with the benchmark plan, wherein the comparison may refer to comparing the contents of the plan to obtain the difference between the two plans, and the difference may reflect the optimal plan between the latest query plan or the benchmark plan, that is, the comparison result indicates that the latest query plan is better than the benchmark plan or that the benchmark plan is better than the latest query plan.
[0051] When comparing plans, a quantitative approach can be used to convert the plans into corresponding quantitative values. The sizes of the quantitative values can be compared to determine which plan has the larger or smaller quantitative value as the optimal plan. Of course, a preset evaluation model can also be used to evaluate the two plans separately. The evaluation process can estimate the time or delay of each step in the plan, and conduct a comprehensive optimal evaluation of the difference in the degree of impact of each step and its time consumption to give the optimal plan.
[0052] During the plan comparison process, since the number of plans involved in the comparison is small, the time, resources and other costs spent on the comparison are far less than the cost of the plan itself. Therefore, the cost spent in step S202 can be ignored, which helps to improve the accuracy of the comparison between the latest query plan and the benchmark plan.
[0053] Step S203: When the comparison result shows that the benchmark plan is better, the current query is executed based on the benchmark plan; when the comparison result shows that the latest query plan is better, the current query is executed based on the latest query plan.
[0054] In an embodiment of the present application, the optimal plan is determined from the comparison results. Of course, the optimal plan is the benchmark plan or the latest query plan. Due to the existence of the benchmark plan, the current query will not be degraded compared to the previous query, and when the latest query plan is the optimal plan, the query operation can also be performed with the latest query plan, which helps to improve query efficiency.
[0055] In the embodiment of the present application, when the recommendation model predicts a piece of data to be recommended, a classification boundary value for classifying the prediction result generated by the recommendation model is obtained, and a fuzzy interval representing the classification error is determined according to the classification boundary value, a target data set whose prediction result is in the fuzzy interval is screened out from the training data set used in training the recommendation model, prediction rules are extracted for the target data set to obtain at least one prediction rule, the prediction result of the recommendation model corresponding to the data to be recommended is obtained, and it is detected whether the prediction result of the data to be recommended is in the fuzzy interval. If it is detected that the prediction result of the data to be recommended is in the fuzzy interval, a recommendation prediction is performed on the data to be recommended according to at least one prediction rule to obtain an optimized recommendation result corresponding to the data to be recommended, thereby avoiding the process of using the recommendation model to make recommendations for uncertain data such as the fuzzy interval, extracting prediction rules by analyzing the corresponding data in the training data set, and then using the prediction rules to make recommendation predictions for the uncertain data, thereby optimizing the recommendation results without changing the recommendation model, thereby improving the accuracy of the recommendation model.
[0056] See also Figure 3 , is a flow chart of a plan execution method for database query optimization provided in Example 3 of the present application. Figure 3 As shown, using the query optimizer to generate the latest query plan in step S201 may also include the following steps:
[0057] Step S301, obtaining current statistical information and a first query plan group generated by a query optimizer for a target query statement under historical statistical information corresponding to a previous query.
[0058] In the embodiment of the present application, statistical information may refer to detailed information described in multiple dimensions of database objects, such as data selectivity, cardinality, etc. The current statistical information is the current moment mentioned in the above-mentioned embodiment 2, that is, the moment of entering the current query. Of course, there is also a corresponding historical statistical information in the previous query, which is the statistical information corresponding to the historical moment, that is, the moment of the previous query.
[0059] The query optimizer may be a cost-based optimizer, a machine learning-based optimizer, etc. In the embodiment, the optimizer needs to generate a query plan based on statistical information.
[0060] The current query is based on the current statistical information, and the query optimizer estimates the costs of various possible and different execution plans of the target query statement, and then gives at least one query plan. Correspondingly, in the previous query, the query optimizer estimates the cost based on the historical statistical information, and gives at least one query plan. In this embodiment, the query plan generated by the query optimizer in the previous query is counted into the first query plan group.
[0061] Step S302: Based on the current statistical information, a query optimizer is used to generate a second query plan group for the target query statement, and a query plan in the second query plan group that is different from the first query plan group is used as the latest query plan.
[0062] In an embodiment of the present application, based on current statistical information, the query optimizer can generate at least one query plan, which is then included in the second query plan group. Of course, in general, the query optimizer generates one query plan, but in one embodiment, multiple query plans can be generated.
[0063] Therefore, by comparing the first query plan group with the second query plan group, it can be known that the query plan in the second query plan group is different from that in the first query plan group. For example, if each query plan group is a query plan, that is, it is determined that the query plan generated by the query optimizer in the previous query is different from the query plan generated by the query optimizer in the current query, then the query plan generated by the query optimizer in the current query is considered to be a new query plan (i.e., the latest query plan).
[0064] In the above steps, there is no need to re-estimate the query plan of the previous query. Only by generating the query plan corresponding to the current query, it is possible to determine whether the query plan corresponding to the current query is a new query plan, which helps to improve the analysis efficiency of the query plan.
[0065] See also Figure 4 , is a flow chart of a method for executing a plan for optimizing database query provided in Example 4 of the present application. Figure 4As shown, in the above step S202, the latest query plan is compared with the benchmark plan to obtain a comparison result, and the following steps may also be included:
[0066] Step S401 : Use a score estimation model to perform score prediction on the latest query plan and the benchmark plan respectively, to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan.
[0067] Among them, the score estimation model is a model trained before the current query, which can undertake the evaluation task of the query plan. The model can be a machine learning model, a neural network model, etc. Since only the specific weight parameters of the score estimation model are used during use, the architecture of the model is not limited in this embodiment.
[0068] It should be noted that the time, resources and other costs spent in the estimation process of the rating estimation model can be incorporated into the rating estimation model. Of course, the time spent in the estimation process of the rating estimation model can also be ignored.
[0069] Step S402: obtaining a comparison result according to the first prediction score and the second prediction score.
[0070] Among them, for the prediction scores, the optimal plan can be determined by comparing the sizes of the prediction scores. For example, if a higher score is positively correlated with a better one, when the first prediction score is greater than the second prediction score, the comparison result is that the latest query plan corresponding to the first prediction score is the optimal plan, and when the second prediction score is greater than the first prediction score, the comparison result is that the benchmark plan corresponding to the second prediction score is the optimal plan, and so on for other cases.
[0071] See also Figure 5 , is a flowchart of a plan execution method for database query optimization provided in Example 5 of the present application, such as Figure 5 As shown, the above step S401 uses the pre-trained score estimation model to perform score prediction on the latest query plan and the benchmark plan respectively to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan, which may include the following steps:
[0072] Step S501, based on the scoring estimation model, respectively perform performance analysis on the latest query plan and the benchmark plan to obtain the first indicator value corresponding to each indicator in the preset performance indicator set in the latest query plan and the second indicator value corresponding to each indicator in the benchmark plan.
[0073] In the embodiment of the present application, the score estimation model is a weight model, which specifically performs the task of weighted summation, wherein the weight parameters are trained, the weight parameters are multiplied by the corresponding data values respectively, and then the multiplication results are summed.
[0074] Among them, for query plans, the performance of the query plans is mainly examined during evaluation. Performance is opposite to cost, that is, the stronger the performance, the lower the cost. Some known indicators with greater influence are used as preset indicators to form a preset performance indicator set. By performing performance analysis on the query plan, the indicator value corresponding to each preset indicator can be obtained. Among them, the larger the indicator value, the stronger the performance, that is, the lower the cost.
[0075] Step S502, based on the preset weight corresponding to each indicator in the score estimation model, the first indicator value in the latest query plan is weighted summed to obtain the sum result as the first predicted score, and the second indicator value in the benchmark plan is weighted summed to obtain the sum result as the second predicted score.
[0076] In the embodiment of the present application, the preset weight is the weight parameter described in the above step S501. The preset weight is a trained value. The index value is weighted and summed using the preset weight to finally obtain a predicted score that can better characterize the performance of the query plan.
[0077] For example, the set of indicators after performance analysis in a query plan is A = {α1, α2, ...., α n}, where α n It represents the indicator value corresponding to the nth indicator, and the predicted score is V(P). The calculation formula is as follows:
[0078]
[0079] In the formula, w i Represents the weight of the i-th indicator.
[0080] Among them, for the score estimation model in this embodiment, a weight model is adopted, and the calculation process has low requirements on resources and time. Therefore, there is no need to calculate the estimation cost of the score estimation model, which can effectively improve the accuracy of the score and is easier to promote and use.
[0081] See also Figure 6 , is a flow chart of a plan execution method for database query optimization provided in Example 6 of the present application. Figure 6 As shown, obtaining a comparison result according to the first prediction score and the second prediction score in the above step S402 may include the following steps:
[0082] Step S601, normalize the first prediction score to obtain a normalized first prediction score, and normalize the second prediction score to obtain a normalized second prediction score.
[0083] Step S602: If the normalized first prediction score is greater than the normalized second prediction score, determining that the latest query plan is better is the comparison result.
[0084] Step S603: If the normalized first prediction score is not greater than the normalized second prediction score, determining that the baseline plan is better is the comparison result.
[0085] In the embodiment of the present application, since the span of the above-mentioned prediction score is very large, the test score is normalized so that the data is mapped to the interval of [0,1], which is helpful for subsequent comparison operations. For example, the minimum-maximum normalization (MMN) on log(V(P)) is used to map the data to the interval of [0,1]. Among them, the larger the prediction score, the stronger the performance of the corresponding query plan, which means that the query plan is better.
[0086] See also Figure 7 , is a flow chart of a method for executing a plan for optimizing database query provided in Embodiment 7 of the present application. Figure 7 As shown, based on the use of the scoring estimation model, after the current query is executed, the plan execution method further includes the following steps:
[0087] Step S701, obtaining the execution result of the current query execution.
[0088] Step S702, if the latest query plan is executed, the plan score of the latest query plan is determined according to the execution result, and the score estimation model is trained according to the difference between the plan score of the latest query plan and the first predicted score to obtain a trained score estimation model.
[0089] Step S703, if the benchmark plan is executed, the plan score of the benchmark plan is determined according to the execution result, and the score estimation model is trained according to the difference between the plan score of the benchmark plan and the second predicted score to obtain a trained score estimation model.
[0090] In the embodiment of the present application, for the above-mentioned rating estimation model, the execution results of each round of queries can be used for optimization to implement cyclic training of the model, which helps to obtain a more accurate rating estimation model.
[0091] like Figure 8As shown, it is an overall architecture diagram of an execution method provided in Example 7 of the present application. If the query optimizer generates a new plan P1, the new plan P1 and the baseline plan P2 are used as inputs of the plan comparator, and then the score estimation model is called to obtain the comprehensive score of the two plans. After the score comparison is completed, the plan with the highest score is used as the best plan P* as output. In the background of the system, the model trainer collects the execution information of plan P* into the model trainer, and continuously trains the score estimation model and updates it regularly. This enables the score estimation model to be continuously trained and makes the score estimation more accurate over time without affecting normal database services. For example, the score estimation model is trained with a data set in the form of (P, label) for all pairs of execution plans for each query, where the label represents the plan score, and the learning goal is to fit the output of the predicted score.
[0092] The feature vector representing the query plan in the score estimation model in the embodiment of the present application does not include the estimated cost, so it can avoid introducing additional costs to a certain extent, thereby improving the accuracy of the model. For the feature vector of the query plan, the model can first flatten the tree-like vector, and then input it into the neural network to generate the score estimation model, and output the predicted score.
[0093] Corresponding to the plan execution method for database query optimization in the above embodiment, Fig. 9 The structural block diagram of the plan execution device for database query optimization provided by the eighth embodiment of the present application is shown. The plan execution device is applied to Figure 1 The server in the embodiment of the present invention is connected to the client to obtain the target query statement sent by the client. In addition, the server has the function of providing query services, that is, it has a query optimizer for generating query plans and an engine for executing query plans. For ease of explanation, only the parts related to the embodiment of the present application are shown.
[0094] See also Fig. 9 , the plan execution device includes:
[0095] A plan generation module 91, used to generate a latest query plan using a query optimizer when entering a current query based on a target query statement;
[0096] A plan comparison module 92 is used to use the query plan executed by the target query statement in the previous query as a benchmark plan, compare the latest query plan with the benchmark plan, and obtain a comparison result;
[0097] The plan execution module 93 is used to execute the current query based on the benchmark plan when the comparison result shows that the benchmark plan is better, and to execute the current query based on the latest query plan when the comparison result shows that the latest query plan is better.
[0098] Optionally, the plan execution device further includes:
[0099] A query detection module is used to detect whether the current query is the first query of the target query statement;
[0100] A first query module, configured to execute the current query based on the latest query plan after using the query optimizer to generate the latest query plan if it is detected that the current query is the first query of the target query statement; or
[0101] The execution module, if detecting that the current query is not the first query of the target query statement, executes the step of using the query plan executed in the previous query of the target query statement as the benchmark plan.
[0102] Optionally, the plan generating module 91 includes:
[0103] A statistical information acquisition unit, used to acquire current statistical information and a first query plan group generated by the query optimizer for the target query statement under the historical statistical information corresponding to the previous query;
[0104] The plan generating unit is used to generate a second query plan group for the target query statement using a query optimizer based on the current statistical information, and use the query plan in the second query plan group that is different from the first query plan group as the latest query plan.
[0105] Optionally, the plan comparison module 92 includes:
[0106] A score prediction unit, used to use a score estimation model to perform score prediction on the latest query plan and the benchmark plan respectively, to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan;
[0107] The comparison result determination unit is used to obtain a comparison result according to the first prediction score and the second prediction score.
[0108] Optionally, the rating prediction unit includes:
[0109] The performance analysis subunit is used to perform performance analysis on the latest query plan and the benchmark plan respectively based on the scoring estimation model, and obtain the first indicator value corresponding to each indicator in the preset performance indicator set in the latest query plan and the second indicator value corresponding to each indicator in the benchmark plan;
[0110] The scoring prediction subunit is used to perform weighted summation of the first indicator value in the latest query plan based on the preset weight corresponding to each indicator in the scoring estimation model, and obtain the sum result as the first predicted score, and perform weighted summation of the second indicator value in the benchmark plan, and obtain the sum result as the second predicted score.
[0111] Optionally, the comparison result determination unit includes:
[0112] A normalization subunit, configured to perform normalization processing on the first prediction score to obtain a normalized first prediction score, and perform normalization processing on the second prediction score to obtain a normalized second prediction score;
[0113] A first result determination subunit, configured to determine that the latest query plan is better as a comparison result if the normalized first prediction score is greater than the normalized second prediction score;
[0114] The second result determination subunit is used to determine that the baseline plan is better as the comparison result if the normalized first prediction score is not greater than the normalized second prediction score.
[0115] Optionally, the plan execution device further includes:
[0116] An execution result acquisition module is used to obtain the execution result of the current query execution after the current query execution;
[0117] A first training module is used for determining a plan score of the latest query plan according to an execution result if the latest query plan is executed, and training a score estimation model according to a difference between the plan score of the latest query plan and the first predicted score to obtain a trained score estimation model;
[0118] The second training module is used to determine the plan score of the benchmark plan based on the execution result if the benchmark plan is executed, and to train the score estimation model based on the difference between the plan score of the benchmark plan and the second predicted score to obtain a trained score estimation model.
[0119] It should be noted that the information interaction, execution process and other contents between the above-mentioned modules are based on the same concept as the method embodiment of the present application. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0120] Fig.10 This is a schematic diagram of the structure of a computer device provided in Example 9 of the present application. Fig.10 As shown, the computer device of this embodiment includes: at least one processor ( Fig.10 Only one is shown), a memory, and a computer program stored in the memory and executable on at least one processor, wherein when the processor executes the computer program, the steps in any of the above-mentioned database query optimization plan execution method embodiments are implemented.
[0121] The computer device may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that Fig.10This is merely an example of a computer device and does not constitute a limitation on the computer device. The computer device may include more or fewer components than those shown in the figure, or a combination of certain components, or different components. For example, it may also include a network interface, a display screen, and an input device.
[0122] The processor may be a CPU, or other general-purpose processors, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0123] The memory includes a readable storage medium, an internal memory, etc., wherein the internal memory may be the memory of a computer device, and the internal memory provides an environment for the operation of an operating system and computer-readable instructions in the readable storage medium. The readable storage medium may be a hard disk of a computer device, and in other embodiments may also be an external storage device of a computer device, for example, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on a computer device. Further, the memory may also include both an internal storage unit of a computer device and an external storage device. The memory is used to store an operating system, an application program, a boot loader (BootLoader), data, and other programs, such as program codes of computer programs, etc. The memory may also be used to temporarily store data that has been output or is to be output.
[0124] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device is divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned device can refer to the corresponding process in the above-mentioned method embodiment, which will not be repeated here. If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned method embodiment when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device capable of carrying computer program code, recording medium, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.
[0125] The present application implements all or part of the processes in the above-mentioned embodiment method, and may also be completed through a computer program product. When the computer program product runs on a computer device, the computer device can implement the steps in the above-mentioned method embodiment when executing the computer program product.
[0126] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0127] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0128] In the embodiments provided in the present application, it should be understood that the disclosed devices / computer equipment and methods can be implemented in other ways. For example, the device / computer equipment embodiments described above are only schematic, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0129] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0130] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A plan execution method for database query optimization, characterized in that: The plan execution method includes: When entering the current query based on the target query statement, use the query optimizer to generate the latest query plan; Using the query plan executed by the target query statement in the previous query as a benchmark plan, comparing the latest query plan with the benchmark plan, and obtaining a comparison result; When the comparison result shows that the benchmark plan is better, the current query is executed based on the benchmark plan; when the comparison result shows that the latest query plan is better, the current query is executed based on the latest query plan; The comparing the latest query plan with the benchmark plan to obtain a comparison result includes: Using a score estimation model to respectively perform score prediction on the latest query plan and the benchmark plan, to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan; A comparison result is obtained according to the first prediction score and the second prediction score.
2. The plan execution method according to claim 1, characterized in that: The plan execution method also includes: Detecting whether the current query is the first query of the target query statement; If it is detected that the current query is the first query of the target query statement, then after using the query optimizer to generate a latest query plan, executing the current query based on the latest query plan; or If it is detected that the current query is not the first query of the target query statement, the step of using the query plan executed in the previous query of the target query statement as a reference plan is performed.
3. The plan execution method according to claim 1, characterized in that: The use of the query optimizer to generate the latest query plan includes: Obtaining current statistical information and a first query plan group generated by the query optimizer for the target query statement under historical statistical information corresponding to a previous query; Based on the current statistical information, the query optimizer is used to generate a second query plan group for the target query statement, and a query plan in the second query plan group that is different from the first query plan group is used as the latest query plan.
4. The plan execution method according to claim 1, characterized in that: The using the score estimation model to perform score prediction on the latest query plan and the benchmark plan respectively to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan, includes: Based on the scoring estimation model, the latest query plan and the benchmark plan are respectively subjected to performance analysis to obtain a first indicator value corresponding to each indicator in a preset performance indicator set in the latest query plan and a second indicator value corresponding to each indicator in the benchmark plan; Based on the preset weight corresponding to each indicator in the score estimation model, the first indicator value in the latest query plan is weighted and summed to obtain the first predicted score, and the second indicator value in the benchmark plan is weighted and summed to obtain the second predicted score.
5. The plan execution method according to claim 1, characterized in that: The obtaining a comparison result according to the first prediction score and the second prediction score includes: Normalizing the first prediction score to obtain a normalized first prediction score, and normalizing the second prediction score to obtain a normalized second prediction score; If the normalized first prediction score is greater than the normalized second prediction score, determining that the latest query plan is better as a comparison result; If the normalized first prediction score is not greater than the normalized second prediction score, then determining that the baseline plan is better is the comparison result.
6. The plan execution method according to claim 1, characterized in that: After the current query is executed, the plan execution method further includes: Obtaining the execution result of the current query execution; If the latest query plan is executed, determining a plan score of the latest query plan according to the execution result, and training the score estimation model according to a difference between the plan score of the latest query plan and the first predicted score to obtain a trained score estimation model; If the benchmark plan is executed, the plan score of the benchmark plan is determined according to the execution result, and the score estimation model is trained according to the difference between the plan score of the benchmark plan and the second predicted score to obtain a trained score estimation model.
7. A plan execution device for database query optimization, characterized in that: The plan execution device comprises: A plan generation module is used to generate a latest query plan using a query optimizer when entering the current query based on the target query statement; A plan comparison module is used to use the query plan executed by the target query statement in the previous query as a benchmark plan, compare the latest query plan with the benchmark plan, and obtain a comparison result; A plan execution module, configured to execute the current query based on the benchmark plan when the comparison result shows that the benchmark plan is better, and to execute the current query based on the latest query plan when the comparison result shows that the latest query plan is better; The plan comparison module includes: A score prediction unit, configured to use a score estimation model to perform score prediction on the latest query plan and the benchmark plan respectively, to obtain a first prediction score corresponding to the latest query plan and a second prediction score corresponding to the benchmark plan; A comparison result determining unit is used to obtain a comparison result according to the first prediction score and the second prediction score.
8. A computer device, characterized in that: The computer device includes a processor, a memory, and a computer program stored in the memory and executable on the processor, and when the processor executes the computer program, the plan execution method according to any one of claims 1 to 6 is implemented.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the plan execution method according to any one of claims 1 to 6 is implemented.
