A query execution optimization method based on query plan template cache

CN118170797BActive Publication Date: 2026-10-09CHINESE PEOPLES LIBERATION ARMY UNIT 93184
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311803848.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-12-26
Publication Date
2026-10-09
Estimated Expiration
2043-12-26

AI Technical Summary

Technical Problem

[0005]本发明的目的是提供一种基于查询计划模版缓存的查询执行优化方法,以解决目前的查询执行优化方法存在算法复杂性高、效率低以及无法准确识别查询语句间的因果关系的问题

Benefits of technology

本方法通过在查询计划模版中加入因果关系,可以明确查询语句间的因果联系,以确定查询语句的执行顺序,进而数据库系统可以更有效的避免不必要的等待和计算,从而提高查询执行的效率,其次,通过了解查询语句间的因果关系,数据库系统可以更有效的重用已计算的结果,有助于减少不必要的计算和数据传输,降低了资源消耗,从而解决目前的查询执行优化方法存在算法复杂性高、效率低以及无法准确识别查询语句间的因果关系的问题。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118170797B_ABST
    Figure CN118170797B_ABST
Patent Text Reader

Abstract

The specification discloses a query execution optimization method based on a query plan template cache, relates to the technical field of database management, and comprises the following steps: obtaining training set data based on real labels of a number of query statements in a query load and an optimized query statement sequence; inputting the training set data into a target original model for model training to obtain a target model; inputting a to-be-optimized query load into the target model to obtain an optimized query load causal graph; generating a query plan template based on the optimized query load causal graph and the cost of query execution; and saving the query plan template into a query plan template cache, so as to solve the problems of high algorithm complexity, low efficiency and the inability to accurately identify the causal relationship between query statements in the current query execution optimization method.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of database management technology, specifically relating to a query execution optimization method based on query plan template caching. Background Technology

[0002] Query execution optimization is a crucial area in database management systems. Database queries typically use Structured Query Language (SCL), but SCL execution for complex query loads suffers from time consumption and inefficiency. Query execution optimization aims to improve the performance and efficiency of database queries. In the modern information age, databases play a vital role in storing and managing massive amounts of data. Due to the sheer volume of data in databases, query performance has become a critical issue. The goal of query execution optimization is to design and select the optimal execution plan to respond quickly and efficiently to user query requests.

[0003] The field of database query execution optimization has been continuously evolving. Current methods based on deep reinforcement learning rearrange queries within a query load, using attributes such as query time and efficiency as rewards to optimize the execution order of the query load through training. However, these methods suffer from excessively long training times, and the models do not easily converge to the optimal execution order for the query load. Furthermore, these methods struggle to uncover causal relationships between queries, such as how the time consumed to query a single field value can be affected by the order in which multiple tables are queried and the different table join methods. Currently, no effective solutions have been proposed to address these issues.

[0004] Therefore, current query execution optimization methods suffer from high algorithm complexity, low efficiency, and an inability to accurately identify causal relationships between query statements. Summary of the Invention

[0005] The purpose of this invention is to provide a query execution optimization method based on query plan template caching, so as to solve the problems of high algorithm complexity, low efficiency and inability to accurately identify causal relationships between query statements in current query execution optimization methods.

[0006] To achieve the above objectives, the present invention adopts the following technical solution: On the one hand, this specification provides a query execution optimization method based on query plan template caching, including: Step 102: Obtain training set data based on the true labels of several query statements in the query load and the optimized query statement order; Step 104: Input the training set data into the target original model for model training to obtain the target model; Step 106: Input the query load to be optimized into the target model to obtain the optimized query load cause-effect graph; Step 108: Based on the optimized query load cause-effect graph and the cost of query execution, generate a query plan template; Step 110: Save the query plan template to the query plan template cache.

[0007] On the other hand, this specification provides a query execution optimization apparatus based on query plan template caching, comprising: The training data acquisition module is used to obtain training set data based on the true labels of several query statements in the query load and the optimized query statement order; The original model training module is used to input the training set data into the target original model for model training to obtain the target model; The query load cause-effect graph optimization module is used to input the query load to be optimized into the target model and obtain the optimized query load cause-effect graph. The query plan template generation module is used to generate a query plan template based on the optimized query load cause-effect graph and the cost of query execution. The query plan template storage module is used to save the query plan template to the query plan template cache.

