A query optimization method based on tree attention and base perception and a storage medium

By optimizing the query execution plan through tree attention mechanism and cardinality-aware DQN model, the problem of inaccurate calculation of long-term costs in existing technologies is solved, and more efficient query optimization is achieved.

CN116483863BActive Publication Date: 2025-11-04UNIV OF ELECTRONICS SCI & TECH OF CHINA
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202310551661.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-16
Publication Date
2025-11-04
Estimated Expiration
2043-05-16

AI Technical Summary

Technical Problem

Existing technologies fail to effectively consider the tree-like structure and cardinality of long-term costs when generating query execution plans, leading to inaccurate calculations.

Method used

We employ a tree attention mechanism and cardinality-aware approach, training a DQN model using the reinforcement learning DQN algorithm, and combining state features, action features, and cardinality feature networks to optimize the query execution plan.

Benefits of technology

It improves the accuracy and efficiency of query execution plans, enabling the generation of better query plans within a limited time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116483863B_ABST
    Figure CN116483863B_ABST
Patent Text Reader

Abstract

The application relates to the field of database query, and provides a query optimization method based on tree attention and base number perception and a storage medium.The main purpose is to solve the problem that the current technical scheme does not consider the base number as the most critical influencing factor in query optimization, thereby leading to inaccurate calculation of long-term cost.The main scheme comprises the following steps: data collection, random generation of a query statement in a database and execution, and acquisition of query execution time; DQN model training, training of a q-network comprising a state feature network, an action feature network and a base number feature network in the DQN by using the data collected in step 1, and obtaining of a DQN model; DQN model application, for a query statement, calculation of long-term rewards by using the DQN model trained in step 2, selection of the maximum value of the long-term rewards, selection of the optimal connection, generation of a complete execution plan, and obtaining of a complete query plan.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the field of database query, and provides a query optimization method based on tree attention and cardinality perception and a storage medium. BACKGROUND

[0002] For a query statement, a database cannot be directly executed. The database needs to first parse the query statement, then generate a corresponding query execution plan by an optimizer, and finally hand over the plan to an execution engine to execute the plan. In the application, an effective solution is provided for how to generate a relatively optimal query plan in a relatively short time for a multi-connection query.

[0003] The technical solution in the prior art closest to the application is as follows:

[0004] 1. Chinese invention patent, patent name: database multi-connection query optimization method based on improved SDD-1 algorithm, application number: CN201110043615.9.

[0005] First, the improved SDD-1 algorithm is executed, and a query execution strategy set is obtained by using the algorithm, and the execution strategy set is used as a basis for generating an initial population of a genetic algorithm. Then, the genetic algorithm is executed, and the global search capability of the genetic algorithm is used to optimize the result obtained by the SDD-1 algorithm. Finally, a relatively ideal query execution strategy is obtained. Specifically, the following steps are included:

[0006] Step 1: setting initial parameters, including initial parameter settings of SDD-1 and the genetic algorithm;

[0007] Step 2: obtaining a query execution strategy set, finding beneficial bidirectional semi-connections from a constructed query graph, and selecting beneficial bidirectional semi-connections from a beneficial bidirectional semi-connection candidate set to a beneficial bidirectional semi-connection set BS, repeating the above steps until there is no beneficial bidirectional semi-connection in the query graph, adding the value of the beneficial bidirectional semi-connection set BS obtained to the execution strategy set ES, and repeating the above steps until the running time reaches N:

[0008] Step 3: constructing an initial population of the genetic algorithm, sequentially performing coding operations on elements in the execution strategy set ES, and taking the obtained result as an initial population of the genetic algorithm;

[0009] Step 4: running the genetic algorithm, repeatedly performing crossover, mutation and selection operations on the population until the running time reaches M;

[0010] Step 5: outputting a query execution strategy, taking the best individual in the population output in step 4 as a final result, and decoding the final result into a query tree, that is, a query execution strategy.

[0011] 2. Chinese invention patent, patent name: a big data real-time query optimization method based on hypergraph and dynamic plan, application number: CN201020231887.2.

[0012] A big data real-time query optimization method based on hypergraph and dynamic plan, including optimal cost model construction process and execution plan space search process. The optimal cost model construction process includes the following steps:

