Domain cost estimation method for query optimization based on graph attention network

By using graph attention network to feature encoding of the physical execution plan tree in PostgreSQL database, the problem of insufficient learning of cross-node dependencies and node-level relationships is solved, and a more accurate cost estimation is achieved.

CN117113075BActive Publication Date: 2025-08-29UNIV OF ELECTRONICS SCI & TECH OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310810875.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-04
Publication Date
2025-08-29
Estimated Expiration
2043-07-04

AI Technical Summary

Technical Problem

There are large errors in the cardinality and cost estimates of existing databases, which are unable to effectively learn cross-node dependence and node-level relationships and fail to fully utilize existing database features.

Method used

Using a graph attention network-based method, the physical execution plan tree of PostgreSQL database is characterized by feature encoding, graph attention network and prior attention matrix are introduced, dependencies between nodes are learned, and database statistical information and pattern information are used for feature encoding.

Benefits of technology

Improve the accuracy of cost estimates and improve the accuracy of cardinality and cost estimates.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117113075B_ABST
    Figure CN117113075B_ABST
Patent Text Reader

Abstract

The present invention discloses a cost estimation method for query optimization based on a graph attention network, comprising the following steps: acquiring data and constructing a training set for a PostgreSQL database; constructing a deep learning network for a cost estimation model; passing the training set into the cost estimation model to train the deep learning network; obtaining a predicted cost based on the data of the query statement to be predicted; and externalizing the cost estimation model to a PostgreSQL database to implement the cost estimation task on the database. The present invention investigates the development of traditional cardinality estimation and cost estimation methods, conducts in-depth research and analysis on the problems existing in existing learning-based cost estimation models, and proposes a cost estimation model based on a graph attention network. Effective feature data such as table cardinality, base table selectivity, and connection selectivity are introduced into feature encoding, thereby improving the accuracy of cost estimation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of database query optimization, and in particular to a cost estimation method in the query optimization field based on a graph attention network. Background Art

[0002] In the field of database query optimization, cost-based query optimizers need to estimate the execution costs of different physical execution plan trees and select the one with the lowest execution cost. Cost estimation primarily involves cardinality estimation and cost models. Cardinality estimation is a key input to cost models, and several common methods exist. Existing databases perform cardinality and cost estimation using methods such as sampling, histograms, data profiling, and wavelets. To simplify design and implementation, these methods employ idealized assumptions that contradict real data, such as uniformity, independence, and inclusion, leading to significant errors.

[0003] In recent years, machine learning methods have emerged to address cardinality and cost estimation problems, effectively improving accuracy. These methods encode features in the physical execution plan tree and use deep learning models to learn the features of the physical execution plan. There are three key points to feature encoding in the physical execution plan tree:

[0004] 1. Dependency between parent and child nodes. During the execution of the physical execution plan tree, the input data of the parent node comes from the child node, so it is directly affected by the child node.

[0005] 2. Cross-node dependencies exist between different nodes, and cross-node dependencies are affected by the node hierarchical relationship. For example, the execution overhead of an intermediate node is related to all its child nodes, and the weight of the impact is related to the hierarchical relationship between the nodes.

[0006] 3. Effectively utilize existing features such as statistical information, pattern information, and query statement execution status in existing databases.

[0007] The existing solution needs to be improved in two key points: 2 and 3. There are problems such as the inability to effectively learn the relationship between cross-node dependencies and node hierarchies and the inability to effectively utilize the existing features of the database. Summary of the Invention

[0008] To address the shortcomings of the above-mentioned background technology, the present invention provides a cost estimation method based on a graph attention network (GAT). Targeting the cost estimation task of the PostgreSQL database, the graph attention network is introduced, and the dependencies between nodes are learned using a priori attention encoding algorithm based on the physical execution plan tree graph structure; the feature encoding of the physical execution plan tree nodes is improved, and more database statistical information is introduced; thereby effectively improving the accuracy of cost estimation.

[0009] The object of the present invention is achieved through the following technical solutions:

[0010] The query optimization domain cost estimation method based on graph attention network includes the following steps:

[0011] Step S1, obtain data and build a training set for the PostgreSQL database;

[0012] Step S2, constructing a deep learning network for the cost estimation model;

[0013] Step S3: passing the training set into the cost estimation model to train the deep learning network;

[0014] Step S4, obtaining a predicted cost based on the data of the query statement to be predicted;

[0015] Step S5: plug the cost estimation model into the PostgreSQL database to implement the cost estimation task on the database.

[0016] Specifically, the data acquisition in step S1 includes two methods: querying the existing statistical information of the PostgreSQL database and the Explain Analyze interface;

[0017] The data includes database schema information, statistical information and query statement physical execution plan tree;

[0018] The physical execution plan tree consists of multiple nodes, each node corresponds to a physical operator;

[0019] The physical execution plan tree also includes the steps of using the Floyd algorithm to calculate the shortest path and construct a priori attention matrix;

[0020] The physical operators include Bitmap Heap Scan, Index Scan and Nested Loop Join.

[0021] Specifically, in step S1, a feature encoding method is used to construct a training set for the PostgreSQL database;

[0022] The feature encoding includes using a Learning Embedding method for table, column names, and join predicates;

[0023] The selection predicate uses a method encoded as a triple;

[0024] The triplet is<Column,Operator,Value> ;

[0025] The cardinality, base table selectivity, and connection selectivity in the database statistics are concatenated and passed to Linner for feature encoding.

[0026] The selecting predicate further includes the step of generating a fixed-length vector according to the satisfied conditions on the sampling table.

[0027] Specifically, step S2 includes the following sub-steps:

[0028] Step S21, encoding the features;

[0029] Step S22, constructing graph attention;

[0030] Step S23, multi-layer iterative fitting;

[0031] Step S24: predict the cost.

[0032] Specifically, step S22 includes the following sub-steps:

[0033] Step S221: Use the Embedding layer and the Linner layer to perform feature encoding on the table name, column name, operation type in the selection predicate, and equi-join in sequence;

[0034] Step S222, using the Linner layer to perform feature encoding on the sampled data;

[0035] Step S223, using the Mask mechanism to perform average pooling on the selected predicate;

[0036] Step S224: Use the Concat layer to concatenate different feature codes together as the feature code vector of the query statement physical execution plan tree node.

[0037] Specifically, step S22 inputs the node feature vector encoded by the feature encoding layer and the prior attention matrix calculated by the Floyd algorithm, and uses the prior attention to adjust the attention matrix learned by the graph attention network to learn the relationship between the cross-node dependencies and node levels of the physical execution plan tree.

[0038] Specifically, step S23 inputs the output of the graph attention network into a subsequent feedforward neural network for multi-layer iterative fitting.

[0039] Specifically, step S24 passes the results of the multi-layer iterative fitting to the prediction layer for prediction.

[0040] Beneficial effects of the present invention:

[0041] This paper investigates the development of traditional cardinality and cost estimation methods, thoroughly studies and analyzes the problems existing in existing learning-based cost estimation models, and proposes a cost estimation model based on graph attention networks. By incorporating effective feature data such as table cardinality, base table selection rate, and connection selection rate into feature encoding, the accuracy of cost estimation is improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the structures shown in these drawings without paying any creative work.

[0043] Figure 1 is a schematic diagram of a relational database query optimizer of the present invention;

[0044] Figure 2 This is a schematic diagram of query statement feature coding according to the present invention;

[0045] Figure 3 This is a schematic diagram of the physical execution plan tree encoding of the present invention;

[0046] Figure 4 It is a schematic diagram of cross-border dependency of the physical execution plan tree of the present invention;

[0047] Figure 5 This is a schematic diagram of statistical information of the title.id column in the pg_stats table of the present invention;

[0048] Figure 6 It is a schematic diagram of EXPLAIN ANALYZE of the present invention;

[0049] Figure 7 is a sampling vector generation flow chart of the present invention;

