Deep Learning-Based Cardinality Estimation Method for Relational Databases

By constructing a relational topology graph in a relational database and establishing an independent cardinal estimator for its nodes and connected subgraphs, the estimation error problem of existing cardinal estimation methods during multi-column queries and multi-table join queries is solved, and more efficient and accurate cardinal estimation is achieved.

CN115269639BActive Publication Date: 2025-06-20HEBEI UNIV OF TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210722637.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-20
Publication Date
2025-06-20
Estimated Expiration
2042-06-20

AI Technical Summary

Technical Problem

The existing cardinality estimation method based on histograms has serious estimation errors in multi-column query and multi-table join query, and the cardinality estimation method based on deep learning has problems such as long training time, high model update cost and large estimation errors for unfamiliar SQL statements.

Method used

By constructing a relational topology graph of a relational database, an independent cardinal estimator is established for each node and connected subgraph, and a deep learning model is used for training to realize cardinal estimation of SQL statements, and only the relevant cardinal estimator is updated when the database data changes.

Benefits of technology

It effectively shortens the training time of the cardinal estimation model, reduces the update cost caused by database data changes, and improves the cardinal estimation accuracy for multi-table join queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115269639B_ABST
    Figure CN115269639B_ABST
Patent Text Reader

Abstract

The present invention is a cardinality estimation method for relational databases based on deep learning. First, obtain the metadata and statistical information of the database and construct a relationship topology graph between database tables; then, establish a cardinality estimator for each node and connected subgraph of the relationship topology graph; next, create SQL statements for training, execute the SQL statements in the database and save the query results; use the SQL statements and query results as training data to train the cardinality estimator; finally, use the trained cardinality estimator to estimate the cardinality. If the SQL statement only involves the query of a single database table or the database tables involved in the join query already have corresponding cardinality estimators, directly estimate the cardinality; otherwise, split the SQL statement into multiple subqueries and indirectly estimate the cardinality. Each cardinality estimator only corresponds to a single or part of the database tables in the database. When the data in the database changes, only part of the cardinality estimators are updated, reducing the update cost.
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 cardinality estimation, and specifically relates to a method for relational database cardinality estimation based on deep learning. By estimating the cardinality of a relational database, it helps the database query optimizer generate a more efficient execution plan. Background Art

[0002] Cardinality estimation is one of the most influential tasks in the query optimization process. Cardinality estimation refers to pre-estimating the number of result rows that may be returned after executing an SQL (Structured Query Language) statement in a database. The database query optimizer is based on the planned execution cost, and without changing the expected result, it makes the database engine plan execution time the shortest, and then selects the best execution plan among multiple alternative plans. And the cardinality estimate value is an important parameter for calculating the planned execution cost. If the cardinality estimate value is inaccurate, it will affect the calculation of the planned execution cost, may lead to the selection of an incorrect execution plan, and thus affect the execution efficiency and system performance.

[0003] To estimate the cardinality value, most commercial database management systems (DBMS) rely on histograms. Although histograms are fast to build and have low storage costs, when it comes to multi-column queries and multi-table join queries, independence assumptions are often required. This assumption ignores the correlation between columns and between tables. When the workload involves complex queries and there is a correlation between the column attributes of the tables, it will lead to serious estimation errors and hinder system performance. Facing some problems of histograms, they have started to come into the public eye.

[0004] Cardinality estimation based on deep learning can solve the problems existing in cardinality estimation based on histograms. However, currently, cardinality estimation based on deep learning generally has these problems: 1) The scale of the deep learning model is large. To improve the accuracy, a large amount of training data is required, resulting in a long training time for the model; 2) The trained model can only perform cardinality estimation for a static database. If the data in the database changes, the model needs to be retrained, and the cost of retraining is relatively high. If the model is not updated in time, it will lead to serious cardinality estimation errors; 3) The trained cardinality estimator can only perform cardinality estimation for the SQL statements used during the training process. For the cardinality estimation of unfamiliar SQL statements, there will generally be relatively large estimation errors. Summary of the Invention