[0013] Step 1: analyze the table data in the metadata server, construct and generate fine-grained column-level statistical information histogram, and store it in the metadata server;

[0014] Step 2: use statistical information to construct the corresponding optimal cost model for generating plan.

[0015] The execution plan space search process includes the following steps:

[0016] Step 1: parse the database query statement, and save the result in the query hypergraph data structure.

[0017] Step 2: initialize the execution plan for a single relationship, and save it in the corresponding dynamic plan table.

[0018] Step 3: define the calculation enumeration strategy: each connected subgraph and connected complement is generated only once;

[0019] Step 4: enumerate connected subgraphs by calculating the field;

[0020] Step 5: find the appropriate connected complement for each connected subgraph;

[0021] Step 6: calculate the cost of each execution plan composed of a pair of connected subgraphs and complements, and update its execution plan according to the cost model;

[0022] Step 7: repeat steps 4-7 until the entire left linear tree execution plan space search is complete, and generate the execution plan tree.

[0023] 3. Chinese invention patent, patent name: a big data real-time query optimization method based on hypergraph and dynamic plan, application number: CN202011351761.3.

[0024] Including the following specific steps:

[0025] Step 1: data collection, randomly generate query statements in the database and execute, split the execution plan tree corresponding to the query statement from the root node, and record the connection relationship of each node;

[0026] Step 2: Model training, encode each node according to the connection relationship of each node to obtain an encoding feature matrix, graph describe each node to obtain a graph description set Edge, initialize the Q-network network parameters in the DQN model, the Q-network network in the DQN model adopts the GAT graph attention network, and the encoding feature matrix and the graph description set Edge are used as network inputs to train the DQN model;

[0027] Step 3: Model application, for a table involved in a query statement, each table is a node, and the graph description and encoding of each node are initialized, and the DQN model trained in step 2 is used to generate the connection of 2 nodes in each step of the query execution plan. At this time, the state is transferred, and the graph description and encoding description are updated until all tables are connected to generate a complete query plan.

[0028] The technical solution one has the following defects:

[0029] 1. Genetic algorithm is essentially a greedy strategy, which is easy to fall into local optimal solution;

[0030] 2. This scheme needs to set the number of genetic algorithm iterations. When the number of iterations is small, a better query execution plan cannot be obtained. When the number of iterations is large, a long algorithm execution time is required, and it cannot be avoided to fall into local optimum.

[0031] 3. The one-dimensional encoding of the query execution plan encoding cannot capture the tree structure information of the query execution plan.

[0032] The technical solution two has the following defects:

[0033] 1. The left linear tree is used to enumerate and search the entire query execution plan space. When the connection relationship of the query statement is very complex, the query execution plan space will be very large, and searching the entire query execution space will consume a lot of time.

[0034] The technical solution three has the following defects:

[0035] 1. The GAT graph attention network is used as the q-network in the DQN model, and the left and right nodes in the entire execution sub-plan are treated with equal importance, ignoring the characteristics of the data set from bottom to top in the execution plan, and it is difficult to completely measure the long-term reward of the execution plan.

[0036] 2. The size of the base query result set, i.e. the number of records in the result, is the most important parameter in the database query cost model. Technical solution three does not make full use of it, which is not conducive to improving the accuracy of the q-network.

[0037] To facilitate a better understanding of the technical background by those skilled in the art, the following further explanation is provided:

[0038] In a database, executing a query requires following a query execution plan. Since a query may involve joining multiple tables, and table joins are commutative and associative, it's necessary to determine which two parts to join each time to form a complete query execution plan. The query execution plan can be abstracted as a Markov decision process, where each state represents a partial join situation, starting with no tables joined. In each state, it's necessary to select which two tables or the join result to join, until only one join result remains in all states, at which point a complete query plan is generated. For example, if we want to join three tables A, B, and C, their join costs are: C(A, B) = 30, C(B, C) = 20, C((A, B), C) = 40, C((B, C), A) = 90. If we greedily choose the connection order with the lowest current cost each time, where C(B, C) = 20 is the lowest cost, then B and C will be connected first, and then A will be connected. This would result in the final connection order being ((B, C), A), with a total connection cost of 110 (i.e., the connection cost between B and C is 20, and the connection cost between (B, C) and A is 90, for a total cost of 110). However, the optimal connection order is actually ((A, B), C), with a total cost of only 70. If we could accurately calculate the long-term cost for each connection, rather than the current single-step cost, we could generate a better connection order. Summary of the Invention