[0008] Based on the above technical solution, this specification can achieve the following technical effects: This method, by incorporating causal relationships into the query plan template, clarifies the causal connections between query statements, thereby determining the execution order of the queries. This allows the database system to more effectively avoid unnecessary waiting and computation, thus improving query execution efficiency. Furthermore, by understanding the causal relationships between query statements, the database system can more effectively reuse calculated results, helping to reduce unnecessary computation and data transmission, and lowering resource consumption. This addresses the problems of high algorithm complexity, low efficiency, and the inability to accurately identify causal relationships between query statements in current query execution optimization methods. Attached Figure Description

[0009] Figure 1 This is a flowchart illustrating a query execution optimization method based on query plan template caching in one embodiment of the present invention.

[0010] Figure 2 This is a schematic diagram of querying a load cause-effect graph in one embodiment of the present invention.

[0011] Figure 3 This is a flowchart illustrating a query execution optimization method based on query plan template caching in one embodiment of the present invention.

[0012] Figure 4 This is a schematic diagram of the mapping in one embodiment of the present invention.

[0013] Figure 5 This is a schematic diagram of a query execution optimization device based on query plan template caching in one embodiment of the present invention.

[0014] Figure 6 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present invention. Detailed Implementation

[0015] The present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. The advantages and features of the present invention will become clearer from the following description and claims. It should be noted that the drawings are all in a very simplified form and are not to a precise scale, and are only used to facilitate and clarify the illustration of the embodiments of the present invention.

[0016] It should be noted that, in order to clearly illustrate the content of this invention, several embodiments are provided to further explain different implementations of the invention. These embodiments are enumerated rather than exhaustive. Furthermore, for the sake of brevity, content mentioned in the preceding embodiments is often omitted in the following embodiments. Therefore, content not mentioned in the later embodiments can be referred to in the preceding embodiments. Example 1

[0017] Please refer to Figure 1 , Figure 1 The illustration shows a query execution optimization method based on query plan template caching provided in this embodiment. In this embodiment, the method includes: Step 102: Obtain training set data based on the true labels of several query statements in the query load and the optimized query statement order; Step 104: Input the training set data into the target original model for model training to obtain the target model; In this embodiment, the target original model includes an encoding part and a decoding part.

[0018] Specifically, the encoding part of the original target model consists of a causal graph construction layer and a graph neural network layer, while the backbone network of the decoding part is a cross-attention mechanism layer. The input to the encoding part is a vectorized representation of the query load. The output is the graph representation of the causal graph of the query load learning results.

[0019] In this embodiment, the method further includes the following steps before step 104: The attributes of several query statements in the query load are processed using one-hot encoding to obtain a vectorized representation of the query load.

[0020] Specifically, the attributes of the n query statements in the query load are processed into a vectorized representation using one-hot encoding, and the vectorized representation is expressed as follows: .

[0021] In this embodiment, one implementation of step 104 is as follows: Step 202: Based on the cause-effect graph construction layer of the encoded part and several query statements in the query load, construct a query load cause-effect graph; In this embodiment, one implementation of step 202 is as follows: Step 302: Based on the attribute values ​​of the several query statements, obtain the attribute values ​​of the vertices of the query load cause-effect graph; Specifically, construct a query load cause-effect graph. Treat all attribute values ​​from the query statements as vertex attribute values. Input image .

[0022] Step 304: Concatenate the query statements in pairs and determine whether the vertices corresponding to the two statements can be connected by an edge to obtain the edge of the query load cause-effect graph. In this embodiment, one implementation of step 304 is as follows: The two query statements are concatenated and multiplied by the first weight matrix to map them into a one-dimensional vector; The one-dimensional vector is normalized and the probability of mutual influence between the two query statements is calculated. Determine whether the probability of influence is greater than a preset threshold. If so, determine whether the two query statements can be connected by an edge and add the directed line segments corresponding to the two query statements to the query load causal graph to obtain the edges of the query load causal graph.