[0005] Aiming at the deficiencies of the prior art, the technical problem to be solved by the present invention is to provide a method for relational database cardinality estimation based on deep learning.

[0006] The technical solution adopted by the present invention to solve the above technical problems is as follows:

[0007] A cardinality estimation method for relational databases based on deep learning, characterized in that the method comprises the following steps:

[0008] Step 1: Obtain the metadata and statistical information of the relational database, including the table names, attributes of each database table, and the foreign key association information between database tables, and construct a relationship topology graph of the database tables according to the foreign key association information; the nodes of the relationship topology graph represent database tables, and the connections between nodes represent the existence of foreign key associations between two database tables;

[0009] Step 2: Establish a cardinality estimator for each node and connected subgraph of the relationship topology graph, and determine the network structure of each cardinality estimator;

[0010] The cardinality estimators of nodes and connected subgraphs are all composed of an input layer, a hidden layer, and an output layer; the input layer includes two groups of input layer neurons, corresponding to two types of input information, basic predicates and connection predicates, each group of input layer neurons contains multiple neurons, the hidden layer contains multiple hidden layer neurons, and the output layer contains 1 + n output layer neurons, where n is a positive integer. In the cardinality estimator of a node, n is equal to the number of attributes of a single database table, and in the cardinality estimator of a connected subgraph, n is equal to the total number of attributes contained in multiple database tables with foreign key associations; the output layer is used to output the cardinality estimation value and the number of different values of each attribute;

[0011] Step 3: Create SQL statements for training, execute the SQL statements in the relational database and save the query results; use the SQL statements and query results as training data to train the cardinality estimator;

[0012] Step 4: Use the trained cardinality estimator to estimate the cardinality;

[0013] 4.1) Analyze and split the SQL statement for query. If the SQL statement only involves the query of a single database table or the database tables involved in the join query already have corresponding cardinality estimators, then execute step 4.2) to directly estimate the cardinality; otherwise, split the SQL statement into multiple subqueries and then execute step 4.2) to indirectly estimate the cardinality; the splitting principle is: ensure that there are corresponding cardinality estimators for the database tables involved in each subquery, and the database tables involved in each subquery should be as many as possible;

[0014] 4.2) Use SQL statements for cardinality estimation; vectorize the SQL statements or subqueries to obtain input vectors, and input the input vectors into the corresponding trained cardinality estimators to output the cardinality and the number of distinct values for each attribute; if the SQL statement does not need to be split, the cardinality estimator outputs the cardinality estimation value; otherwise, if the SQL statement is split into r subqueries S1, S2, …, S r , record the cardinality estimation values obtained by each subquery through the cardinality estimator and the number of distinct values for each attribute involved in the join predicates in the original SQL statement, and calculate the final cardinality estimation value according to Equation (1);

[0015]

[0016] where C(S) represents the cardinality estimation value, and C(S r ) represents the cardinality estimation value obtained by the subquery S r through the cardinality estimator, and D(S r .k r-1 ) represents the number of distinct values of attribute k r obtained by the subquery S r-1 through the cardinality estimator.

[0017] Furthermore, step 3 includes the following steps:

[0018] 3.1) Create several SQL statements for training according to the metadata and foreign key association information of the relational database. The SQL statements for training need to cover all database tables in the relational database, execute all SQL statements in the relational database, and then save the query results including the cardinality and the number of distinct values for each attribute;

[0019] 3.2) Use the SQL statements and the obtained cardinality and the number of distinct values for each attribute as training data to train the cardinality estimator; first, vectorize the SQL statements to obtain input vectors. The input vectors are divided into two parts: basic predicates and join predicates. The basic predicates are the predicate constraints on the database tables, and the join predicates refer to whether there are join predicate constraints between two database tables; after vectorizing all SQL statements, obtain the training set; divide the training set into m parts, each part of the training set corresponds to a cardinality estimator, and use each part of the training set to train its own cardinality estimator to obtain m trained cardinality estimators, where m represents the number of cardinality estimators.