[0039] The purpose of this invention is to solve the problem that the current technical solution does not take into account the tree structure characteristics of the execution plan's data transmission from bottom to top in the long-term cost calculation model, and does not take into account the cardinality, the most critical influencing factor in query optimization, which leads to inaccurate calculation of long-term cost.

[0040] To achieve the above objectives, the present invention employs the following technical means:

[0041] A database multi-join query optimization method based on tree attention mechanism and cardinality awareness includes the following main steps:

[0042] Step 1: Data collection. Randomly generate and execute query statements in the database, and obtain the query execution time.

[0043] Step 2: DQN model training. The query execution plan selection process is abstracted as a Markov decision process, and the reinforcement learning DQN algorithm is used for training. The q-network containing the state feature network, action feature network, and cardinality feature network of DQN is trained using the data collected in Step 1 to obtain the DQN model:

[0044] Step 3: DQN model application, for a query statement, use the DQN model trained in step 2 to calculate the long-term reward respectively, select the maximum value of the long-term reward to select the optimal connection, and then generate a complete execution plan, that is, generate a complete query plan.

[0045] In the above technical solution, step 1 includes the following steps:

[0046] Step 1.1: Data collection, randomly generate query statements in the database and execute.

[0047] Step 1.2: The execution plan is split into a multi-step join selection process, and the execution sub-plan, execution time, and connected table at each step are collected as training data for the q-network.

[0048] Step 1.3: Collect the query statement and execution time as training data for the cardinality estimation model.

[0049] In the above technical solution, the cardinality feature network construction method in step 2 is as follows:

[0050] In step 1, many query statements and their corresponding cardinalities are collected as training data. The structure of the cardinality estimation model is 2 layers of graph convolution layers and 2 layers of fully connected layers. After training, the last layer of fully connected layers is removed, leaving only 2 layers of graph convolution layers and 1 layer of fully connected layers as the cardinality feature network.

[0051] In the above technical solution, the training of the cardinality estimation model includes the following steps:

[0052] Step 2.1.1, for a query statement, encode it into graph structure data;

[0053] Step 2.1.1.1, decompose the query statement into tables, constraint predicates, and join predicates, where each table is a node in the graph, and each join predicate forms an edge in the graph;

[0054] Step 2.1.1.2, one-hot encoding for tables; for constraint predicates, one-hot encoding of involved attributes, one-hot encoding of operators, and constraint values in constraint predicates are directly placed into the encoded graph data;

[0055] Step 2.1.2, after constructing the graph structure data, pass it through 2 layers of graph convolution layers and then through 2 layers of fully connected layers to output the cardinality;

[0056] Step 2.1.3: Based on the cardinality estimation model in step 2.1, construct a q-network based on tree attention mechanism and cardinality perception, and train it, as follows:

[0057] Step 2.1.3.1, using the data collected in step 1, perform steps 2.1.1-2.1.2 for training, after training, combine this base estimate model into the q-network in DQN, freeze the first 2 layers of graph convolution in step 2.1.2, remove the last layer of output in the output of 2 layers of fully connected layer, only train 1 layer of fully connected layer, and take the output vector as the final output F of the base feature network ccardinality .

[0058] In the above technical solution, the state feature network in step 2:

[0059] The state is to execute the sub-plan, which is represented in the form of a sub-query tree, where all leaf nodes are table nodes, and other nodes are connection nodes (representing the connection relationship of connecting tables). The initial encoding of the table encoding of the table node is the one-hot encoding of the current table, with only one bit being 1 and the rest being 0. The connection encoding of the connection node is the result of the logical AND operation between the left child node encoding and the right child node encoding. Since the encoding is relatively sparse, the table encoding and the connection encoding are respectively formed into table features and initial connection features through a fully connected network.

[0060] A tree attention mechanism is proposed to extract information from the tree structure transmitted from bottom to top.

[0061] Connection feature F' j The features of the left and right child nodes corresponding to the connection feature are calculated by the tree attention mechanism as follows,

[0062] F' J =F J +a L F L +a R F R