[0023] Specifically, the query statements are concatenated pairwise and then an edge is determined. For example, with... and For example, splicing and Then, with the learnable weight matrix Multiplication maps to a one-dimensional vector. This can be expressed by the following formula:

[0024] in, It is an operation that flattens the original vector into a one-dimensional vector. It is a learnable weight matrix. This is a concatenation operation. Next, the result A is normalized using a softmax layer, limiting the value of A to the range [0, 1], thus calculating the query statement. The query statement was affected. probability of occurrence It can be expressed by the following formula:

[0025] in, It is a non-linear activation function; a preset threshold is used. and To determine whether two vertices are connected by a directed line segment, a comparison can be made using the following formula:

[0026] like Then add directed line segments to the cause-effect graph. Otherwise, do not include it, including directed line segments. The vectorized representation is , , It is a set of vectorized edges in the cause-effect graph; complete the query load cause-effect graph. The construction.

[0027] Step 306: Based on the vertices and edges, obtain the query load causal graph.

[0028] In this embodiment, reference Figure 2 This is a schematic diagram of the cause-effect graph for query load.

[0029] Specifically, query statements in query load It has m attributes constitute, Including selection criteria Query conditions Table name field name field value ,predicate ; query statement , The attributes are respectively , If two query statements , , If a causal relationship exists, then there exists , , It is the set of all causal relationships that exist in the query load.

[0030] Build a query load cause-effect graph Where D represents the set of all queries in the query load cause-effect graph, and the query load cause-effect graph is a directed acyclic graph, such as... Figure 1 As shown, if the query statement , If there is a causal relationship between them, then Connected by a line segment with an arrowhead to , indicates a query statement The occurrence of this affects the query statement The occurrence of; if the query statement , If there is no causal relationship between them, then , No lines are connected between them, that is The occurrence of The probability of this happening is very small.

[0031] Among them, the causal relationship exists in the following situations: by Start, four arrows point to... , , and , Called , , and The common cause; Called Influence The confounding factors; at the same time Too Influence Confounding factors; , and Three arrows pointing at the same time ,express yes , and The common result.

[0032] Step 204: The cause-effect graph construction layer updates the query load cause-effect graph and determines whether the preset number of iterations has been reached. If so, the model training ends and the target model is obtained. Specifically, the maximum number of iterations for model training is ,refer to Figure 3 In the causal graph construction layer, the edges of the causal graph are updated. When the maximum number of iterations is reached, the causal relationship information is output; otherwise, the above process is repeated after passing through the graph neural network layer and the cross-attention mechanism layer.

[0033] Step 206: If the preset number of iterations has not been reached, obtain the neural network learning result based on the graph neural network of the encoded part and the query load causal graph; In this embodiment, one implementation of step 206 is as follows: The query load causal graph is transformed into graph data and input into the graph neural network; The graph neural network aggregates and updates the vertices of the query load causal graph, that is, it learns the potential causal relationships between the query statements and obtains the neural network learning results.

[0034] Specifically, then... The data is converted into graph data and input into a graph neural network to aggregate and update vertices, thereby learning a causal graph. The latent features between vertices, i.e. the potential causal relationships between query statements, are used to output the encoded results. It can be expressed by the following formula:

[0035] in, These are the aggregation and update operations of a graph neural network; It is a learnable weight matrix.

[0036] Step 208: Based on the cross-attention mechanism layer of the decoding part, the learning results of the neural network and the query load of the vectorized representation are fused and correlation calculation is performed to obtain the output result; In this embodiment, one implementation of step 208 is as follows: The neural network learning results are mapped to query vectors; The vectorized query load is mapped to a key vector and a value vector; The similarity between the query vector and the key vector is calculated using the dot product method to obtain the relevance distribution; The correlation distribution is multiplied by the value vector and summed to obtain the output result.

[0037] Specifically, refer to Figure 4 The backbone network of the decoding part is a cross-attention mechanism layer, which introduces a vectorized representation of the query load. The learning outcome is represented by a cause-and-effect graph. This is used to fuse information from two different sources and calculate the correlation between two sequences, thereby better capturing the dependency between the two sequences and achieving more accurate modeling. For example... Figure 3 As shown, specifically, the result of the encoded part pass Mapped to query vector Vectorization pass Mapping and Mapping to key vector Sum value vector It can be expressed by the following formula:

[0038]

[0039]

[0040] in, It is a fully connected neural network. , , These are the learnable weight matrices.

[0041] Next, calculate using the dot product method. and The similarity between them yields the correlation distribution. The deep correlation between the features of two sequences is obtained using the following formula:

[0042] in, Yes Matrix transpose calculation, yes Matrix dimension, divided by To prevent the internal volume of molecules from becoming too large.

[0043] Finally, the correlation distribution AND value vector Multiplying and summing yields a cross-attention representation that incorporates information from two different input sequences. Standardization is then applied to obtain the final output of the encoded part. It can be expressed by the following formula:

[0044] in, It is a non-linear activation function; Based on this, this embodiment uses cross-attention layer computation to allow the model to focus more on more important features and combine input features from different sequences to extract richer and more effective multi-level high-dimensional features.

[0045] Step 210: Input the output results and the true labels of the optimized query statement order into the loss function to obtain the loss value; Step 212: Update several learning weight matrices of the target original model based on the loss function, and return to the causal graph construction layer to update the query load causal graph.

[0046] Specifically, the output of the encoded part That is, the model's prediction results and the true labels. The input to the loss function can be expressed by the following formula:

[0047] Through loss function optimization, backpropagation is performed to the learnable weight matrices in the model, including... , , , , The weight matrix can be learned and updated, thereby updating the different causal graphs generated in the causal graph construction layer; training reaches the maximum number of iterations. After training ends, output and save the best results. The corresponding learnable weight matrices are used to obtain the target model. Step 106: Input the query load to be optimized into the target model to obtain the optimized query load cause-effect graph; Specifically, after the model training is completed, during the model inference process, the query load to be optimized is input into the above model, and the learned weight matrices that have been trained are used to generate the optimized query load causal graph.

[0048] Step 108: Based on the optimized query load cause-effect graph and the cost of query execution, generate a query plan template; In this embodiment, the execution time and cost of the query plan template are obtained through calculation using the query statement; based on Number of times and Expenses, to obtain the aforementioned cost.

[0049] Specifically, a query plan template is generated by querying the causal inferences in the load cause-effect graph and the cost of query execution. The execution time and cost of the plan are calculated using the query execution statements. from Number of times and Considering both aspects of cost, it can be expressed by the following formula:

[0050] in, This indicates the number of pages accessed during execution, indirectly reflecting disk usage. frequency; This indicates the number of tuples that need to be accessed during execution, indirectly reflecting... Expenses, Indicates disk Cost and Weighting factors between overheads.

[0051] Step 110: Save the query plan template to the query plan template cache.

[0052] In this embodiment, the query plan template cache uses key-value pairs to store the query plan templates. Each query plan template is stored as a key-value pair, where the key is the identifier of the query plan template, and the value contains the query load cause-effect graph and execution cost information of each query plan template.

[0053] Specifically, a query plan template is an abstract description of a known, optimized query plan. Each query plan template represents a possible optimization of a query, including different execution plans, execution orders, and join methods. Each query plan template is typically represented by a unique identifier, which can be a string or a number, used to uniquely identify the template.

[0054] By representing causal relationships within the query plan template, causal relationships are ensured to be maintained during query execution. These causal relationships may include whether certain queries need to be executed before other queries, and whether the results of certain queries can be shared or reused. Simultaneously, the execution cost of each query statement is added to the query template cache to optimize query execution, including costs related to computation time, resource utilization, and storage overhead, helping the system select the optimal query plan template.

[0055] During actual query execution, a query plan template is retrieved from the query plan template cache to guide query execution. Based on the new query load, the query load cause-effect graph, and the execution cost of each query statement, a suitable query plan is selected, ensuring that causal relationships are maintained. The execution order of query statements can be inferred from these causal relationships, allowing for query optimization to improve accuracy and efficiency.