[0050] Figure 8 It is a characteristic coding schematic diagram of the present invention;

[0051] Figure 9 It is a schematic diagram of the prior attention based on the connection relationship of the physical execution plan tree of the present invention;

[0052] Figure 10It is a model architecture diagram of the present invention. DETAILED DESCRIPTION

[0053] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0054] In order to have a clearer understanding of the technical features, purposes and beneficial effects of the present invention, the technical solutions of the present invention are now described in detail below. Obviously, the implementation cases described are part of the embodiments of the present invention, not all of them, and should not be understood as limiting the scope of the implementation of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0055] Example 1:

[0056] In this embodiment, database cost estimation is an important part of relational database query optimization. Figure 1 As shown, current relational database query optimizers primarily consist of logical optimization and physical optimization. Physical optimization consists of two main components: cardinality estimation and a cost model. The query optimizer receives the logical execution plan tree after logical optimization parsing. The same logical execution plan tree can generate different physical execution plan trees based on different join orders and physical operators. This large number of physical execution plan trees constitutes the search space for physical execution plan trees. Finding the fastest physical execution plan tree in this search space is an NP-hard problem. The determination of which physical execution plan tree is fastest cannot be truly executed; the cost model can only be used to estimate its execution cost.

[0057] When conducting cost estimation research, we must first clarify the object of encoding. From the perspective of the query statement life cycle, there are four types of objects that can be encoded: 1) encoding for SQL statements, 2) encoding for abstract syntax trees, 3) encoding for logical execution plan trees, and 4) encoding for physical execution plan trees. The difficulty of encoding from front to back increases from simple to complex, and more and more features can be obtained. Figure 2 The encoding shown is based on the query statement. There are many factors that affect the final cardinality or cost in the query statement, such as the query data source table, the selection predicate conditions and the connection predicates. Different encoding methods are used for different factors. Another encoding method is to directly perform feature encoding on the physical execution plan tree. The physical execution plan tree is a tree structure. Figure 3The physical execution plan tree is directly encoded. Compared to query-specific feature encoding, encoding the physical execution plan tree captures more data. For example, different operators in the physical execution plan tree have different impacts on cardinality and cost estimates. More importantly, query optimization essentially involves selecting the lowest-cost candidate physical execution plan tree from the search space. Encoding only the query statement, even with highly accurate cardinality or cost estimates, cannot guide this selection process. Therefore, encoding the physical execution plan tree is essential.

[0058] There are two key points in feature encoding of the physical execution plan tree: 1) The dependency between parent and child nodes. During the execution of the execution plan tree, the input data of the parent node comes from the child node, so it is directly affected by the child node. 2) There are cross-node dependencies between different nodes. Cross-node dependencies are affected by the node hierarchy. For example, the execution overhead of an intermediate node is related to all its child nodes, and the weight of the impact is related to the hierarchical relationship between the nodes. Figure 4 2) is explained. During the actual execution of the execution plan tree, the intermediate node Filter will find the child node Scan to obtain a tuple and then pass it to the parent node Join. The uploaded tuple contains the child node information, but the Filter node does not include all the features of the Scan node during feature encoding. The execution of the Join node is related to both the Filter and Scan nodes. Therefore, the cross-node dependency between the Scan and Join nodes must be considered in the model.

[0059] To address the aforementioned issues and more effectively learn the dependencies between different nodes, as well as the relationships between dependencies and node hierarchies, this paper proposes a method for feature encoding and learning the physical execution plan tree of query statements using a graph attention network. Furthermore, to improve cost estimation, when encoding the features of the physical execution plan tree nodes, it is necessary to input as much feature information as possible that is influential in cost estimation.

[0060] The specific steps are as follows:

[0061] S1. Obtain data and build a training set for the PostgreSQL database. Collect database schema information, statistical information, and query statement physical execution plan trees for cost estimation model training. The physical execution plan trees for query statements perform feature encoding based on data categories. In a relational database, the schema defines the tables in the database, the fields of each table, and the relationships between tables and fields. In the PostgreSQL database, this data can be obtained by querying the pg_class table. The pg_class table is a system catalog table in PostgreSQL that holds information about all database objects, including tables, views, materialized views, indexes, sequences, etc. This table provides basic information about each database object, such as its name, OID, owner, size, etc. Taking the number of rows (rowCounts) and the number of columns (columnCounts) of all tables as an example, you can use the following statement to query.

[0062]

[0063] In addition, each table or database has other statistical information such as the number of rows (Cardinality), the range of values ​​(Min-Max), the number of independent values ​​(Distinct Values), etc. In the cost estimation model, it is necessary to feature encode the schema and statistical information of the tables in the database. In the PostgreSQL database, this can be obtained by querying the pg_stats table. pg_stats is a system catalog table in PostgreSQL that stores statistical data collected by the query optimizer. It provides information about the distribution of data in the table, such as the number of independent values, the most common value (Most Common Value, MCV), histogram data, etc. These statistical information were originally used by the database in the query optimization process of the query optimizer, such as Figure 5 Displays the system's existing statistics for the id column in the title table in the JOB dataset.

[0064] In deep learning, normalization is used to scale input data and reduce variance. This makes the model easier to learn and helps prevent overfitting by reducing the influence of any single feature in the data. Common normalization methods include Mean Normalization and Min-Max Normalization. These two methods require the mean, minimum, and maximum values ​​of the column values, respectively. This data cannot be directly retrieved from the pg_stats table and requires further retrieval. The following query retrieves the minimum and maximum values ​​of the id column in the title table.

[0065] SELECT min(id)as min,max(id)as max FROM title;

[0066] As shown in Table 1, some statistical information of the IMDb dataset frequently used in academia includes the maximum and minimum values, i.e., the value range, cardinality, and the number of independent values.

[0067]

[0068]

[0069] Table 1 IMDb dataset statistics

[0070] A piece of training data for the cost estimation model consists of a physical execution plan tree, query statement structure, schema information of participating tables, statistical information, and training labels. The most important of these are the physical execution plan tree corresponding to the query statement and the actual execution time of the training labels. In PostgreSQL, these can be obtained through the EXPLAIN interface, such as Figure 6 For example, this query statement will analyze (ANALYZE) and execute the query statement, returning the corresponding physical execution plan tree, the optimizer's estimated cardinality cost, and the actual execution cost. In actual implementation, the FORMAT JSON parameter is set to obtain data in JSON format, facilitating subsequent transmission and processing.

[0071] After successfully acquiring the data required for model training, feature encoding is required. Feature encoding is a crucial step in machine learning and data analysis. It involves converting raw data into a higher-level representation that computer algorithms can better understand and process. The primary purpose of feature encoding is to extract meaningful features that better represent the raw data and are therefore more effectively used in machine learning models. A physical execution plan tree consists of multiple nodes, each corresponding to a physical operator such as Bitmap Heap Scan, Index Scan, or Nested Loop Join. Different operators have different operands and information that can be encoded. For example, a Table Scan operator specifies the table to scan, such as the title table, and the selection predicate for filtering tuples, such as title.id>10; a Join operator specifies the join predicate, such as title.id=movie_companies.movie_id. Different feature encoding methods are used for different node feature element types: 1) Learning Embedding is used for categorical variables such as table (title), column name (title.id), and join predicate (title.id=movie_companies.movie_id). 2) Encode the selection predicate (title.id>10) as a triple<Column,Operator,Value> 3) The cardinality, base table selectivity, and join selectivity from the database statistics are concatenated and passed to Linner for feature encoding. 4) A fixed-length vector is generated based on the satisfaction of the selection predicate on the sampling table to increase the encoding of the selection predicate effect. The details are as follows:

[0072] 1. Categorical variables. A common approach for categorical variables is to use one-hot encoding. When using one-hot encoding, each feature is represented as a binary vector in which only one element is 1 and the rest are 0. One-hot encoding is more suitable for fixed scenarios. However, when the amount of data for categorical variables increases, one-hot encoding takes up more encoding space, resulting in unnecessary memory overhead. One-hot encoding also struggles with changes in categorical variables, and it can be difficult to find an appropriate size for expansion by reserving bits. In real-world databases, tables in the schema change dynamically. As tables are created, the categorical variables corresponding to columns and connections also change. To enable feature encoding in this situation, learned embeddings are used. Learned embeddings are often used in natural language processing, outputting a fixed-length vector for a large number of words to better support the dynamic changes of categorical vectors.

[0073] 2. Selection predicate encoding. In a physical execution plan tree node, each selection predicate is encoded as a triple<Column,Operator,Value> For the first two, encoding based on learning embedding is used, and Value is processed using Min-Max normalization. Different nodes may contain different numbers of predicates. For example, in a physical execution plan tree node, Index Cond, Filter, and Recheck Cond may all contain selection predicates. The Mask mechanism is used to average pool the contribution.

[0074] 3. Database statistics. Traditional databases collect and analyze dataset information during query optimization. This data has been used in traditional query optimization methods, and some of it can truly reflect the cost estimates of query statements. Introducing this data into the cost estimation model helps improve the accuracy of cost estimation. The schema of a dataset is considered basic database statistics. For example, the cardinality of a table and the number of independent values ​​in a table column, as shown in Table 1, have a significant impact on the cardinality and cost of a query statement. The cardinality is normalized using Min-Max processing and then introduced as a feature.

[0075]

[0076] The number of independent values ​​in a table column is used by the database to estimate join operations. In PostgreSQL, the formula for calculating the selectivity of equijoins (eqjoinsel) is as shown above, where μ inner and μ outer The final number of results after the join is obtained by multiplying the number of independent values ​​in the join column of the inner and outer join tables, the number of rows in the outer join table, the number of rows in the inner join table, and the equijoin selectivity by outter*inner*eqjoinsel. The join selectivity is calculated based on the traditional database join selectivity calculation method, and its definition is given in the following formula. The model will evolve from the join selectivity of eqjoinsel. Introduced as a feature.

[0077]

[0078] In addition to the join selectivity calculated based on the number of independent values, the cardinality estimate for a single table is also widely used in databases and has a high accuracy. The base table selectivity σ calculated based on this estimate and the cardinality of the table has a high accuracy and is introduced as a feature in the model. The base table selectivity is a decimal between [0,1]. The following formula gives its definition, where Rows(t) represents the total number of rows in base table t, and the numerator is The cardinality of the base table t that satisfies the predicate p is obtained using traditional cardinality estimation techniques.

[0079]

[0080] 4. Sampling data. The sampling method can reflect the data distribution of global data from a certain aspect and is widely used in traditional and learning-based query optimization methods. When encoding the table elements, the matching of the selection predicate on the sampled data set of the table is encoded. The encoding is a fixed-size 0-1 vector, where each bit represents whether the corresponding tuple meets the predicate of the query node. If it meets, it is 1, and if it does not meet, it is 0. Figure 7 The following figure shows the steps in which the system completes sampling. First, the system randomly samples N rows from the table of the CSV source file of the IMDb dataset. To facilitate the subsequent generation of sampling vectors, a new SID (Sample ID) column with a value of 1-N is added to each table. The Python Csvkit package is used to execute the following query statement on the CSV file to obtain the rows that meet the selection predicate. Finally, the position corresponding to the SID in the vector with an initial value of 0 and a length of N is set to 1.

[0081]

[0082] The above four types of feature coding cover the current feature coding of a physical execution plan tree node. Figure 8 Specific coding examples are shown.

[0083] S2. Build a deep learning network for the cost estimation model.

[0084] The constructed cost estimation model has four layers: feature encoding layer, graph attention network layer, loop iteration layer and prediction layer.

[0085] The feature encoding layer encodes the features of different elements in the physical execution plan tree nodes of the query statement: the table name, column name, operation type in the selection predicate, and equi-join are encoded using the Embedding layer and the Linner layer respectively; the sampled data is encoded using the Linner layer; the selection predicate is average pooled using the Mask mechanism; and finally, the Concat layer is used to splice the different feature codes together as the feature encoding vector of the physical execution plan tree node of the query statement.