[0063] Where connection feature F' J is the current node, F J is the initial connection feature of the current node (the initial connection feature is the feature of the connection encoding after the fully connected layer),

[0064] a L is the attention of the current node and the left child node, F L is the feature of the left child node, which can be a table feature or a connection feature.

[0065] a R is the attention of the current node and the right child node, F R is the feature of the right child node, which is a table feature, and the attention a iThe correlation of the current node and the left and right child nodes is described, and the influence of the left and right child nodes can be effectively measured, and the calculation method is as follows:

[0066]

[0067] Wherein Indicates is the transpose of F i , through the above attention mechanism, the state feature network is calculated from the bottom of the query tree to the top, and the feature vector of the root node at the top is the feature F state extracted by the state feature network finally.

[0068] Table features: Figure 1 The features extracted by the full connection layer in the table encoding.

[0069] Initial connection features: Figure 1 The features extracted by the full connection layer in the connection encoding.

[0070] Connection features: Figure 1 In the above technical scheme, the features extracted by the full connection layer on the left and right child nodes are extracted by the tree attention mechanism described in the foregoing.

[0071] In the above technical scheme, the action in the action feature network in step 2 is the table to be connected, which is first encoded by one-hot, and then the encoded features are extracted by a full connection layer F action .

[0072] In the above technical scheme, the final splicing F state , F cardinality , F action , and then output the final long-term reward through 2 layers of full connection layers.

[0073] In the above technical scheme, the above step 3 specifically includes the following sub-steps:

[0074] Step 3.1: initializing the state of a query statement;

[0075] Step 3.2: using the DQN model (the cardinality estimation model is a part of the DQN model) to select the best connection, and changing the state to the next state after taking the action;

[0076] Step 3.3: repeating step 3.2 until the final state is reached, that is, the final query execution plan is generated.

[0077] Because the above technical means are adopted in the present application, the following beneficial effects are achieved:

[0078] I. Tree attention mechanism is proposed: general graph neural network treats all nodes in the graph equally, while the execution plan is a tree structure by nature with data passing from bottom to top, and the tree attention mechanism can better extract the execution plan features and promote the accuracy of the overall q-network.

[0079] II. Base number-aware q-network is proposed: base number is the most important feature in the traditional cost model, and q-network is actually a cost model that predicts long-term cost, so base number should also be a key factor. In this scheme, a base number estimation model is first trained separately, and then combined into the q-network to further improve the reliability of the long-term cost output by the q-network.

[0080] III. The significance of introducing the base number feature network is that in the cost model of the query optimizer in the current DBMS, the base number is the most important parameter. Intuitively, the smaller the base number in a query process, the smaller the overhead of the query. In this invention, the neural network model in the DQN algorithm can also be understood as a cost model that predicts long-term rewards, so the base number should also be an important parameter. In terms of effect, the DQN model with the base number feature network can extract the features of the query, predict the overall query overhead, and supplement the feature extraction of the neural network model. In addition, in the DQN algorithm in this invention, all action sequences are finite and known. At the beginning, the neural network model should pay more attention to long-term rewards, and when approaching the final state, it should pay more attention to short-term rewards. Using the base number feature network to extract query features can make the neural network model in DQN perceive the complete query features and the position of the current state in the overall sequence, and have a certain long-short-term reward adaptive effect. BRIEF DESCRIPTION OF DRAWINGS

[0081] Figure 1 Tree attention mechanism;

[0082] Figure 2 Base number estimation graph data;

[0083] Figure 3 Q-network structure. DETAILED DESCRIPTION

[0084] The embodiments of the present application will be described in detail below. Although the present application will be described and illustrated by means of some specific embodiments, it should be noted that the present application is not limited to only these embodiments. On the contrary, modifications or equivalent replacements to the present application should be covered in the scope of the claims of the present application.

[0085] Also, for a better understanding of the present application, numerous specific details are given in the following detailed description. The skilled person will understand, however, that the application can be practiced without these specific details.

[0086] The present application provides that in the DQN algorithm, starting from the initial state, actions are performed step by step until the final state is reached. Connecting a table is an action. The final state is that all tables are connected and a complete execution plan is generated. DQN predicts the long-term reward generated by an action for a state through a neural network model. In a state, the actions that can be performed are limited, so the DQN model calculates the long-term reward of each action for the current state, and selects the action with the maximum long-term reward, which can obtain the relatively optimal action sequence in a limited time.