[0056] In this embodiment, the query plan template cache allows the database system to store and reuse known query plan templates and the causal relationships between them. This enables more effective query execution optimization, reduces unnecessary query plan generation and optimization time, and improves query execution efficiency. Furthermore, when multiple queries have causal relationships, the query plan template cache can collaboratively optimize these queries to achieve better overall performance. For multiple query statements with causal relationships, some queries may depend on the results of other queries. By adding causal relationships to the query plan template, it can be ensured that when executing related queries, the causal queries are executed first, avoiding invalid or inconsistent results.

[0057] In summary, this method, by incorporating causal relationships into the query plan template, clarifies the causal connections between query statements, thereby determining the execution order of the queries. This allows the database system to more effectively avoid unnecessary waiting and computation, thus improving query execution efficiency. Furthermore, by understanding the causal relationships between query statements, the database system can more effectively reuse calculated results, helping to reduce unnecessary computation and data transmission, and lowering resource consumption. This addresses the problems of high algorithm complexity, low efficiency, and the inability to accurately identify causal relationships between query statements in current query execution optimization methods. Example 2

[0058] Please refer to Figure 5 , Figure 5 The diagram shows a query execution optimization device based on query plan template caching provided in this embodiment, comprising: The training data acquisition module is used to obtain training set data based on the true labels of several query statements in the query load and the optimized query statement order; The original model training module is used to input the training set data into the target original model for model training to obtain the target model; The query load cause-effect graph optimization module is used to input the query load to be optimized into the target model and obtain the optimized query load cause-effect graph. The query plan template generation module is used to generate a query plan template based on the optimized query load cause-effect graph and the cost of query execution. The query plan template storage module is used to save the query plan template to the query plan template cache. Optional, also includes: The vectorized statement module is used to process the attributes of several query statements in the query load using one-hot encoding to obtain a vectorized representation of the query load.

[0059] Optionally, the original model training module includes: The cause-effect graph construction unit is used to construct a query load cause-effect graph based on the cause-effect graph construction layer of the encoding part and several query statements in the query load; Optionally, the causal graph construction unit includes: A vertex construction subunit is used to obtain the attribute values ​​of the vertices of the query load causal graph based on the attribute values ​​of the plurality of query statements. The edge-connection judgment subunit is used to concatenate the several query statements in pairs and determine whether the vertices corresponding to the two statements can be connected to obtain the edges of the query load cause-effect graph. Optionally, the edge-connection judgment sub-unit includes: The two query statements are concatenated and multiplied by the first weight matrix to map them into a one-dimensional vector; The one-dimensional vector is normalized and the probability of mutual influence between the two query statements is calculated. Determine whether the probability of influence is greater than a preset threshold. If so, determine whether the two query statements can be connected by an edge and add the directed line segments corresponding to the two query statements to the query load causal graph to obtain the edges of the query load causal graph.

[0060] The causal graph construction subunit is used to obtain the query load causal graph based on the vertices and edges. The iteration judgment unit is used by the cause-effect graph construction layer to update the query load cause-effect graph and determine whether the preset number of iterations has been reached. If so, the model training ends and the target model is obtained. The aggregation update unit is used to obtain the neural network learning result based on the graph neural network of the encoded part and the query load causal graph if the preset number of iterations has not been reached. Optionally, the aggregation update unit includes: The graph transformation subunit is used to transform the query load causal graph into graph data and input it into the graph neural network; The aggregation and update subunit is used by the graph neural network to aggregate and update the vertices of the query load causal graph, that is, to learn the potential causal relationships between the several query statements and obtain the neural network learning results.

[0061] The result fusion unit is used to fuse the neural network learning results and the query load of the vectorized representation based on the cross-attention mechanism layer of the decoding part, and perform correlation calculation to obtain the output result; Optionally, the result fusion unit includes: The result mapping subunit is used to map the learning result of the neural network into a query vector; The load mapping subunit is used to map the vectorized query load into a key vector and a value vector; The similarity calculation subunit calculates the similarity between the query vector and the key vector using a dot product method to obtain the relevance distribution; The summation subunit is used to multiply the correlation distribution with the value vector and sum them to obtain the output result.