[0020] Furthermore, when the data in the relational database changes or the amount of changed data reaches a set scale, the corresponding cardinality estimator needs to be updated, including the following content:

[0021] Record the database table where the changed data is located, and mark the nodes corresponding to the database table in the relational topology graph; determine the cardinality estimator to be updated according to the marked nodes, and the cardinality estimators corresponding to the marked nodes and the connected subgraphs where the marked nodes are located are the cardinality estimators to be updated;

[0022] Create SQL statements for training based on the metadata and foreign key association information of the changed relational database, train the cardinality estimator to be updated, and obtain the updated cardinality estimator; then replace the original cardinality estimator with the updated cardinality estimator, thus completing the update of the cardinality estimator.

[0023] Compared with the prior art, the beneficial effects of the present invention are:

[0024] 1. This method establishes a cardinality estimator for each node and connected subgraph of the relational topology graph, divides the large-scale cardinality estimators corresponding to the entire database into m small-scale cardinality estimators, and each cardinality estimator uses its own training set for parallel training. Compared with the existing deep learning cardinality estimation model, the training time is effectively shortened.

[0025] 2. Each cardinality estimator only corresponds to a single or part of the database tables in the database. When the data in the database changes, it is not necessary to update all the cardinality estimators. Only the cardinality estimators corresponding to the database tables and connected subgraphs involved in the changed data need to be trained and updated, that is, only part of the cardinality estimators need to be trained and updated, effectively reducing the update cost caused by database changes.

[0026] 3. This method can estimate the cardinality of SQL statements that have not been used during the training process, that is, split the SQL statement into multiple subqueries, and associate the split cardinality estimators through the number of different values of each attribute output by the cardinality estimator to achieve multi-table join queries; compared with the existing deep learning-based cardinality estimation method and the multi-table join cardinality estimation method provided by the database, the present invention has higher estimation accuracy. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 is a schematic structural diagram of the cardinality estimator;

[0028] Figure 2 is a schematic diagram of the relational topology graph;

[0029] Figure 3 is a flowchart of the cardinality estimator;

[0030] Figure 4 is a schematic diagram of node marking of changed data;

[0031] Figure 5 is a schematic diagram of the relational topology graph in Embodiment 1;

[0032] Figure 6 For the cardinality estimator Net in Embodiment 1 person*order Structural schematic diagram. Detailed implementation manners

[0033] The technical solutions of the present invention will be described in detail below in conjunction with the accompanying drawings and specific implementation manners, but the protection scope of this application is not limited thereby.

[0034] The present invention is a method for estimating the cardinality of a relational database based on deep learning (hereinafter referred to as the method), including the following steps:

[0035] Step 1: Obtain the metadata and statistical information of the relational database, including the table names, attributes of each database table, and the foreign key association information between database tables, and construct a relationship topology graph of the database tables according to the foreign key association information; the nodes of the relationship topology graph represent database tables, and the connections between nodes represent the existence of foreign key associations between two database tables. A subgraph formed by nodes with connection relationships in the relationship topology graph is called a connected subgraph;

[0036] For example, a relational database contains four database tables A, B, C, and D. According to their foreign key association information, the Figure 2 shown relationship topology graph is obtained. Node A is connected to nodes B and C respectively, indicating that there are foreign key associations between database table A and database tables B and C respectively. Figure 2 There are a total of three connected subgraphs G(A,B), G(A,C), and G(A,B,C) in it;

[0037] Step 2: Establish a cardinality estimator for each node and connected subgraph of the relationship topology graph, establish a total of m cardinality estimators, and determine the network structure of each cardinality estimator;

[0038] Figure 2 In A , the cardinality estimators of the four nodes A, B, C, and D are respectively denoted as Net B , Net C , Net D , and the cardinality estimators of the three connected subgraphs G(A,B), G(A,C), and G(A,B,C) are respectively denoted as Net A*B , Net A*C , Net A*B*C , so a total of seven cardinality estimators are created;