[0087] In the present application, the state corresponds to the tables that have been connected so far, which can be represented by the execution sub-plan. Taking a query that needs to connect four tables A, B, C and D as an example, if (A, B) has been connected so far, the next possible connection is C or D. Therefore, the state at this time is the sub-plan (A, B), and the possible actions are to connect C or D. Therefore, the long-term reward brought by the two connections is calculated through the neural network respectively.

[0088] In the present application, the neural network model is divided into three parts, namely the state feature network, the action feature network and the cardinality feature network. The state feature network extracts the features of the above-mentioned sub-plan (A, B); the action feature network extracts the features of the table C or D to be connected; and the cardinality feature network has been introduced in step 2.

[0089] The present application provides a database multi-connection query optimization method based on tree attention mechanism and cardinality perception, which includes the following main steps:

[0090] Step 1: Data collection, randomly generate query statements in the database and execute them to obtain the query execution time;

[0091] Step 2: DQN model training, abstract the query execution plan selection process into a Markov decision process, train using the reinforcement learning DQN algorithm, use the data collected in step 1 to train the q-network containing the state feature network, the action feature network and the cardinality feature network in DQN, and obtain the DQN model:

[0092] Step 3: DQN model application, for a query statement, use the DQN model trained in step 2 to calculate the long-term reward respectively, and select the value with the maximum long-term reward to select the optimal connection, and then generate a complete execution plan, that is, obtain a complete query plan.

[0093] In the above scheme, step 1 includes the following steps:

[0094] Step 1.1: Data collection, randomly generate query statements in the database and execute.

[0095] Step 1.2: Its execution plan and split into multi-step join selection process, collect the execution sub-plan of each step, execution time, connected table, as the training data of q-network.

[0096] Step 1.3: Collect the query statement and execution time as the training data of the cardinality estimation model.

[0097] In the above scheme, the cardinality feature network construction method in step 2 is as follows:

[0098] In step 1, a lot of query statements and their corresponding cardinalities are collected as training data. The structure of the cardinality estimation model is 2 layers of graph convolution layers and 2 layers of fully connected layers. After training, the last 1 layer of fully connected layer is removed, and only 2 layers of graph convolution layer and 1 layer of fully connected layer are left as the cardinality feature network.

[0099] The above cardinality estimation model can take query as input and cardinality as output. In step 1, a lot of query statements and their corresponding cardinalities are collected as training data. At this time, the structure of the model is 2 layers of graph convolution layers and 2 layers of fully connected layers. After training, the last 1 layer of fully connected layer is removed, and only 2 layers of graph convolution layer and 1 layer of fully connected layer are left as the cardinality feature network. In the q-network in the following text, there are 3 parts of input, among which the query will be encoded as graph structure data and sent to the cardinality feature network. Only through 1 layer of fully connected layer will output F cardinality When training q-network, the parameters of 2 layers of graph convolution layer of cardinality feature network are frozen, and only the parameters of 1 layer of fully connected layer are trained.

[0100] In the above scheme, the training of the cardinality estimation model includes the following steps:

[0101] Step 2.1.1, for a query statement, encode it into graph structure data;

[0102] Step 2.1.1.1, decompose the query statement into tables, constraint predicates and join predicates, where each table is a node in the graph, and each join predicate forms an edge in the graph;

[0103] Step 2.1.1.2, one-hot encoding for tables; for constraint predicates, one-hot encoding of involved attributes, one-hot encoding of operators, and constraint values in constraint predicates are directly put into the formed graph data for encoding;

[0104] Step 2.1.2, after the graph structure data is constructed as a graph structure, the base number is output by 2 layers of graph convolution layers and then by 2 layers of fully connected layers;

[0105] Step 2.1.3: Based on the base number estimation model in step 2.1, a q-network based on tree attention mechanism and base number perception is constructed and trained, as follows:

[0106] Step 2.1.3.1, using the data collected in step 1, perform steps 2.1.1-2.1.2 for training, after training, combine this base number estimation model into the q-network in DQN, freeze the first 2 layers of graph convolution layers in step 2.1.2, remove the last output layer in the 2 layers of fully connected layer output, and only train the 1 layer of fully connected layer, with its output vector as the final output F of the base number feature network cardinality .