[0086] The inputs to the graph attention network layer are the node feature vectors encoded by the feature encoding layer and the prior attention matrix calculated by the Floyd algorithm. The prior attention is used in the model calculation to adjust the attention matrix learned by the graph attention network to learn the relationship between cross-node dependencies and node hierarchies in the physical execution plan tree. To more effectively learn the dependencies between different nodes and the relationship between dependencies and node hierarchies, a method for feature encoding and learning the physical execution plan tree of a query statement using a graph attention network is proposed. The graph attention network is a graph family neural network, while the physical execution plan tree is a graph in a special scenario. The graph network can fully encode the features of the latter and learn the dependencies between parent and child nodes. Secondly, the attention mechanism in the graph attention network helps to encode and learn the relationship between cross-node dependencies and hierarchies. The self-attention mechanism in the graph attention network essentially constructs an attention distribution as the weight between node features, and then performs a weighted sum of the node features. The attention distribution can usually be introduced by considering known features, which is called prior attention.

[0087] The self-attention mechanism in the graph attention network is shown in the following formula, that is, the hidden state of each node is calculated by paying attention to its neighboring nodes.

[0088]

[0089] where α i,j is the attention coefficient from node j to i, N i represents the neighbor nodes of node i, and h is the node input feature. Each physical execution plan tree constitutes an adjacency graph, which is a directed acyclic graph, such as Figure 4 As shown, in the physical execution plan tree, the dependency between parent and child nodes is a one-way dependency from the parent node to the child node. There may also be dependencies across nodes, which are related to the hierarchical relationship between the nodes. By preprocessing the adjacency relationship of the physical execution plan tree, the prior attention implied between the adjacency relationships is obtained. Adding it to the attention matrix generated by the graph attention network can adjust the latter, thereby improving the model accuracy. A prior attention encoding algorithm based on graph connectivity and inter-node distance is applied in the graph attention network. As shown in the following formula, during the model iteration process, the prior attention α based on the physical execution plan tree graph structure is used. p Tuning posterior attention in graph attention networks.

[0090] α′=α+α p

[0091] Figure 9The figure shows the prior attention calculation process of a physical execution plan tree. The physical execution plan tree has a total of 6 nodes, numbered from 0 to 5. The adjacency matrix in the upper left corner reflects the five directed edges of the physical execution plan tree. The matrix in the lower left corner sets the weights of unconnected nodes to a large integer N to facilitate the subsequent calculation of the shortest path between nodes using the Floyd algorithm. The upper right corner shows the calculated shortest path result. The weight represents the path length between two nodes. A large path represents a long distance. The weight for distances greater than a certain threshold is set to -∞, resulting in the matrix in the lower right corner. The final matrix is ​​added to the attention matrix generated by the graph attention algorithm. -∞ will block out unconnected links, ensuring that the attention weights and subsequent eigenvector calculations conform to the graph dependency of the physical execution plan tree. Other weights can reflect the cross-node dependency and hierarchical relationship between eigenvectors.

[0092] The loop iteration layer inputs the output of the graph attention network into the subsequent feedforward neural network for multi-layer iterative fitting, and finally passes it to the prediction layer for prediction.

[0093] The final cost estimation model is as follows Figure 10 As shown in Figure 2, the input data of the model mainly includes the physical execution plan tree, database schema information, and database statistical information. i It is the feature code of the physical execution plan tree node, which becomes h after being encoded by the feature coding layer. i ′ is then passed to the graph attention network. Also passed to the graph attention network is the prior attention matrix calculated using the Floyd algorithm for the physical execution plan tree adjacency matrix on the right side of the graph. This matrix is ​​combined with the graph attention matrix generated by the graph attention network itself. After N iterative feature extractions with the feedforward propagation network (FFN), it is passed to the final prediction layer for prediction, resulting in the final estimated cost value.