[0039] The cardinality estimators for nodes and connected subgraphs are both composed of an input layer, a hidden layer, and an output layer. The input layer includes two groups of input layer neurons, corresponding to two types of input information: basic predicates and join predicates. Each group of input layer neurons contains multiple neurons. The hidden layer contains multiple hidden layer neurons, and the output layer contains 1 + n output layer neurons, where n is a positive integer. In the cardinality estimator for a node, n is equal to the number of attributes in a single database table. In the cardinality estimator for a connected subgraph, n is equal to the total number of attributes contained in multiple database tables with foreign key associations. The output layer is used to output the cardinality estimate value and the number of distinct values (DNV) for each attribute.

[0040] For example, if database table A has 3 attributes and database table B has 4 attributes, then the output layer of the cardinality estimator Net A for node A contains 1 + 3 = 4 output layer neurons, and the output layer of the cardinality estimator Net B for node B contains 1 + 4 = 5 output layer neurons. The output layer of the cardinality estimator Net A*B for the connected subgraph G(A, B) contains 1 + 3 + 4 = 8 output layer neurons.

[0041] Step 3: Create SQL statements for training, execute the SQL statements in a relational database, and save the query results. Use the SQL statements and query results as the training data for the cardinality estimator to train the cardinality estimator.

[0042] 3.1) Create several SQL statements for training based on the metadata and foreign key association information of the relational database. The SQL statements for training should cover all database tables in the relational database. Execute all SQL statements in the relational database and save the query results including the cardinality (the number of rows returned) and the number of distinct values for each attribute. This query result is the true result obtained by querying the SQL statements in the relational database.

[0043] 3.2) Use the SQL statement, the obtained cardinality, and the number of distinct values of each attribute as training data to train the cardinality estimator. First, vectorize the SQL statement to obtain an input vector. The input vector is divided into two parts: basic predicates and join predicates. Basic predicates are predicate constraints on database tables, and join predicates refer to whether there are predicate constraints between two database tables. Since the query result is already numeric, no vectorization is required. After vectorizing all SQL statements, a training set is obtained. Divide the training set into m parts, with each part of the training set corresponding to a cardinality estimator. Input the input vector into the cardinality estimator. The cardinality estimator outputs a cardinality estimate value and the number of distinct values of each attribute. Use each part of the training set to train its respective cardinality estimator to obtain m trained cardinality estimators.

[0044] Step 4: Estimate the cardinality using the trained cardinality estimator.

[0045] 4.1) Analyze and split the SQL statement used for query. If the SQL statement only involves querying a single database table or the database tables involved in the join query already have corresponding cardinality estimators, then execute step 4.2) to directly estimate the cardinality. Otherwise, split the SQL statement into multiple subqueries and then execute step 4.2) to indirectly estimate the cardinality. The splitting principle is: ensure that there are corresponding cardinality estimators for the database tables involved in each subquery, and the database tables involved in each subquery should be as many as possible.

[0046] For example, a certain SQL statement SQL A*B*D involves a join query of three database tables A, B, and D, but there is no corresponding cardinality estimator Net A*B*D , so the SQL statement SQL A*B*D needs to be split. Although the SQL statement SQL A*B*D can be split into subquery statements S A , S B , and S D , because there are corresponding cardinality estimators for nodes A and B, the SQL statement SQL A*B*D is finally split into subqueries S A*B and S D , corresponding to cardinality estimators Net A*B and Net D , where S A*B represents the join query of database tables A and B.

[0047] 4.2) Perform cardinality estimation using SQL statements; vectorize the SQL statements or subqueries to obtain input vectors, and input the input vectors into the corresponding trained cardinality estimators to output the cardinality and the number of distinct values for each attribute; if the SQL statement does not need to be split, the cardinality estimator outputs the cardinality estimate value; otherwise, if the SQL statement is split into r subqueries S1, S2, …, S r , record the cardinality estimate values obtained by each subquery through the cardinality estimator and the number of distinct values for each attribute involved in the join predicates in the original SQL statement, and calculate the final cardinality estimate value according to Equation (1);