[0107] In the above scheme, the state feature network in step 2:

[0108] The state is a sub-plan execution, represented in the form of a subquery tree, where all leaf nodes are table nodes and other nodes are connection nodes (representing the connection relationship of connecting tables), the initial encoding of the table encoding of the table node is the one-hot encoding of the current table, with only 1 bit being 1 and the rest being 0, and the connection encoding of the connection node is the result of the logical AND of the left child node encoding and the right child node encoding. Because the encoding is relatively sparse, the table encoding and the connection encoding are respectively formed into table features and initial connection features through a layer of fully connected network;

[0109] A tree attention mechanism is proposed to extract information of the tree structure transmitted from bottom to top:

[0110] Connection feature F' J The features of the left child node and the right child node corresponding to the connection feature are calculated by the tree attention mechanism as follows,

[0111] F' J = F J +a L F L +a R F R

[0112] Where connection feature F' J is the current node, F J is the initial connection feature of the current node (the initial connection feature is the feature of the connection encoding after the fully connected layer),

[0113] a L is the attention of the current node and the left child node, F LThe feature of the left child node can be a table feature or a connection feature;

[0114] a R The attention of the current node and the right child node, FR is the feature of the right child node, and the feature of the right child node is a table feature, and the attention a i The correlation between the current node and the left and right child nodes is described, which can effectively measure the influence of the left and right child nodes, and the calculation method is as follows:

[0115]

[0116] Wherein represents is the transpose of F i , through the above attention mechanism, the state feature vector of the root node at the top end is calculated from the bottom of the query tree to the top, which is the final feature F state extracted by the state feature network.

[0117] Table feature: Figure 1 The feature extracted by the full connection layer in the table encoding.

[0118] Initial connection feature: Figure 1 The feature extracted by the full connection layer in the connection encoding.

[0119] Connection feature: Figure 1 In the above-mentioned tree attention mechanism, the feature extracted by the full connection layer on the left and right child nodes.

[0120] In the above scheme, the action in the action feature network in step 2 is the table to be connected, which is first encoded by one-hot, and then the encoded feature is extracted by a full connection layer F action .

[0121] In the above scheme, the final splicing F state , F cardjnality , F action , and then output the final long-term reward through 2 layers of full connection layer.

[0122] In the above scheme, the above step 3 specifically includes the following sub-steps:

[0123] Step 3.1: initialize the state of a query sentence;

[0124] Step 3.2: use the DQN model (the cardinality estimation model is a part of the DQN model) to select the best connection, and change the state to the next state after taking the action;

[0125] Step 3.3: Repeat the execution of Step 3.2 until the final state is reached, i.e., the final query execution plan is generated.

Claims

1. A database multi-join query optimization method based on tree attention mechanism and base perception, characterized in that, The method comprises the following main steps: Step 1: data collection, randomly generating query statements in a database and executing, obtaining the query execution time thereof; Step 2: DQN model training, abstracting the query execution plan selection process into a Markov decision process, training using a reinforcement learning DQN algorithm, training the q-network comprising a state feature network, an action feature network and a cardinality feature network in the DQN using the data collected in step 1, and obtaining the DQN model; The cardinality feature network is constructed as follows: In step 1, a plurality of query statements and their corresponding cardinalities are collected as training data, the structure of the cardinality estimation model is 2 layers of graph convolution layers and 2 layers of fully connected layers, when the training is completed, the last layer of fully connected layers is removed, and only the 2 layers of graph convolution layers and the 1 layer of fully connected layers are left as the cardinality feature network; The training of the cardinality estimation model comprises the following steps: Step 2.1.1, for a query statement, encoding it into graph structure data; Step 2.1.1.1, decomposing the query statement into tables, constraint predicates and connection predicates, wherein each table is a node in the graph, and each connection predicate forms an edge in the graph; Step 2.1.1.2, one-hot encoding is adopted for the tables; for the constraint predicates, the properties involved are one-hot encoded, the operators are one-hot encoded, and the constraint values in the constraint predicates are directly put into the formed graph data to form the encoding; Step 2.1.2, after being constructed into graph structure data, the 2 layers of graph convolution layers are passed through, and then the 2 layers of fully connected layers are passed through to output the cardinality; Step 2.1.3: based on the cardinality estimation model in step 2.1, a q-network based on tree attention mechanism and cardinality perception is constructed and trained, and the specific steps are as follows: Step 2.1.3.1, using the data collected in step 1, perform training according to step 2.1.1-step 2.1.2, after training, combine this base estimate model into the q-network in DQN, freeze the first 2 layers of graph convolution in step 2.1.2, remove the last output layer in the output of 2 layers of fully connected layer, only train 1 layer of fully connected layer, and take the output vector as the final output of the base feature network ; Step 3: DQN model application, for a query statement, using the DQN model trained in step 2 to calculate the long-term reward respectively, and selecting the value with the maximum long-term reward to select the optimal connection, and then generating a complete execution plan, that is, generating a complete query plan.