[0094] S3. Pass the training set into the cost estimation model to train the deep learning network, use the Min-Max normalized cost as the label, MSE as the loss function, and Adam optimizer.

[0095]

[0096] S4. Obtain and process the query data that needs to be predicted in the same way as the training set, input the trained cost estimation model, and obtain the predicted cost.

[0097] S5. Plug the cost estimation model into the PostgreSQL database to implement the cost estimation task on the database.

[0098] The basic principles, main features, and advantages of the present invention are shown and described above. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The above embodiments and descriptions are merely illustrative of the principles of the present invention. Various changes and modifications may be made to the present invention without departing from the spirit and scope of the present invention. Such changes and modifications are intended to fall within the scope of the present invention. The scope of the present invention is defined by the appended claims and their equivalents.

[0099] It should be noted that for the aforementioned various method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and units involved are not necessarily required by this application.

[0100] In the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0101] Those skilled in the art will appreciate that all or part of the processes in the above-described method embodiments can be implemented by instructing related hardware through a computer program. The program can be stored in a computer-readable storage medium, and when executed, the program can include the processes in the above-described method embodiments. The storage medium can be a magnetic disk, an optical disk, a ROM, a RAM, or the like.

[0102] The above disclosure is merely a preferred embodiment of the present invention and certainly cannot be used to limit the scope of the present invention. Therefore, equivalent changes made according to the claims of the present invention are still within the scope of the present invention.

Claims

1. A query optimization domain cost estimation method based on graph attention network, characterized by: The following steps are involved: Step S1, obtain data and build a training set for the PostgreSQL database; Step S2, constructing a deep learning network for the cost estimation model; Step S3: passing the training set into the cost estimation model to train the deep learning network; Step S4, obtaining a predicted cost based on the data of the query statement to be predicted; Step S5: plug the cost estimation model into the PostgreSQL database to implement the cost estimation task on the database; The data acquisition in step S1 includes two methods: querying the existing statistical information of the PostgreSQL database and the Explain Analyze interface; The data includes database schema information, statistical information and query statement physical execution plan tree; The physical execution plan tree consists of multiple nodes, each node corresponds to a physical operator; The physical execution plan tree also includes the steps of using the Floyd algorithm to calculate the shortest path and construct a priori attention matrix; The physical operators include Bitmap Heap Scan, Index Scan and Nested Loop Join; In step S1, a feature encoding method is used to construct a training set for the PostgreSQL database; The feature encoding includes using a Learning Embedding method for table, column names, and join predicates; The selection predicate uses a method encoded as a triple; The triple is < Column, Operator, Value >; The cardinality, base table selectivity, and connection selectivity in the database statistics are concatenated and passed to Linner for feature encoding. The selection predicate further includes the steps of generating a fixed-length vector based on the satisfied conditions on the sampling table; The step S2 includes the following sub-steps: Step S21, encoding the features; Step S22, constructing graph attention; Step S23, multi-layer iterative fitting; Step S24, predicting the cost; The step S22 includes the following sub-steps: The table name, column name, operation type in the selection predicate, and equi-connection are sequentially encoded using the Embedding layer and the Linner layer; Use the Linner layer to perform feature encoding on the sampled data; The Mask mechanism is used to perform average pooling on the selection predicate; Use the Concat layer to concatenate different feature codes together as the feature code vector of the query statement physical execution plan tree node; The step S22 inputs the node feature vector encoded by the feature encoding layer and the prior attention matrix calculated by the Floyd algorithm, and uses the prior attention to adjust the attention matrix learned by the graph attention network to learn the relationship between the cross-node dependencies and the node hierarchy of the physical execution plan tree.

2. The query optimization domain cost estimation method based on graph attention network according to claim 1 is characterized in that The step S23 inputs the output of the graph attention network into the subsequent feedforward neural network for multi-layer iterative fitting.

3. The query optimization domain cost estimation method based on graph attention network according to claim 2 is characterized in that The step S24 passes the results of the multi-layer iterative fitting to the prediction layer for prediction.