[0048]

[0049] where C(S) represents the cardinality estimate value, and C(S r ) represents the cardinality estimate value obtained by the subquery S r through the cardinality estimator, and D(S r .k r-1 ) represents the number of distinct values of attribute k r obtained by the subquery S r-1 through the cardinality estimator.

[0050] This method further includes the following content:

[0051] When the data in the relational database changes or the amount of changed data reaches a set scale, the corresponding cardinality estimator needs to be updated. The specific process is as follows:

[0052] First, record the database table where the changed data is located, and mark the corresponding node of the database table in the relational topology graph; as Figure 4 shown, if the data in database table A changes, then mark node A in the relational topology graph; then, according to the marked nodes, determine the cardinality estimators to be updated. The cardinality estimators corresponding to the marked nodes and the connected subgraphs where the marked nodes are located are the cardinality estimators to be updated; as Figure 4 shown, the connected subgraphs where node A is located are G(A,B), G(A,C), G(A,B,C), so the cardinality estimators to be updated include Net A , Net A*B , Net A*C and Net A*B*C ;

[0053] Finally, create SQL statements for training according to the metadata and foreign key association information of the changed relational database, train the cardinality estimators to be updated, and obtain the updated cardinality estimators; then replace the original cardinality estimators with the updated cardinality estimators, and thus complete the update of the cardinality estimators.

[0054] Example 1

[0055] The method for estimating the cardinality of a relational database based on deep learning in this example includes the following steps:

[0056] Step 1: Obtain the metadata and statistical information of the relational database. The relational database includes three database tables, named person, item, and order respectively. Table 1 shows the attributes of each database table, and * indicates that this attribute has a foreign key association with the attributes of other database tables; construct a relationship topology graph between the database tables according to the foreign key association information. See Figure 5 , the relationship topology graph contains three nodes, and there is a foreign key association between the two database tables of node person and order;

[0057] Attributes of Each Database Table in Table 1

[0058] person item order Id* Id Id Name Name Num Age Price person_id* item_id

[0059] Step 2: According to the relationship topology graph, establish a cardinality estimator for each node and connected subgraph, denoted as Net person , Net order , Net item and Net person*order , and determine the network structure of each cardinality estimator;

[0060] The cardinality estimators of nodes and connected subgraphs are all composed of an input layer, a hidden layer, and an output layer. The input layer includes two groups of input layer neurons, which are respectively used to input two types of information: basic predicates and connection predicates; each group of input layer neurons includes multiple neurons, the hidden layer includes multiple hidden layer neurons, and the output layer contains 1 + n output layer neurons. In the cardinality estimator of a node, n is equal to the number of attributes of a single database table. In the cardinality estimator of a connected subgraph, n is equal to the total number of attributes included in multiple database tables with foreign key associations. Therefore, the output layer of the cardinality estimator Net person includes 4 output layer neurons, the output layer of the cardinality estimator Net order includes 5 output layer neurons, the output layer of the cardinality estimator Net item includes 4 output layer neurons, and the output layer of the cardinality estimator Net person*order includes 8 output layer neurons;

[0061] Step 3: Create SQL statements for training, execute the SQL statements in the relational database and save the query results; use the SQL statements and query results as the training data of the cardinality estimator to train the cardinality estimator;

[0062] 3.1) To train the cardinality estimator Net person*orderFor example, create an SQL statement for training, such as "select * from person, order where person.id = order.person_id and person.id > 2 and order.num < 5;" Execute the SQL statement in a relational database and save the query result including the cardinality (the number of rows returned) and the number of distinct values for each attribute. This query result is the true result obtained by querying the SQL statement in the relational database;