2. The database multi-join query optimization method based on tree attention mechanism and base perception according to claim 1, characterized in that: The above step 1 comprises the following steps: Step 1.1: data collection, randomly generating query statements in a database and executing; Step 1.2: splitting the execution plan thereof into a multi-step connection selection process, collecting the execution sub-plan, execution time and connected tables at each step as training data of the q-network; Step 1.3: collecting the query statements and execution time as training data of the cardinality estimation model.

3. The database multi-join query optimization method based on tree attention mechanism and base perception according to claim 1, characterized in that: State feature network in step 2: The state is the execution sub-plan, which is represented in the form of a sub-query tree, wherein all leaf nodes are table nodes, and other nodes are connection nodes, the initial encoding of the table encoding of the table node is one-hot encoding of the current table, and only 1 bit is 1 and the rest are 0, the connection encoding of the connection node is the result of the left child node encoding and the right child node encoding, and because the encoding is relatively sparse, the table encoding and the connection encoding are respectively passed through a layer of fully connected network to form the table feature and the initial connection feature; A tree attention mechanism is proposed to extract the information of the tree structure transmitted from bottom to top: Connection features The features from the left and right child nodes corresponding to the connection feature below are computed by the tree attention mechanism as follows, wherein the connection feature is the current node, is the current initial connection feature, for the attention of the current node to the left child node, for the features of the left child node, which can be table features or connection features; attention for the current node and the right child node, the feature of the right child node, the feature of the right child node being a table feature, and the attention The correlation of the current node and the left and right child nodes is described, which can effectively measure the influence of the left and right child nodes, and the calculation method is as follows: Wherein Indicates The transpose of the state feature network is calculated from the bottom of the query tree to the top by the attention mechanism, and the root node feature vector at the top is the final feature extracted by the state feature network ; Wherein, the table feature: the feature extracted by the fully connected layer from the table encoding; Initial connection features: connection encoding features extracted by the fully connected layer; Connection features: features extracted by the fully connected layer on the left and right child nodes, and the extracted features are extracted by the tree attention mechanism.

4. The database multi-join query optimization method based on tree attention mechanism and base perception according to claim 3, characterized in that: In step 2, the action in the action feature network is the table to be connected. First, the table to be connected is encoded by one-hot, and then a fully connected layer is used to extract features from the encoding .

5. The database multi-join query optimization method based on tree attention mechanism and base perception according to claim 4, characterized in that: Final stitching , , , and outputs the final long-term reward through 2 layers of fully connected layers.

6. The database multi-join query optimization method based on tree attention mechanism and base perception according to claim 1, characterized in that: The step 3 specifically includes the following sub-steps: Step 3.1: For a query statement, initialize its state; Step 3.2: Use the DQN model to select the best connection and change the state to the next step state after taking the action; Step 3.3: Repeat step 3.2 until the final state is reached, that is, the final query execution plan is generated.

7. A storage medium, characterized by The processor implements the database multi-connection query optimization method based on the tree attention mechanism and the base perception according to any one of claims 1-6 when executing the program in the storage medium.

Citation Information

Patent Citations

  • Multi-join query optimization method for database based on improved SDD-1 (System for Distributed Database) algorithm

    CN102110158A

  • Database query optimization method based on reinforcement learning and graph attention network

    CN112328578A

  • GPS blind guiding instrument

    CN201823010U

  • Data query optimization processing method and device based on machine learning

    CN115617830A