[0062] The loss calculation unit is used to input the output results and the true labels of the optimized query statement order into the loss function to obtain the loss value; The model update unit is used to update several learning weight matrices of the target original model based on the loss function, and return to the causal graph construction layer to update the query load causal graph.

[0063] This embodiment also includes: The weight matrix saving module is used to output and save several learning weight matrices corresponding to the minimum loss value.

[0064] In this embodiment, the query plan template cache uses key-value pairs to store the query plan templates. Each query plan template is stored as a key-value pair, where the key is the identifier of the query plan template, and the value contains the query load cause-effect graph and execution cost information of each query plan template.

[0065] The execution time and cost of the query plan template are calculated using the query statement; based on Number of times and Expenses, to obtain the aforementioned cost.

[0066] Based on this, this work can clarify the causal relationships between query statements by adding causal relationships to the query plan template, thereby determining the execution order of the query statements. As a result, the database system can more effectively avoid unnecessary waiting and calculation, thus improving the efficiency of query execution. Secondly, by understanding the causal relationships between query statements, the database system can more effectively reuse the calculated results, which helps to reduce unnecessary calculations and data transmission, and reduce resource consumption. This solves the problems of high algorithm complexity, low efficiency, and inability to accurately identify the causal relationships between query statements in current query execution optimization methods.

[0067] Example 3 Please refer to Figure 6 This embodiment provides an electronic device including a processor, an internal bus, a network interface, memory, and non-volatile memory, and may also include other hardware required for business operations. The processor reads the corresponding computer program from the non-volatile memory into memory and then runs it, forming a query execution optimization method based on query plan template caching at the logical level. Of course, in addition to software implementation, this specification does not exclude other implementation methods, such as logic devices or a combination of hardware and software, etc., that is, the execution subject of the following processing flow is not limited to individual logic units, but can also be hardware or logic devices.

[0068] Network interfaces, processors, and memory can be interconnected via a bus system. These buses can be categorized as address buses, data buses, control buses, etc.

[0069] Memory is used to store programs. Specifically, programs may include program code, which includes computer operation instructions. Memory may include read-only memory and random access memory, and provides instructions and data to the processor.

[0070] The processor is used to execute the program stored in the aforementioned memory, and specifically perform the following: Step 102: Obtain training set data based on the true labels of several query statements in the query load and the optimized query statement order; Step 104: Input the training set data into the target original model for model training to obtain the target model; Step 106: Input the query load to be optimized into the target model to obtain the optimized query load cause-effect graph; Step 108: Based on the optimized query load cause-effect graph and the cost of query execution, generate a query plan template; Step 110: Save the query plan template to the query plan template cache.

[0071] A processor may be an integrated circuit chip with signal processing capabilities. In implementation, the steps of the above method can be completed through the processor's integrated hardware logic circuits or software instructions.

[0072] Based on the same invention, embodiments of this specification also provide a computer-readable storage medium that stores one or more programs, which, when executed by an electronic device including multiple applications, cause the electronic device to perform... Figures 1 to 4 A corresponding embodiment provides a query execution optimization method based on query plan template caching.

[0073] Those skilled in the art will understand that the embodiments of this specification can be provided as methods, systems, or computer program products. Therefore, this specification may take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this specification may take the form of a computer program product embodied on one or more computer-readable storage media containing computer-usable program code.

[0074] Furthermore, the specific implementation of the above-mentioned device is basically similar to the method implementation, so the description is relatively simple. For relevant details, please refer to the description of the method implementation. Moreover, it should be noted that in each module of the system of this application, the components are logically divided according to the functions to be implemented. However, this application is not limited to this and can re-divide or combine the components as needed.

[0075] The various embodiments in this specification are described in a progressive manner. The same or similar parts between the various embodiments can be referred to each other. Each embodiment focuses on describing the differences between it and other embodiments.

[0076] The foregoing has described specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps described in the claims may be performed in a different order than those shown in the embodiments and still achieve the desired result. Furthermore, the specific order or sequential order shown in the drawings is not necessarily required to achieve the desired result; in some embodiments, multitasking and parallel processing are possible or may be advantageous.

[0077] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.

Claims