[0063] 3.2) Use the SQL statement and the obtained cardinality and the number of distinct values for each attribute as training data to train the cardinality estimator Net person*order First, vectorize the SQL statement to obtain an input vector. Use the one - hot encoding method to digitize the join predicates of the SQL statement as [0 1], and digitize the basic predicates as [1 0 0 0 0 1 0 0 2], [0 1 0 0 0 0 1 1 5]. The input vector is [0 1 1 1 0 0 0 1 1 1 7]; Input the input vector into the cardinality estimator Net person*order The cardinality estimator Net person*order Outputs the cardinality estimate value and the number of distinct values for each attribute, and trains the cardinality estimator Net person*order to obtain the trained cardinality estimator Net person*order ;

[0064] Step 4: Estimate the cardinality using the trained cardinality estimator;

[0065] Analyze and split the SQL statement used for query. If the SQL statement only contains a query of a single database table or the database tables involved in the join query already have corresponding cardinality estimators, directly vectorize the SQL statement and use the trained cardinality estimator to estimate the cardinality; otherwise, split the SQL statement into multiple sub - queries, ensuring that there is a corresponding cardinality estimator for each database table involved in each sub - query, and the database tables involved in each sub - query should be as many as possible;

[0066] For example, there are the following three SQL statements for query:

[0067] ① select * from item where item.price > 5.2;

[0068] ② select * from person, order where person.id = order.person_id and person.person.id > 2 and order.num > 1;

[0069] ③Select * from person, order, item where person.id = order.person_id and person.id > 2 and order.num > 1 and order.item_id = item.id and item.price > 5.2;

[0070] SQL statement ① is a single-table query involving the database table item. It only involves the database table item, and there already exists a cardinality estimator Net item , so directly vectorize SQL statement ① and input the vectorized SQL statement ① into the trained cardinality estimator Net item to directly estimate the cardinality and output the cardinality estimation value and the number of distinct values of each attribute;

[0071] SQL statement ② is a join query involving the database tables person and order. It involves the database tables person and order, and there already exists a cardinality estimator Net person*order , so directly vectorize SQL statement ② to obtain an input vector, and input the input vector into the trained cardinality estimator Net person*order to directly estimate the cardinality and output the cardinality estimation value and the number of distinct values of each attribute;

[0072] SQL statement ③ involves a join query of the database tables person, order, and item, and there is no corresponding cardinality estimator. Therefore, SQL statement ③ needs to be split into two subqueries, SQL statement ① and ②, and then vectorize the two subqueries. Input the vectorized SQL statement ① into the trained cardinality estimator Net item to output the cardinality estimation value and the number of distinct values of each attribute; Input the vectorized SQL statement ② into the trained cardinality estimator Net person*order to output the cardinality estimation value and the number of distinct values of each attribute; The join predicate of SQL statement ③ is "order.item_id = item.id", and the number of distinct values of each attribute involved in the join predicate is D(item.id) and D(order.item_id) respectively. D(item.id) represents the number of distinct values of the attribute obtained by SQL statement ① through the cardinality estimator Net item and D(order.item_id) represents the number of distinct values of the attribute obtained by SQL statement ② through the cardinality estimator Net person*order ;

[0073]

[0074] The final cardinality estimation value is obtained through the above formula to achieve indirect cardinality estimation.

[0075] Where the present invention is not described applies to the prior art.

Claims

