Query processing method and device based on Hash connection, equipment, medium and product
By using LSTM, CNN and FCNN networks for query cost prediction in relational databases, the problem of query result deviation when the number of hash connections increases is solved, and more accurate query cost calculations and optimizer actions are achieved.
Patent Information
- Application Number
- CN202410875416.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-01
- Publication Date
- 2024-09-20
AI Technical Summary
When the number of hash connections increases, the existing technology cannot accurately calculate the query cost of the query plan, resulting in a large deviation between the query results of the relational database and the real results.
By inputting each target query plan of the target query statement into the target cost prediction model, LSTM, CNN and FCNN networks are used for relationship mining, feature extraction and cost prediction to obtain the target contribution value of each query selection rate, and then determine the query cost, and Choose the optimal execution plan to get accurate query results.
The calculation accuracy of query cost is improved, the final result is closer to the real result, the optimizer action is more accurate, and the problem of result value deviation when the number of hash connections increases is solved.
Smart Images

Figure CN118673047A
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a query processing method, device, equipment, medium and product based on hash connection. Background Art
[0002] At present, when determining the query cost of each query plan under the query statement for the relational database, it is still based on multiple selectivities under the query statement, combined with the fixed formula for calculation. The fixed formula essentially denies the inherent coupling of probability between the various selectivities. For the ascending square geometric decrease and other fixed formula solutions with similar ideas, they only approximate the final result as much as possible when the number of connections is small. As the number of hash connections increases, the fixed formula's prediction of the accuracy of the probability distribution under multiple hash connections will deviate further and further from the actual value, and the above approximation method will become invalid as the number of hash connections increases.
[0003] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention
[0004] The main purpose of the present application is to provide a query processing method, device, equipment, medium and product based on hash connection, aiming to solve the technical problem in the prior art that when the number of hash connections increases, the query cost of each query plan cannot be accurately calculated, resulting in a large deviation between the query results of the relational database and the actual results.
[0005] To achieve the above object, the present application proposes a query processing method based on hash join, the method comprising:
[0006] Inputting each target query plan of the target query statement into the target cost prediction model respectively to obtain the target contribution value of each query selectivity corresponding to the target query statement;
[0007] Determine the target query cost of each target query plan according to the target contribution value of each query selectivity;
[0008] A target execution plan is selected from a plurality of target query plans according to the target query costs of the target query plans, and a query result of the relational database is obtained based on the target execution plan.
[0009] In one embodiment, the step of inputting each target query plan of the target query statement into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statement includes:
[0010] Inputting each target query plan of the target query statement into the target cost prediction model respectively, performing relationship mining through the LSTM network in the target cost prediction model, and determining the query association relationship in each target query plan;
[0011] Extracting features based on the query association relationship in each target query plan and the CNN network in the target cost prediction model to determine target feature information of each target query plan;
[0012] Cost prediction is performed based on the target feature information of each target query plan and the FCNN network in the target cost prediction model to determine the target contribution value of each query selectivity corresponding to the target query statement.
[0013] In one embodiment, before inputting each target query plan of the target query statement into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statement, the method further includes:
[0014] Obtaining multiple sample query statements and multiple sample query plans for each sample query statement;
[0015] Inputting each sample query plan of each sample query statement into the cost prediction model to be trained respectively, and determining the predicted contribution value of each sample selection rate of each sample query statement;
[0016] Determining a predicted execution plan for each sample query statement according to the predicted contribution value of each sample selection rate of each sample query statement, and obtaining a predicted execution result of the sample database under each sample query statement based on the predicted execution plan for each sample query statement;
[0017] Comparing the predicted execution results of the sample database under each sample query statement with the actual execution results of the sample database under each sample query statement;
[0018] The cost prediction model to be trained is optimized according to the comparison result to obtain a target cost prediction model.
[0019] In one embodiment, before inputting each sample query plan of each sample query statement into the to-be-trained cost prediction model and determining the predicted contribution value of each sample selection rate of each sample query statement, the method further includes:
[0020] Get the number of neurons of the initial cost prediction model;
[0021] Determine the initial parameter value of the model according to the number of neurons;
[0022] The initial cost prediction model is initialized with the model initial parameter values to obtain a cost prediction model to be trained.
[0023] In one embodiment, the cost prediction model to be trained is optimized according to the comparison result to obtain a target cost prediction model, including:
[0024] Determine iterative training parameters and iterative learning rate according to the comparison results;
[0025] Perform parameter calculation based on the iterative training parameter and the iterative learning rate to obtain current training parameters;
[0026] Calculating the learning rate according to the current training parameters and the preset momentum coefficient to determine the training learning rate;
[0027] The cost prediction model to be trained is optimized according to the training learning rate to obtain a target cost prediction model.
[0028] In one embodiment, before inputting each target query plan of the target query statement into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statement, the method further includes:
[0029] When receiving a target query statement from a relational database, the target query statement is parsed to obtain a plurality of query comparison fields, a plurality of query comparison conditions, and a query selection rate corresponding to each query comparison condition;
[0030] Information of each query comparison field, each query comparison condition, and the query selectivity corresponding to each query comparison condition is packaged to generate multiple target query plans of the target query statement.
[0031] In addition, to achieve the above purpose, the present application also proposes a query processing device based on hash connection, the query processing device based on hash connection includes: an input module, used to input each target query plan of the target query statement into the target cost prediction model respectively, and obtain the target contribution value of each query selectivity corresponding to the target query statement;
[0032] A processing module, used to determine a target query cost of each target query plan according to a target contribution value of each query selectivity;
[0033] The selection module is used to select a target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtain the query result of the relational database based on the target execution plan.
[0034] In addition, to achieve the above-mentioned purpose, the present application also proposes a query processing device based on hash connection, the device comprising: a memory, a processor and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the query processing method based on hash connection as described above.
[0035] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by the processor, the steps of the query processing method based on hash connection as described above are implemented.
[0036] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, the steps of the query processing method based on hash connection as described above are implemented.
[0037] The present application provides a query processing method based on hash connection. When receiving a target query statement of a relational database, the present application parses the target query statement to obtain multiple target query plans of the relational database; inputs each target query plan into a target cost prediction model to obtain a target contribution value of each query selectivity corresponding to the target query statement; determines the target query cost of each target query plan according to the target contribution value of each query selectivity; selects a target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtains the query result of the relational database based on the target execution plan. Through the above method, the coupling and internal correlation between different connection conditions in actual scenarios are taken into account, and a more accurate contribution value of the selectivity to the final selectivity can be obtained, thereby improving the calculation accuracy of the query cost corresponding to each query plan, making the final result closer to the real result, achieving the purpose of making the optimizer action more accurate, and solving the current problem of result value deviation when the number of hash connections increases. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0039] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0040] Figure 1 A flowchart diagram of the query processing method based on hash connection provided in the first embodiment of the present application;
[0041] Figure 2 A schematic diagram of the network structure of the query processing method based on hash connection provided in Example 1 of the present application;
[0042] Figure 3 A flowchart diagram of the second embodiment of the query processing method based on hash connection of the present application is provided;
[0043] Figure 4 A flowchart diagram of the third embodiment of the query processing method based on hash connection of the present application is provided;
[0044] Figure 5 A brief flowchart of a query processing method based on hash join provided in Example 3 of the present application;
[0045] Figure 6 This is a schematic diagram of the module structure of a query processing device based on hash connection in an embodiment of the present application;
[0046] Figure 7 This is a schematic diagram of the device structure of the hardware operating environment involved in the query processing method based on hash connection in an embodiment of the present application.
[0047] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0048] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0049] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0050] The main solution of the embodiment of the present application is: input each target query plan of the target query statement into the target cost prediction model respectively to obtain the target contribution value of each query selectivity corresponding to the target query statement; determine the target query cost of each target query plan according to the target contribution value of each query selectivity; select a target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtain the query result of the relational database based on the target execution plan.
[0051] At present, when relational databases estimate the results of multi-condition hash joins (HashJoin), they often use the selectivity of each join condition to calculate using a probability formula: P(AB) = P(A) × P(B). This classic method uses a mathematical function expression of f(p1, p2) = p1 × p2, as shown in the following hash join:
[0052] Connection condition 1 [AND connection condition 2]
[0053] Child Node 1
[0054] Hash
[0055] Child Node 2,
[0056] If join condition 1 and join condition 2 have selectivity 1 (sel_1) and selectivity 2 (sel_2) respectively, then its selectivity = sel_1*sel_2. Extending to a more general scenario, when the number of hash joins increases, the selectivity of each join condition is recorded as p1, p2, p3, until p n , then the mathematical formula of the classic solution is f(p1,p2,...,p n )=p1×p2×...×p n ,Right now The disadvantage of this solution is that it assumes that each connection condition is independent, completely ignoring the coupling and internal correlation between different connection conditions in actual scenarios, which will lead to smaller actual results.
[0057] At the same time, there is a recent improvement called ascending square geometric decrease. For n selection rates, first sort them in ascending order, and the size of the sorted ones is arranged as p1 <p2<p3<...<p n , take the square root of each selection rate geometrically decreasing and then multiply them, the final selection rate calculation formula is: Right now This solution can reduce the neglect of coupling between connection conditions and improve the results to a certain extent.
[0058] However, when the number of hash connections is small (less than 3), both the classic scheme and the ascending square geometric decreasing scheme can achieve satisfactory results. However, as the number of hash connections increases, the prediction of the probability distribution accuracy of this fixed formula in the case of multiple hash connections will deviate further and further from the actual value. The fixed formula essentially denies the inherent coupling of probability between the various selection rates. For the ascending square geometric decreasing scheme and other fixed formula schemes with similar ideas, they only try to approximate the final result as much as possible when the number of connections is small, but this approximation will fail as the number of hash connections increases.
[0059] Data in relational databases are often relatively relevant in business, rather than being unrelated as the ideal model guesses. Therefore, this application uses deep learning methods to optimize the selectivity calculation model of multi-hash connections, explore the inherent connections between data, make the final result closer to the real result, and make the optimizer more accurate.
[0060] This application obtains the target contribution value of each query selectivity corresponding to the target query statement by inputting each target query plan of the target query statement into the target cost prediction model respectively; determines the target query cost of each target query plan according to the target contribution value of each query selectivity; selects the target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtains the query result of the relational database based on the target execution plan. Through the above method, the coupling and internal correlation between different connection conditions in actual scenarios are taken into account, and a more accurate contribution value of the selectivity to the final selectivity can be obtained, thereby improving the calculation accuracy of the query cost corresponding to each query plan, making the final result closer to the real result, achieving the purpose of making the optimizer act more accurately, and solving the current problem of result value deviation when the number of hash connections increases.
[0061] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device capable of realizing the above functions, a query processing device based on hash connection, etc. The following takes a query processing device based on hash connection as an example to illustrate this embodiment and the following embodiments.
[0062] Based on this, the embodiment of the present application provides a query processing method based on hash connection, referring to Figure 1 , Figure 1 This is a flowchart of the first embodiment of the query processing method based on hash connection in the present application.
[0063] In this embodiment, the query processing method based on hash connection includes steps S10 to S30:
[0064] Step S10, inputting each target query plan of the target query statement into the target cost prediction model respectively, and obtaining the target contribution value of each query selectivity corresponding to the target query statement;
[0065] It should be noted that when it is necessary to estimate the result of a multi-condition hash join on a relational database, the target query statement of the relational database is obtained, the target query statement of the relational database is parsed and processed, and multiple query plans corresponding to the target query statement are obtained. Among the multiple query plans, the query plan that uses hash index search is used as the target query plan. Different target query plans contain different comparison fields, comparison conditions, and selectivity corresponding to the comparison conditions.
[0066] It can be understood that the target cost prediction model is composed of LSTM (Long Short-Term Memory), CNN (Convolutional Neural Network) and FCNN (Fully Connected Neural Network). The network layers of the target cost prediction model are as follows: Figure 2 As shown, they are the input layer, the first LSTM layer, the second LSTM layer, the third LSTM layer, the first convolutional layer, the first pooling layer, the second convolutional layer, the second pooling layer, the third convolutional layer, the third pooling layer, the first fully connected layer, and the SoftMax output layer, that is, Input→LSTM_1→LSTM_2→LSTM_3→Conv_1→Pool_1→Conv_2→Pool_2→Conv_3→Pool_3→Dense_1→SoftMax→Output.
[0067] In a specific implementation, each target query plan is packaged into a tuple data (field_i, field_j, operator, selectivity_i_j), where field_i and field_j are query comparison fields, operator is the query comparison condition, and selectivity_i_j is the query selectivity corresponding to the query comparison condition. The tuple data of each target query plan are respectively input into the target cost prediction model. The target cost prediction model will generate the contribution of each query selectivity to the final selectivity in the target query statement. In this embodiment, the contribution of each query selectivity to the final selectivity output by the target cost prediction model is the target contribution value of each query selectivity. Based on the target contribution value of each query selectivity of the target query statement, a suitable estimation model can be generated for each query statement, and its mathematical formula is f(p1, p2, ..., p n )=f1(p1)×f2(p2)×...×f n (p n ),Right now Among them, f i (p i ) corresponds to the actual contribution of the i-th query selectivity to the final selectivity.
[0068] In a feasible implementation manner, before step S10, steps A11 to A13 may be included:
[0069] Step A11, when receiving a target query statement of a relational database, parsing the target query statement to obtain a plurality of query comparison fields, a plurality of query comparison conditions, and a query selection rate corresponding to each query comparison condition;
[0070] It should be noted that the target query statement of the relational database is parsed to obtain the query comparison fields, query comparison conditions and query selectivity corresponding to each query comparison condition corresponding to the target query statement. The query comparison condition may also be called a query comparison connection.
[0071] Step A12: Packaging information of each query comparison field, each query comparison condition, and the query selectivity corresponding to each query comparison condition to generate multiple target query plans of the target query statement.
[0072] It should be noted that the parsed query comparison fields, query comparison conditions, and query selectivity corresponding to each query comparison condition are integrated to form a structured data unit. Based on the packaged information, the query optimizer will explore the possibility of multiple execution plans, including but not limited to different index usage methods, connection orders, and parallel processing strategies. Each possible execution strategy corresponds to a query plan. Among multiple query plans, the query plan using hash index search will be used as the target query plan.
[0073] It can be understood that, for ease of understanding, an example is given below. When the relational database is initialized, the parameters of the calculation model are initialized at the same time. Take the following target query statement as an example:
[0074] SELECT*FROM tab1,tab2,tab3,tab4
[0075] WHERE tab1.id1>tab2.id2
[0076] AND tab2.id2 <tab3.id3
[0077] AND tab3.id3=tab4.id4;
[0078] In the above target query statement, there are 4 comparison fields and 3 comparison connections (each comparison connection consists of three parts: left comparison number, comparison connection, and right comparison number). Each connection has a selectivity, which satisfies 0≤selectivity≤1. Extended to a more general scenario, after a target query statement is parsed and processed, when generating a query plan, there are filed_num comparison fields and condition_num comparison connections. Each comparison connection corresponds to a selectivity_i, where 1≤i≤condition_num. The comparison field, comparison connection, and selectivity are packaged into a tuple data (field_i,field_j,operator,selectivity_i_j).
[0079] In this implementation, when a target query statement of a relational database is received, the target query statement is parsed to obtain multiple query comparison fields, multiple query comparison conditions, and query selectivity corresponding to each query comparison condition; information of each query comparison field, each query comparison condition, and the query selectivity corresponding to each query comparison condition is packaged to generate multiple target query plans for the target query statement, thereby ensuring the accuracy of query plan generation.
[0080] Step S20, determining the target query cost of each target query plan according to the target contribution value of each query selectivity;
[0081] It should be noted that since each target query plan contains one or more query selectivity, based on the target contribution value of each query selectivity, combined with the estimation model corresponding to each query statement The target query cost of each target query plan can be obtained.
[0082] Step S30 , selecting a target execution plan from a plurality of target query plans according to the target query costs of the target query plans, and obtaining a query result of the relational database based on the target execution plan.
[0083] It should be noted that the target query costs of each target query plan are sorted, and according to the sorting results, the target query plan with the lowest target query cost among multiple target query plans is used as the target execution plan. The target execution plan is the best execution plan among all target query plans, and the target execution plan is handed over to the executor for execution, thereby obtaining the query result of the relational database.
[0084] It can be understood that, in order to better reflect the effect achieved by the solution of this embodiment, for a database with a data volume of 100G, after one thousand query experiments, better accuracy was obtained than the classic solution and the ascending square geometric decreasing solution, and the experimental results are shown in Table 1. Through the method of this embodiment, for multi-hash connection scenarios, the estimation accuracy can be improved and the scope of application is wider.
[0085] Table 1
[0086]
[0087] The present embodiment provides a query processing method based on hash connection, which obtains the target contribution value of each query selectivity corresponding to the target query statement by inputting each target query plan of the target query statement into the target cost prediction model respectively; determines the target query cost of each target query plan according to the target contribution value of each query selectivity; selects the target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtains the query result of the relational database based on the target execution plan. Through the above method, the coupling and internal correlation between different connection conditions in actual scenarios are taken into account, and a more accurate contribution value of the selectivity to the final selectivity can be obtained, thereby improving the calculation accuracy of the query cost corresponding to each query plan, making the final result closer to the real result, achieving the purpose of making the optimizer act more accurately, and solving the current problem of result value deviation when the number of hash connections increases.
[0088] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above-mentioned embodiment 1 can be referred to the above introduction, and will not be repeated in the following. Figure 3 , step S10, the query processing method based on hash connection also includes steps S11 to S13:
[0089] Step S11, inputting each target query plan of the target query statement into the target cost prediction model respectively, performing relationship mining through the LSTM network in the target cost prediction model, and determining the query association relationship in each target query plan;
[0090] It should be noted that the tuple data corresponding to each target query plan is respectively input into the target cost prediction model, and the LSTM network in the target cost prediction model mines the intrinsic relationship between the query selectivity and the query comparison field and the intrinsic relationship between the query comparison condition in each target query plan. In this embodiment, the intrinsic relationship between the query selectivity and the query comparison field and the intrinsic relationship between the query comparison condition are the query association relationship in the query plan.
[0091] Step S12, extracting features based on the query association relationship in each target query plan and the CNN network in the target cost prediction model to determine target feature information of each target query plan;
[0092] Step S13, performing cost prediction according to the target feature information of each target query plan and the FCNN network in the target cost prediction model, and determining the target contribution value of each query selectivity corresponding to the target query statement;
[0093] It should be noted that the CNN network in the target cost prediction model extracts the corresponding target feature information of each target query plan according to the query association relationship of each target query plan, and the FCNN network in the target cost prediction model performs cost prediction based on the target feature information of each target query plan to generate the contribution of each query selectivity to the final selectivity in the target query statement.
[0094] This embodiment provides a query processing method based on hash connection, which inputs each target query plan of the target query statement into the target cost prediction model, performs relationship mining through the LSTM network in the target cost prediction model, and determines the query association relationship in each target query plan; performs feature extraction based on the query association relationship in each target query plan and the CNN network in the target cost prediction model, and determines the target feature information of each target query plan; performs cost prediction based on the target feature information of each target query plan and the FCNN network in the target cost prediction model, and determines the target contribution value of each query selectivity corresponding to the target query statement. In the above manner, a more accurate actual contribution of the selectivity to the final selectivity can be obtained, which lays the foundation for the accurate calculation of the subsequent query cost.
[0095] Based on the first embodiment and / or the second embodiment of the present application, in the third embodiment of the present application, the same or similar contents as those of the above-mentioned first and second embodiments can refer to the above introduction, and will not be repeated later. Figure 4 Before step S10, the query processing method based on hash connection also includes steps S01 to S05:
[0096] Step S01, obtaining multiple sample query statements and multiple sample query plans for each sample query statement;
[0097] It should be noted that the sample query statement refers to a query statement used to train the cost prediction model, each sample query statement corresponds to multiple sample query plans, each sample query plan includes a corresponding sample comparison field, a sample comparison condition, and a sample selection rate corresponding to the sample comparison condition. In this embodiment, the query method corresponding to each sample query plan is to use a hash index search.
[0098] Step S02, inputting each sample query plan of each sample query statement into the cost prediction model to be trained, and determining the predicted contribution value of each sample selection rate of each sample query statement;
[0099] It should be noted that the cost prediction model to be trained refers to an untrained target cost prediction model. Each sample query plan corresponding to each sample query statement is packaged into a tuple data, and then respectively input into the cost prediction model to be trained. The cost prediction model to be trained will output the actual contribution of each sample selection rate to the final selection rate in each sample query statement. In this embodiment, the actual contribution of each sample selection rate of each sample query statement output by the cost prediction model to the final selection rate is the predicted contribution value of each sample selection rate of each sample query statement.
[0100] In a feasible implementation manner, before step S02, steps B11 to B13 may also be included:
[0101] Step B11, obtaining the number of neurons of the initial cost prediction model;
[0102] It should be noted that the initial cost prediction model refers to an uninitialized neural network model, which is composed of LSTM (Long Short-Term Memory), CNN (Convolutional Neural Network) and FCNN (Fully Connected Neural Network), and the number of neurons of the initial cost prediction model is obtained.
[0103] Step B12, determining the initial parameter values of the model according to the number of neurons;
[0104] It should be noted that the Normalization method is used to determine the parameters of the initialization model according to the number of neurons. Among them, n [layer-1] is the number of neurons in layer-1. The initial parameter value w can be set from a mean of 0 and a variance of 1 / n [layer-1] The parameters of the initialized model are obtained from the normal distribution of the model.
[0105] Step B13, initializing the parameters of the initial cost prediction model using the model initial parameter values to obtain the cost prediction model to be trained.
[0106] It should be noted that the initial cost prediction model is initialized by the initial parameter values of the model. The initialized initial cost prediction model is the cost prediction model to be trained. The above method helps to avoid the neural network from falling into a local optimal value at the beginning, which helps to speed up the training of the neural network.
[0107] This embodiment obtains the number of neurons of the initial cost prediction model; determines the initial parameter values of the model according to the number of neurons; initializes the parameters of the initial cost prediction model through the initial parameter values of the model to obtain the cost prediction model to be trained, thereby improving the training speed and performance of the model.
[0108] Step S03, determining a predicted execution plan for each sample query statement according to the predicted contribution value of each sample selection rate of each sample query statement, and obtaining a predicted execution result of the sample database under each sample query statement based on the predicted execution plan of each sample query statement;
[0109] It should be noted that the sample query cost of each sample query plan corresponding to each sample query statement is calculated based on the predicted contribution value of each sample selection rate of each sample query statement. The sample query plan with the lowest sample query cost under each sample query statement is used as the predicted execution plan of each sample query statement. The executor executes the query in the sample database according to the predicted execution plan of each sample query statement, thereby obtaining the execution result corresponding to the sample database under the predicted execution plan of each sample query statement, which is the predicted execution result of the sample database under each sample query statement.
[0110] Step S04, comparing the predicted execution result of the sample database under each sample query statement with the actual execution result of the sample database under each sample query statement;
[0111] It should be noted that after obtaining multiple sample query statements, each sample query statement is parsed to generate multiple feasible query plans corresponding to the sample query statements. The sample database will estimate the query costs of all plans including the hash join solution, select the plan with the lowest query cost to execute, and obtain the actual execution results of the sample database under each sample query statement. In addition to returning the results to the upper-level caller, they will also be fed back to the deep learning module of the cost prediction model to be trained for comparison with the actual execution results of the sample database under each sample query statement.
[0112] Step S05, optimizing the cost prediction model to be trained according to the comparison result to obtain a target cost prediction model.
[0113] It should be noted that the predicted execution results and actual execution results under each sample query statement of the sample database are compared to calculate the gradient of model training, thereby training the model parameters, and optimizing the cost prediction model to be trained by training the model parameters to obtain the trained cost prediction model to be trained. The trained cost prediction model to be trained is the target cost prediction model.
[0114] In a feasible implementation, step S05 may include steps C11 to C14:
[0115] Step C11, determining iterative training parameters and iterative learning rate according to the comparison result;
[0116] It should be noted that the parameter ε of the nth round is determined by comparing the predicted execution results and the actual execution results of the sample database under each sample query statement. n And the learning rate η of the current round, the parameters of the nth round are the iterative training parameters, and the learning rate of the current round is the iterative learning rate.
[0117] Step C12, performing parameter calculation based on the iterative training parameters and the iterative learning rate to obtain current training parameters;
[0118] It should be noted that the parameters of the n+1th round are calculated based on the iterative training parameters and the iterative learning rate. The parameters of the n+1th round are the current training parameters.
[0119] Step C13, calculating the learning rate according to the current training parameters and the preset momentum coefficient to determine the training learning rate;
[0120] It should be noted that the preset momentum coefficient is the preset momentum coefficient β. The learning rate is calculated by the calculation method of the current training parameters and the preset momentum coefficient to determine the learning rate of the n+1th round. Among them, η n is the learning rate of the nth round, and γ is a variable related to the number of neural network training times. In this embodiment, the learning rate of the n+1th round is the training learning rate.
[0121] Step C14, optimizing the cost prediction model to be trained according to the training learning rate to obtain a target cost prediction model.
[0122] It should be noted that by optimizing the cost prediction model to be trained through training learning rate, a trained target cost prediction model can be obtained. This embodiment uses the gradient descent method with momentum to optimize the parameters of the neural network. When the number of queries is small in the early stage, a larger learning rate is used to train the neural network faster to obtain the optimal solution.
[0123] This embodiment determines the iterative training parameters and the iterative learning rate according to the comparison results; performs parameter calculation based on the iterative training parameters and the iterative learning rate to obtain the current training parameters; performs learning rate calculation according to the current training parameters and the preset momentum coefficient to determine the training learning rate; optimizes the cost prediction model to be trained according to the training learning rate to obtain the target cost prediction model, thereby ensuring the training efficiency and performance of the model.
[0124] The present embodiment provides a query processing method based on hash connection, and the present embodiment obtains multiple sample query statements and multiple sample query plans of each sample query statement; inputs each sample query plan of each sample query statement into the cost prediction model to be trained, and determines the predicted contribution value of each sample selection rate of each sample query statement; determines the predicted execution plan of each sample query statement according to the predicted contribution value of each sample selection rate of each sample query statement, and obtains the predicted execution result of the sample database under each sample query statement based on the predicted execution plan of each sample query statement; compares the predicted execution result of the sample database under each sample query statement with the actual execution result of the sample database under each sample query statement; optimizes the cost prediction model to be trained according to the comparison result to obtain the target cost prediction model, thereby ensuring the training efficiency and performance of the model.
[0125] For example, to help understand the implementation process of the query processing method based on hash connection obtained by combining the present embodiment with the above-mentioned embodiment 1 and embodiment 2, please refer to Figure 5 , Figure 5 A brief flowchart of the training process in a query processing method based on hash join is provided, specifically:
[0126] This embodiment combines LSTM (Long Short-Term Memory), CNN (Convolutional Neural Network) and FCNN (Fully Connected Neural Network) to propose a neural network model for a multi-hash connection estimation method for a relational database, that is, a target cost prediction model. In this model, the input layer data includes a query plan composed of tuples (comparison field, comparison condition, selection rate), and the first three layers of the model are long short-term memory networks, which are used to mine the intrinsic relationship between the selection rate and the comparison field and the comparison condition. Different from the existing scheme that considers the intrinsic connection of the selection rate purely from a mathematical perspective, the combination of the selection rate and the comparison field is more conducive to the training of the neural network. After the training of the neural network model is completed, the parsing result of the query statement is input into the network model, and the optimal execution scheme is selected according to the query cost calculated by the model, and the execution is handed over to the executor for execution, and the predicted execution result (that is, the query result predicted by the model) is returned to the upper caller.
[0127] In this embodiment, the normalization method is used to initialize the neural network parameters. Among them, n [layer-1] is the number of neurons in layer-1. The initial parameter value w can be set from a mean of 0 and a variance of 1 / n [layer-1] It is obtained from the normal distribution of , which helps to prevent the neural network from falling into a local optimal value at the beginning and helps to speed up the training of the neural network.
[0128] In this embodiment, after parsing the query statement, multiple feasible query plans are generated. The database estimates the query cost of all plans including the hash join solution, selects the plan with the lowest query cost, and obtains the actual result. In addition to returning the result to the upper-level caller, it will also be fed back to the deep learning module for comparison with the predicted execution result, calculation of gradients, and training of model parameters. Among them, ε n represents the parameters of the nth round, and η is the learning rate of this round. This embodiment uses the gradient descent method with momentum to optimize the parameters of the neural network. When the number of queries is small in the early stage, a larger learning rate is used to train the neural network faster to obtain the optimal solution. Among them, η n is the learning rate of the nth round, γ is a variable related to the number of neural network training times, and β is the momentum coefficient.
[0129] In this embodiment, the execution cost is calculated by the trained target cost prediction model, and the executor selects the optimal solution to execute the query. The updated neural network parameters and model are used to calculate the selectivity of the hash connection, and the above steps are repeated to achieve the fitting of the actual selectivity calculation. After sufficient query training, the target cost prediction model can generate a suitable estimation model for each target query statement, and its mathematical formula is f(p1, p2, ..., p n )=f1(p1)×f2(p2)×...×f n (p n ),Right now Among them, f i (p i ) corresponds to the actual contribution of the ith selection rate to the final selection rate.
[0130] This embodiment can have better estimation accuracy for multi-hash connection scenarios, and can train unique estimation models for different query statements, with a wider range of adaptability.
[0131] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the query processing method based on hash connection of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0132] This application also provides a query processing device based on hash connection, please refer to Figure 6 , the query processing device based on hash connection includes:
[0133] An input module 10 is used to input each target query plan of the target query statement into the target cost prediction model to obtain a target contribution value of each query selectivity corresponding to the target query statement;
[0134] A processing module 20, configured to determine a target query cost of each target query plan according to a target contribution value of each query selectivity;
[0135] The selection module 30 is used to select a target execution plan from multiple target query plans according to the target query costs of each target query plan, and obtain a query result of the relational database based on the target execution plan.
[0136] Optionally, the input module 10 is further used for:
[0137] The target query plans of the target query statements are respectively input into the target cost prediction model, and the relationship mining is performed through the LSTM network in the target cost prediction model to determine the query association relationship in each target query plan; feature extraction is performed based on the query association relationship in each target query plan and the CNN network in the target cost prediction model to determine the target feature information of each target query plan; cost prediction is performed based on the target feature information of each target query plan and the FCNN network in the target cost prediction model to determine the target contribution value of each query selectivity corresponding to the target query statement.
[0138] Optionally, the input module 10 is further used for:
[0139] Acquire multiple sample query statements and multiple sample query plans of each sample query statement; input each sample query plan of each sample query statement into the cost prediction model to be trained respectively, and determine the predicted contribution value of each sample selection rate of each sample query statement; determine the predicted execution plan of each sample query statement according to the predicted contribution value of each sample selection rate of each sample query statement, and obtain the predicted execution result of the sample database under each sample query statement based on the predicted execution plan of each sample query statement; compare the predicted execution result of the sample database under each sample query statement with the actual execution result of the sample database under each sample query statement; optimize the cost prediction model to be trained according to the comparison result to obtain the target cost prediction model.
[0140] Optionally, the input module 10 is further used for:
[0141] The number of neurons of the initial cost prediction model is obtained; the initial parameter values of the model are determined according to the number of neurons; the parameters of the initial cost prediction model are initialized by the initial parameter values of the model to obtain the cost prediction model to be trained.
[0142] Optionally, the input module 10 is further used for:
[0143] Determine the iterative training parameters and iterative learning rate based on the comparison results; perform parameter calculation based on the iterative training parameters and iterative learning rate to obtain the current training parameters; perform learning rate calculation based on the current training parameters and the preset momentum coefficient to determine the training learning rate; optimize the training cost prediction model based on the training learning rate to obtain the target cost prediction model.
[0144] Optionally, the input module 10 is further used for:
[0145] When a target query statement of a relational database is received, the target query statement is parsed to obtain multiple query comparison fields, multiple query comparison conditions, and query selectivity corresponding to each query comparison condition; information of each query comparison field, each query comparison condition, and the query selectivity corresponding to each query comparison condition is packaged to generate multiple target query plans of the target query statement.
[0146] The query processing device based on hash connection provided by the present application adopts the query processing method based on hash connection in the above embodiment, and can solve the technical problem of query processing based on hash connection. Compared with the prior art, the beneficial effect of the query processing device based on hash connection provided by the present application is the same as the beneficial effect of the query processing method based on hash connection provided by the above embodiment, and other technical features in the query processing device based on hash connection are the same as the features disclosed in the above embodiment method, which will not be repeated here.
[0147] The present application provides a query processing device based on hash connection, and the query processing device based on hash connection includes: at least one processor; and a memory connected to the at least one processor in communication; wherein the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the query processing method based on hash connection in the above-mentioned embodiment one.
[0148] Reference below Figure 7 , which shows a schematic diagram of the structure of a query processing device based on hash connection suitable for implementing the embodiment of the present application. The query processing device based on hash connection in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 7 The query processing device based on hash connection shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0149] like Figure 7As shown, the query processing device based on hash connection may include a processing device 1001 (such as a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. Various programs and data required for the operation of the query processing device based on hash connection are also stored in RAM1004. The processing device 1001, ROM1002 and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 1003 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 1009. The communication device 1009 can allow the hash-connection-based query processing device to communicate wirelessly or wired with other devices to exchange data. Although the figure shows a hash-connection-based query processing device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems may be implemented or have instead.
[0150] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0151] The query processing device based on hash connection provided by the present application adopts the query processing method based on hash connection in the above embodiment, which can solve the technical problem of query processing based on hash connection. Compared with the prior art, the beneficial effect of the query processing device based on hash connection provided by the present application is the same as the beneficial effect of the query processing method based on hash connection provided by the above embodiment, and other technical features in the query processing device based on hash connection are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.
[0152] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0153] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
[0154] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the query processing method based on hash join in the above-mentioned embodiment.
[0155] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.
[0156] The computer-readable storage medium may be included in the query processing device based on hash connection; or may exist independently without being assembled into the query processing device based on hash connection.
[0157] The computer-readable storage medium carries one or more programs. When the one or more programs are executed by the query processing device based on hash connection, the query processing device based on hash connection performs query processing based on hash connection.
[0158] Computer program code for performing the operations of the present application may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0159] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0160] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.
[0161] The readable storage medium provided by the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned query processing method based on hash join, and can solve the technical problem of query processing based on hash join. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as the beneficial effects of the query processing method based on hash join provided by the above-mentioned embodiment, and will not be repeated here.
[0162] The present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned query processing method based on hash connection when executed by a processor.
[0163] The computer program product provided by the present application can solve the technical problem of query processing based on hash join. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as the beneficial effects of the query processing method based on hash join provided by the above embodiment, which will not be repeated here.
[0164] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A query processing method based on hash join, characterized in that: The query processing method based on hash connection includes: Inputting each target query plan of the target query statement into the target cost prediction model respectively to obtain the target contribution value of each query selectivity corresponding to the target query statement; Determine the target query cost of each target query plan according to the target contribution value of each query selectivity; A target execution plan is selected from a plurality of target query plans according to the target query costs of the target query plans, and a query result of the relational database is obtained based on the target execution plan.
2. The method according to claim 1, characterized in that The step of inputting each target query plan of the target query statement into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statement includes: Inputting each target query plan of the target query statement into the target cost prediction model respectively, performing relationship mining through the LSTM network in the target cost prediction model, and determining the query association relationship in each target query plan; Extracting features based on the query association relationship in each target query plan and the CNN network in the target cost prediction model to determine target feature information of each target query plan; Cost prediction is performed based on the target feature information of each target query plan and the FCNN network in the target cost prediction model to determine the target contribution value of each query selectivity corresponding to the target query statement.
3. The method according to claim 1, characterized in that Before inputting the target query plans of the target query statements into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statements, the method further includes: Obtaining multiple sample query statements and multiple sample query plans for each sample query statement; Inputting each sample query plan of each sample query statement into the cost prediction model to be trained respectively, and determining the predicted contribution value of each sample selection rate of each sample query statement; Determining a predicted execution plan for each sample query statement according to the predicted contribution value of each sample selection rate of each sample query statement, and obtaining a predicted execution result of the sample database under each sample query statement based on the predicted execution plan for each sample query statement; Comparing the predicted execution results of the sample database under each sample query statement with the actual execution results of the sample database under each sample query statement; The cost prediction model to be trained is optimized according to the comparison result to obtain a target cost prediction model.
4. The method according to claim 3, characterized in that Before inputting each sample query plan of each sample query statement into the to-be-trained cost prediction model and determining the predicted contribution value of each sample selection rate of each sample query statement, the method further includes: Get the number of neurons of the initial cost prediction model; Determine the initial parameter value of the model according to the number of neurons; The initial cost prediction model is initialized with the model initial parameter values to obtain a cost prediction model to be trained.
5. The method according to claim 3, characterized in that The step of optimizing the cost prediction model to be trained according to the comparison result to obtain a target cost prediction model includes: Determine iterative training parameters and iterative learning rate according to the comparison results; Perform parameter calculation based on the iterative training parameter and the iterative learning rate to obtain current training parameters; Calculating the learning rate according to the current training parameters and the preset momentum coefficient to determine the training learning rate; The cost prediction model to be trained is optimized according to the training learning rate to obtain a target cost prediction model.
6. The method according to claim 1, characterized in that Before inputting the target query plans of the target query statements into the target cost prediction model to obtain the target contribution value of each query selectivity corresponding to the target query statements, the method further includes: When receiving a target query statement from a relational database, the target query statement is parsed to obtain a plurality of query comparison fields, a plurality of query comparison conditions, and a query selection rate corresponding to each query comparison condition; Information of each query comparison field, each query comparison condition, and the query selectivity corresponding to each query comparison condition is packaged to generate multiple target query plans of the target query statement.
7. A query processing device based on hash connection, characterized in that: The query processing device based on hash connection includes: An input module, used to input each target query plan of the target query statement into the target cost prediction model respectively, to obtain a target contribution value of each query selectivity corresponding to the target query statement; A processing module, used to determine a target query cost of each target query plan according to a target contribution value of each query selectivity; The selection module is used to select a target execution plan from multiple target query plans according to the target query cost of each target query plan, and obtain the query result of the relational database based on the target execution plan.
8. A query processing device based on hash connection, characterized in that: The device comprises: a memory, a processor, and a hash connection-based query processing program stored in the memory and executable on the processor, wherein the hash connection-based query processing program is configured to implement the steps of the hash connection-based query processing method according to any one of claims 1 to 6.
9. A storage medium, characterized in that: The storage medium stores a query processing program based on hash connection. When the query processing program based on hash connection is executed by the processor, the steps of the query processing method based on hash connection according to any one of claims 1 to 6 are implemented.
10. A computer program product, characterized in that The computer program product comprises a query processing program based on hash connection, and when the query processing program based on hash connection is executed by a processor, the steps of the query processing method based on hash connection according to any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Data query method and device, computer equipment, readable storage medium and program product
CN121542309A
Data query method and device, computer device, readable storage medium and program product
CN121542309B