1. A query execution optimization method based on query plan template caching, characterized in that, include: The training set data is obtained based on the true labels of several query statements in the query load and the optimized query statement order. The training set data is input into the target original model for model training to obtain the target model; the target original model includes an encoding part and a decoding part. Before inputting the training set data into the target original model for model training and obtaining the target model, the method further includes processing the attributes of several query statements in the query load using one-hot encoding to obtain a vectorized representation of the query load. The step of inputting the training set data into the target original model for model training to obtain the target model includes: Based on the cause-effect graph construction layer of the encoded part and several query statements in the query load, a query load cause-effect graph is constructed; The cause-effect graph construction layer updates the query load cause-effect graph and determines whether the preset number of iterations has been reached. If so, the model training ends and the target model is obtained. If the preset number of iterations is not reached, the neural network learning result is obtained based on the graph neural network of the encoded part and the query load causal graph. The cross-attention mechanism layer of the decoding part fuses the neural network learning results and the query load of the vectorized representation and performs correlation calculations to obtain the output result; Input the output results and the true labels of the optimized query statement order into the loss function to obtain the loss value; Based on the loss function, several learning weight matrices of the target original model are updated, and the result is returned to the causal graph construction layer to update the query load causal graph. Input the query load to be optimized into the target model to obtain the optimized query load cause-effect graph; The steps of constructing a query load causal graph based on the causal graph construction layer of the encoded part and several query statements in the query load include: Based on the attribute values ​​of the aforementioned query statements, the attribute values ​​of the vertices of the query load cause-effect graph are obtained. The query statements are concatenated in pairs, and it is determined whether the vertices corresponding to the two statements can be connected by an edge to obtain the edge of the query load cause-effect graph. Based on the vertices and edges, a query load causal graph is obtained; Based on the optimized query load cause-effect graph and the cost of query execution, a query plan template is generated; Save the query plan template to the query plan template cache; During the actual query execution process, the query plan template is retrieved from the query plan template cache to guide the query execution.

2. The method according to claim 1, characterized in that, The step of concatenating the plurality of query statements in pairs and determining whether the vertices corresponding to the two statements can be connected by an edge to obtain the edge of the query load cause-effect graph includes: The two query statements are concatenated and multiplied by the first weight matrix to map them into a one-dimensional vector; The one-dimensional vector is normalized and the probability of mutual influence between the two query statements is calculated. Determine whether the probability of influence is greater than a preset threshold. If so, determine whether the two query statements can be connected by an edge and add the directed line segments corresponding to the two query statements to the query load causal graph to obtain the edges of the query load causal graph.

3. The method according to claim 1, characterized in that, If the preset number of iterations is not reached, the step of obtaining the neural network learning result based on the graph neural network of the encoded part and the query load causal graph includes: The query load causal graph is transformed into graph data and input into the graph neural network; The graph neural network aggregates and updates the vertices of the query load causal graph, that is, it learns the potential causal relationships between the query statements and obtains the neural network learning results.

4. The method according to claim 1, characterized in that, The step of fusing the neural network learning results and the query load of the vectorized representation based on the cross-attention mechanism layer of the decoding part, and performing correlation calculation to obtain the output result includes: The neural network learning results are mapped to query vectors; The vectorized query load is mapped to a key vector and a value vector; The similarity between the query vector and the key vector is calculated using the dot product method to obtain the relevance distribution; The correlation distribution is multiplied by the value vector and summed to obtain the output result.

5. The method according to claim 1, characterized in that, After the model training is completed, the process also includes outputting and saving several learning weight matrices corresponding to the minimum loss value.

6. The method according to claim 1, characterized in that, The execution time and cost of the query plan template are calculated using the query statement; the cost is obtained based on the number of I / O operations and CPU overhead.

7. The method according to claim 1, characterized in that, The query plan template cache uses key-value pairs to store the query plan templates. Each query plan template is stored as a key-value pair, where the key is the identifier of the query plan template, and the value contains the query load cause-effect graph and execution cost information for each query plan template.

Citation Information

Patent Citations

  • Method for automatically generating SQL (Structured Query Language) template by inquiring text

    CN117149959A

  • Query engine selection

    US20190026335A1