1. A cardinality estimation method for relational databases based on deep learning, characterized in that, The method includes the following steps: Step 1: Obtain the metadata and statistical information of the relational database, including the table names, attributes of each database table, and the foreign key association information between database tables, and construct a relationship topology graph of the database tables according to the foreign key association information; the nodes of the relationship topology graph represent database tables, and the connections between nodes represent the existence of foreign key associations between two database tables; Step 2: Establish a cardinality estimator for each node and connected subgraph of the relationship topology graph, and determine the network structure of each cardinality estimator; The cardinality estimators of nodes and connected subgraphs are all composed of an input layer, a hidden layer, and an output layer; the input layer includes two groups of input layer neurons, corresponding to two types of input information: basic predicates and join predicates. Each group of input layer neurons contains multiple neurons, the hidden layer contains multiple hidden layer neurons, and the output layer contains 1 + n output layer neurons, where n is a positive integer. In the cardinality estimator of a node, n is equal to the number of attributes of a single database table, and in the cardinality estimator of a connected subgraph, n is equal to the total number of attributes contained in multiple database tables with foreign key associations; the output layer is used to output the cardinality estimation value and the number of different values of each attribute; Step 3: Create SQL statements for training, execute the SQL statements in the relational database, and save the query results; use the SQL statements and query results as training data to train the cardinality estimators; Step 4: Use the trained cardinality estimators to estimate the cardinality; 4.1) Analyze and split the SQL statement for query. If the SQL statement only involves the query of a single database table or the database tables involved in the join query already have corresponding cardinality estimators, then execute step 4.2) to directly estimate the cardinality; otherwise, split the SQL statement into multiple subqueries and then execute step 4.2) to indirectly estimate the cardinality; the splitting principle is: ensure that there are corresponding cardinality estimators for the database tables involved in each subquery, and the database tables involved in each subquery should be as many as possible; 4.2) Use SQL statements for cardinality estimation; vectorize the SQL statements or subqueries to obtain input vectors, and input the input vectors into the corresponding trained cardinality estimators to output the cardinality and the number of distinct values of each attribute; if the SQL statement does not need to be split, the cardinality estimator outputs the cardinality estimation value; otherwise, if the SQL statement is split into r subqueries S1, S2, …, S r , record the cardinality estimation values obtained by each subquery through the cardinality estimator and the number of distinct values of each attribute involved in the join predicates in the original SQL statement, and calculate the final cardinality estimation value according to formula (1); Among them, C(S) represents the cardinality estimate value, and C(S r ) represents the cardinality estimate value obtained by the cardinality estimator for the subquery S r . D(S r .k r-1 ) represents the number of distinct values of the attribute k obtained by the cardinality estimator for the subquery S r . r-1 ​ 2. The cardinality estimation method for relational databases based on deep learning according to claim 1, characterized in that, The said step 3 includes the following steps: 3.1) Create several SQL statements for training according to the metadata and foreign key association information of the relational database. The SQL statements for training need to cover all database tables in the relational database, execute all SQL statements in the relational database, and then save the query results including the cardinality and the number of different values of each attribute; 3.2) Use the SQL statements and the obtained cardinality and the number of different values of each attribute as training data to train the cardinality estimators; first, vectorize the SQL statements to obtain input vectors. The input vectors are divided into two parts: basic predicates and join predicates. The basic predicate is the predicate constraint for the database table, and the join predicate refers to whether there is a join predicate constraint between two database tables; after vectorizing all SQL statements, a training set is obtained; divide the training set into m parts, each part of the training set corresponds to a cardinality estimator, and use each part of the training set to train its own cardinality estimator to obtain m trained cardinality estimators, where m represents the number of cardinality estimators.

3. The cardinality estimation method for relational databases based on deep learning according to claim 1 or 2, characterized in that, Once the data in the relational database changes or the amount of changed data reaches a set scale, the corresponding cardinality estimator needs to be updated, including the following: Record the database table where the changed data is located, and mark the corresponding node of the database table in the relational topology diagram; according to the marked nodes, determine the cardinality estimators to be updated, and the cardinality estimators corresponding to the marked nodes and the connected subgraphs where the marked nodes are located are the cardinality estimators to be updated; Create SQL statements for training based on the metadata and foreign key association information of the changed relational database, train the cardinality estimators to be updated, and obtain the updated cardinality estimators; then replace the original cardinality estimators with the updated cardinality estimators, thus completing the update of the cardinality estimators.

Citation Information

Patent Citations

  • Database query optimization method and system based on graph neural network

    CN113010547A

  • Deep learning for optimizer cardinality estimation

    US10942923B1