Sql statement rewriting method, computer device, storage medium and product

CN122507752APending Publication Date: 2026-08-04ZTE CORP
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZTE CORP
Filing Date
2026-07-07
Publication Date
2026-08-04

AI Technical Summary

Technical Problem

现有技术主要依赖基于规则的优化器和代价模型的静态分析在有限时间内搜索改写方案,存在搜索空间受限、难以搜索到最优改写方案,且存在负优化现象等问题,导致对SQL语句的改写效果有限

Benefits of technology

[0008] The SQL statement rewriting method provided in this application generates an initial query plan corresponding to the original SQL statement, which serves as the initial rewriting state of the bundle search state set. Then, the statement rewriting process is iteratively executed: for each rewriting state in the current bundle search state set, a candidate set is generated by applying corresponding matching rewriting rules; target rewriting states are selected from the candidate set through a first screening based on benefit evaluation and a second screening based on a large language model; candidate rewritten SQL statements are generated by reverse transformation based on the new query plan corresponding to the target rewriting state, added to the historical candidate set, and their corresponding new query plans are used as the next round of search states; after the rewriting termination condition is met, the candidate rewritten SQL statement with the lowest query cost is selected from the historical candidate set as the rewriting result of the original SQL statement. This method achieves a full exploration of the rewriting space through bundle search, and the two-stage screening mechanism during the statement rewriting process effectively identifies and retains rewriting schemes with lower query costs, avoids negative optimization, generates better query plans, and improves the SQL statement rewriting effect.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122507752A_ABST
    Figure CN122507752A_ABST
Patent Text Reader

Abstract

This application relates to a method, computer device, storage medium, and product for rewriting SQL statements, and pertains to the field of data processing technology. The method includes: using the initial query plan generated from parsing the original SQL statement as the initial rewriting state of a bundle search state set; iteratively executing the statement rewriting process until a rewriting termination condition is met; selecting the SQL statement with the lowest query cost from a historical candidate set recording all candidate rewritten SQL statements; the statement rewriting process includes: applying matching rewriting rules to each rewriting state in the current bundle search state set to generate a candidate set; selecting the target rewriting state from the candidate set through at least two rounds of filtering; performing a reverse transformation on the new query plan corresponding to the target rewriting state; adding the generated candidate rewritten SQL statement to the historical candidate set; and using the corresponding new query plan as the basis for the next round of iteration.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a method for rewriting SQL statements, a computer device, a storage medium, and a product. Background Technology

[0002] With the development of modern database systems, SQL (Structured Query Language) query optimization has become a key technology for improving performance. Existing technologies mainly rely on rule-based optimizers and static analysis of cost models to search for rewriting solutions within a limited time. However, these technologies suffer from problems such as limited search space, difficulty in finding the optimal rewriting solution, and negative optimization phenomena, resulting in limited effectiveness in rewriting SQL statements. Summary of the Invention

[0003] This application provides an SQL statement rewriting method, computer device, storage medium, and product, which can effectively identify and retain rewriting schemes with lower query costs, avoid negative optimization, generate better query plans, and improve the SQL statement rewriting effect. The technical solution is as follows.

[0004] Firstly, a method for rewriting SQL statements is provided, the method comprising: The original SQL statement is parsed to generate an initial query plan; Using the initial query plan as the initial rewrite state of the cluster search state set, the statement rewrite process is iteratively executed until the rewrite termination condition is met; From the historical candidate set of all candidate rewritten SQL statements that record the entire rewriting process, select the candidate rewritten SQL statement with the lowest query cost as the rewritten SQL statement of the original SQL statement; The statement rewriting process includes: For each rewrite state in the current bundle search state set, apply the matching rewrite rules to generate at least one candidate rewrite state, and aggregate them to obtain a candidate set; the candidate rewrite states include the new query plan generated by the rewrite. The target rewrite state is selected from the candidate set through at least two screenings, wherein the first screening is a pruning screening based on the estimated revenue of the new query plan corresponding to the candidate rewrite state, and the second screening is a further screening of the first screening result using a large language model. The new query plan corresponding to the target rewrite state is reverse-converted to generate the candidate rewrite SQL statement; The candidate rewritten SQL statement is added to the historical candidate set, and the new query plan corresponding to the candidate rewritten SQL statement is used as the rewritten state of the cluster search state set in the next iteration round.

[0005] In a second aspect, a computer device is provided, the computer device comprising a processor and a memory, the memory storing at least one computer program, the at least one computer program being loaded and executed by the processor to implement the above-described SQL statement rewriting method.

[0006] Thirdly, a computer-readable storage medium is provided, wherein at least one computer program is stored in the computer-readable storage medium, the computer program being loaded and executed by a processor to implement the above-described SQL statement rewriting method.

[0007] Fourthly, a computer program product is provided, the computer program product including a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions, which, when executed by a computer, cause the computer to execute to implement the above-described SQL statement rewriting method.

[0008] The SQL statement rewriting method provided in this application generates an initial query plan corresponding to the original SQL statement, which serves as the initial rewriting state of the bundle search state set. Then, the statement rewriting process is iteratively executed: for each rewriting state in the current bundle search state set, a candidate set is generated by applying corresponding matching rewriting rules; target rewriting states are selected from the candidate set through a first screening based on benefit evaluation and a second screening based on a large language model; candidate rewritten SQL statements are generated by reverse transformation based on the new query plan corresponding to the target rewriting state, added to the historical candidate set, and their corresponding new query plans are used as the next round of search states; after the rewriting termination condition is met, the candidate rewritten SQL statement with the lowest query cost is selected from the historical candidate set as the rewriting result of the original SQL statement. This method achieves a full exploration of the rewriting space through bundle search, and the two-stage screening mechanism during the statement rewriting process effectively identifies and retains rewriting schemes with lower query costs, avoids negative optimization, generates better query plans, and improves the SQL statement rewriting effect.

[0009] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and do not limit this application. Attached Figure Description

[0010] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0011] Figure 1 A flowchart illustrating an exemplary embodiment of this application is shown. Figure 2A schematic diagram of the architecture of a first screening model provided in an exemplary embodiment of this application is shown; Figure 3 A schematic diagram of an SQL statement rewriting system provided in an exemplary embodiment of this application is shown; Figure 4 A flowchart illustrating an SQL statement rewriting method provided in another exemplary embodiment of this application is shown; Figure 5 A structural block diagram of a computer device illustrated in an exemplary embodiment of this application is shown; Figure 6 A structural block diagram of a computer device illustrated in another exemplary embodiment of this application is shown. Detailed Implementation

[0012] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0013] SQL statements are the core interaction interface in relational database systems. In a database, the execution of an SQL statement typically involves multiple processing stages, including syntax parsing, semantic analysis, logical plan generation, query optimization, and physical execution plan generation. Among these, query rewriting is a crucial step in the query optimization process. Its goal is to transform the original SQL statement while maintaining semantic equivalence, thereby improving execution efficiency, reducing resource consumption, or adapting to specific execution environments. To enhance the space exploration capability of SQL statements in complex query scenarios and generate semantically equivalent and performance-improving rewritten SQL statements, this application provides an SQL statement rewriting method that enables high-performance SQL statement rewriting.

[0014] Figure 1 A flowchart illustrating an exemplary embodiment of this application shows a method for rewriting SQL statements. This method can be executed by a computer device, which can be implemented as a server or a terminal device; for example... Figure 1 As shown, the SQL statement rewriting method may include the following steps.

[0015] Step 110: Parse the original SQL statement to generate an initial query plan.

[0016] The original SQL statement is a user-submitted SQL string received through the interactive interface. At the same time as receiving the original SQL statement, the target database information submitted by the user can also be received. The target database is used to indicate the data query object to be executed by the original SQL statement. The target database information may include the target database identifier and / or the target database type.

[0017] When parsing the original SQL statement, the computer device can combine the parsing with the target database information. For example, this process can be implemented as follows: Obtain the schema information (i.e., schema information) of the target database, which includes, but is not limited to, table structure, column types, and index information; When parsing the original SQL statement, the computer device can combine the schema information of the target database to parse the text information of the SQL statement and generate an initial query plan.

[0018] Parsing the text information of SQL statements can include using schema information obtained from the target database to perform lexical analysis, syntax analysis, and semantic verification on the SQL statement text information. This involves checking the existence and validity of the tables, columns, and operation types referenced in the SQL statement. After successful verification, the SQL statement text information is converted into a tree structure, and the schema information of the corresponding operation object is bound to each operator node in the tree structure to generate an initial query plan. Each node in this data tree represents a database operator used to perform at least one operation among data scanning, data filtering, column projection, table join, data aggregation, or result sorting. Each operator node contains detailed attributes, including input / output column mapping, predicate condition expression, join type, and sort key, etc. For example, this tree structure can be implemented as a RelNode (Relational...). The tree structure (relational nodes) includes various operator nodes such as Filter, Project, Join, Aggregate, and Sort operators. This tree structure provides a foundation for subsequent operator-level rewriting. Depending on the specific application requirements, this tree structure can also be implemented in other types, and this application embodiment does not impose any limitations on this.

[0019] Step 120: Using the initial query plan as the initial rewrite state of the bundle search state set, iteratively execute the statement rewrite process until the rewrite termination condition is met.

[0020] The bundle search state set can store the rewriting state corresponding to the query plan to be rewritten in the current round. The rewriting state can include the rewriting rule identifier, the rewriting operator node identifier, and the generated new query plan. In the first iteration round, the initial rewriting state of the bundle search state is the initial query plan, and the rewriting rule identifier and the rewriting operator node identifier are empty. The rewriting termination condition can include the iteration count reaching the preset maximum iteration count, or the inability to generate a new candidate rewriting state.

[0021] The process of rewriting this statement includes: S121, for each rewrite state in the current bundle search state set, apply the matching rewrite rules to generate at least one candidate rewrite state, and aggregate them to obtain a candidate set; the candidate rewrite states include the new query plan generated by the rewrite.

[0022] The candidate rewrite state generation process includes a rule matching process and a rule application process. During the rule matching process, the computer device can perform operator-level rule matching to determine the rule matching status of each operator node in the query plan contained in each rewrite state. For operator nodes that match rewrite rules, the operator node is rewritten according to the corresponding rewrite rules to generate a new query plan. For example, the predicate pushdown rule is applicable when there is a Filter operator in the query plan, and a Join operator exists below the Filter operator, and the Filter operator's filtering... All columns involved in the conditions come from a side table of the Join operator; if the applicable conditions are met, i.e., a successful match with the predicate pushdown rule, the Filter operator is pushed down to the corresponding side below the Join operator; during the matching process, the computer device checks whether the status of the traversed Filter operator in the query plan matches the applicable conditions of the above predicate pushdown rule. If a match is found, the Filter operator is pushed down to the corresponding side below the Join operator, generating a new query plan; the applicable conditions and post-match operations differ for different rewriting rules, and this application embodiment does not impose any restrictions on them. It should be noted that one operator node can correspond to one or more different rewriting rules.

[0023] For example, if a user enters the following SQL statement (target database is PostgreSQL): select lineitem.l_shipinstruct, orders.o_orderpriority, customer.c_nationkey from customer join orders on orders.o_custkey=customer.c_custkeyjoin lineitem on lineitem.l_orderkey=orders.o_orderkey where customer.c_nationkey<= 24 order by orders.o_orderpriority asc, customer.c_nationkeydesc; After connecting to the target database and obtaining the schema information, the SQL statement is parsed based on this schema information, and the following query plan is constructed: LogicalSort(sort0=[$1], sort1=[$2], dir0=[ASC], dir1=[DESC]) LogicalProject(l_shipinstruct=[$30], o_orderpriority=[$13], c_nationkey=[$3]) LogicalFilter(condition=[<=($3, 24)]) LogicalJoin(condition=[=($8, $17)], joinType=[inner]) LogicalJoin(condition=[=($0, $9)], joinType=[inner]) JdbcTableScan(table=[[tpch100m, customer]]) JdbcTableScan(table=[[tpch100m, orders]]) JdbcTableScan(table=[[tpch100m, lineitem]]) For each operator node in the query plan, rewrite rules are matched. After performing operator rewrite operations on the operator nodes that match the rewrite rules, multiple candidate rewrite states are generated. Each operator rewrite operation corresponds to the generation of a new query plan. Taking one operator rewrite operation as an example, its matching rewrite rule is SORT_PROJECT_TRANSPOSE (i.e., sort-projection exchange rule). After completing the corresponding operator rewrite operation, the new query plan is as follows: LogicalProject(l_shipinstruct=[$30], o_orderpriority=[$13], c_nationkey=[$3]) LogicalSort(sort0=[$13], sort1=[$3], dir0=[ASC], dir1=[DESC]) LogicalFilter(condition=[<=($3, 24)]) LogicalJoin(condition=[=($8, $17)], joinType=[inner]) LogicalFilter(condition=[=($0, $9)]) LogicalJoin(condition=[true], joinType=[inner], semiJoinDone=[true]) LogicalJoin(condition=[=($0, $9)], joinType=[semi]) JdbcTableScan(table=[[tpch100m, customer]]) JdbcTableScan(table=[[tpch100m, orders]]) JdbcTableScan(table=[[tpch100m, orders]]) JdbcTableScan(table=[[tpch100m, lineitem]]) Similarly, for target operator nodes in the query plan that match other rewriting rules, the executable code of the successfully matched rewriting rules is applied to generate their respective new query plans. After rewriting the query plans in each rewriting statement in the current bundle search state set and generating the corresponding candidate rewriting states, a candidate set is obtained. If a query plan does not have a valid rewriting, the current path search corresponding to that query plan is terminated.

[0024] S122, the target rewriting state is selected from the candidate set through at least two screenings. The first screening is a pruning screening based on the estimated revenue of the new query plan corresponding to the candidate rewriting state, and the second screening is a further screening of the first screening results using a large language model.

[0025] In this embodiment, the computer device can perform rewrite state filtering through a cluster decision-making method driven by cascading large and small models. In the first filtering, a first filtering model can be used for coarse pruning, and in the second filtering, a large language model can be used for batch comparison to refine the rewrite states. This achieves aggregation and parallel decision-making of rewrite states, overcoming the contradiction between the context window limitation of the large language model and the parallelization requirement of cluster search. Through the initial pruning, the aggregated candidate rewrite states can be filtered to a first preset number. This first preset number can be set based on actual needs. By limiting the number, the context length of the input large language model can be controlled, thereby achieving low-latency and high-consistency batch parallel decision-making.

[0026] The first screening model can estimate the revenue of candidate rewriting states based on the structural features and rewriting matching features of the query plan, and obtain the revenue prediction results. The structural features of the query plan refer to the quantitative information used to describe the tree structure and composition of the query plan, including the occurrence frequency of various operators, the depth of the tree structure, the hierarchical relationship between operators, and the attribute parameters of operator nodes. The rewriting matching features can refer to the applicability of the rewriting rules in the current query plan, which can include the number of operator nodes matched by the rules in the query plan, the dependency or conflict relationship between rules, etc.

[0027] The training sample set for this first screening model can be constructed based on synthetic datasets of different parameter sizes and real business datasets to ensure that the trained model maintains robustness and generalization in complex scenarios close to real production environments. For example, the training samples can cover different scales of TPC-H and TPC-DS (scale parameter ScaleFactor 1 to 100, simulating test libraries from GB to TB). The datasets used include a synthetic dataset (configured for a production-level database) and a real business dataset from JOB (IMDB, Internet Movie Database). When determining training labels, the original query plan can be used as a starting point. An exhaustive search can be performed within a preset rewriting depth, traversing all possible rewriting rule application paths. For each path, the query cost of the final query plan is actually executed or estimated, and the decrease in query cost relative to the original query plan is calculated. The benefit corresponding to the maximum decrease in cost across all paths is used as the real benefit label for each intermediate rewriting state on that path. Based on the training sample set containing "rewriting state features - real benefit labels," the first screening model is trained. After the training completion condition is met, the first screening model is obtained. The rewriting state features include the structural features of the query plan and the rewriting matching features.

[0028] Furthermore, to improve the prediction accuracy of the first screening model, during application, high-value samples can be continuously accumulated to dynamically iteratively train and optimize the model. This addresses the problem of static models being unable to adapt to dynamically changing business data distributions, and ensures continuous high-precision pruning capabilities by automatically correcting cognitive biases. In this dynamic iterative optimization process, the computer automatically records the optimal rewriting path, standardizing and encapsulating key information such as the corresponding query plan topology, available rule set, historical operation sequence, and actual cost reduction rate according to feature engineering standards consistent with the training phase. This forms a new "rewriting state feature - predicted benefit label" enhanced sample library, which is then persistently stored. When conditions for enhanced training of the first screening model are triggered, the computer can iteratively optimize and train the model based on the new sample library. For example, when the accumulated enhanced samples meet a preset threshold, or when the prediction accuracy of the first screening model is checked, if the number of incorrectly predicted samples reaches a certain threshold (e.g., 10% of the total samples in one period, which can be adjusted according to the actual situation), a retraining process is triggered based on these enhanced samples. This process injects newly accumulated biased samples into the training set and uses a full fine-tuning strategy to update the neural network weights, thereby correcting the model's cognitive biases for specific complex scenarios and ensuring that the prediction algorithm can maintain high-precision pruning capabilities as the distribution of business data evolves.

[0029] After the first screening model prunes and filters the candidate rewriting states in the candidate set, the large language model can be called to make a decision on the rewriting scheme for the remaining candidate rewriting states in the candidate set. The output result is the second preset number of target rewriting states. The first preset number of candidate rewriting states obtained by the first screening model is greater than the second preset number of target rewriting states obtained by the large language model.

[0030] S123, reverse the transformation of the new query plan corresponding to the target rewrite state to generate candidate rewrite SQL statements.

[0031] After obtaining the target rewrite status, the new query plan corresponding to the target rewrite status can be input into the query plan to SQL statement executor, so that the executor performs the corresponding SQL reverse transformation on the new query plan and outputs a new SQL statement, namely the candidate rewrite SQL statement. By physically isolating the rewrite status filtering from the SQL statement rewrite, the filtering decision-making process can be decoupled from the execution rewrite process, the generation risk can be isolated, and the certainty and reliability of the rewrite result can be ensured.

[0032] S124, add the candidate rewritten SQL statement to the historical candidate set, and use the new query plan corresponding to the candidate rewritten SQL statement as the rewritten state of the cluster search state set in the next iteration round.

[0033] The historical candidate set stores all candidate rewrite SQL statements generated throughout the rewrite process. The rewrite status of this bundle search status set is updated to the query plan of the candidate rewrite SQL statements generated in this rewrite process, so as to serve as the initial rewrite status for the next round of statement rewrite.

[0034] If the rewriting termination condition is not met, iteratively execute steps S121 to S124 until the rewriting termination condition is met, and obtain a complete set of historical candidates.

[0035] Step 130: From the historical candidate set that records all candidate rewritten SQL statements throughout the entire rewriting process, select the candidate rewritten SQL statement with the lowest query cost as the rewritten SQL statement of the original SQL statement.

[0036] After obtaining each candidate rewritten SQL statement, the computer device can send an execution cost query request to the target database to obtain the query cost of each candidate rewritten SQL statement. During this process, the computer device can call the target database optimizer to perform SQL statement parsing and cost estimation. The processing procedure is consistent with the optimization process used by the target database when executing SQL statements, so that the obtained cost estimate can truly reflect the performance of each candidate rewritten SQL statement in the actual execution environment. For example, the cost query request can be: EXPLAIN FORMAT=JSON [SQL statement] (adapt to the corresponding statement according to different database management systems).

[0037] Then, each candidate rewritten SQL statement, its query cost, and its corresponding query plan are associated and stored in the historical candidate set. In other words, the historical candidate set can contain a triplet of candidate rewritten SQL statement, query cost, and query plan for each candidate rewritten SQL statement.

[0038] Extract each candidate rewritten SQL statement from the historical candidate set, compare the query costs of each candidate rewritten SQL statement, and select the candidate rewritten SQL statement with the lowest query cost as the rewritten SQL statement of the original SQL statement.

[0039] This historical candidate set can serve as a rewrite history record, providing a basis for determining whether a new query plan is repeated. Furthermore, this historical candidate set can also be used to extract features, serving as input features for the machine learning model in subsequent iterations. This assists the first screening model in predicting returns. For example, when the first screening model makes a prediction for the current candidate rewrite state, it can extract rewrite trajectories, cost trends, and final return results of historical rewrite states with similar structures to the current state from the historical candidate set as additional features. This allows the first screening model to learn from historical experience, determine the current candidate rewrite state's exploration status, and the historical effects of rewrite paths with similar structures, thereby improving the accuracy and stability of return estimation.

[0040] After obtaining the rewritten SQL statement of the original SQL statement, subsequent query operations can be performed based on the rewritten SQL statement to access the target database for data querying. By using the optimized rewritten SQL statement to replace the original SQL statement to execute the query, query results can be obtained at a lower execution cost, reducing database resource overhead, shortening query response latency, and thus improving the overall query performance of the database system.

[0041] In summary, the SQL statement rewriting method provided in this application generates an initial query plan corresponding to the original SQL statement, which serves as the initial rewriting state of the bundle search state set. Then, the statement rewriting process is iteratively executed: for each rewriting state in the current bundle search state set, a candidate set is generated by applying corresponding matching rewriting rules; target rewriting states are selected from the candidate set through a first screening based on benefit evaluation and a second screening based on a large language model; candidate rewritten SQL statements are generated by reverse transformation based on the new query plan corresponding to the target rewriting state, added to the historical candidate set, and their corresponding new query plans are used as the next round of search states; after the rewriting termination condition is met, the candidate rewritten SQL statement with the lowest query cost is selected from the historical candidate set as the rewriting result of the original SQL statement. This method achieves a full exploration of the rewriting space through bundle search, and the two-stage screening mechanism during the statement rewriting process effectively identifies and retains rewriting schemes with lower query costs, avoids negative optimization, generates better query plans, and improves the rewriting effect of SQL statements.

[0042] Based on the above embodiments: As an optional embodiment, each rewrite state may include: the applied rewrite rule identifier, the rewritten operator node identifier, and the generated new query plan. For example, the triple information of the rewrite state can be represented as {rewrite rule identifier, operator node identifier, new query plan}, wherein the rewrite rule identifier is associated with the corresponding rewrite rule. That is, the triple information of the rewrite state is used to indicate that a new query plan is generated after applying the specified rewrite rule to the specified operator node.

[0043] Each rewriting rule can have a bimodal representation of executable code and natural language description. The executable code encapsulates and improves the rewriting algorithm of the query plan generator, supporting the application of a single rewriting rule to a single operator node in the programming environment. This executable code has been verified to ensure the logical equivalence of the actual rewriting. The natural language description can be automatically generated by the large language model based on the web page documentation of the query plan generator and the rewriting algorithm. The content can cover the usage scenario characteristics of the rewriting rule (such as "applicable to the case of redundant self-joins and non-null join keys"), the essence of semantic transformation (such as "promoting nested subqueries to semi-joins to eliminate duplicate calculations"), the expected potential benefits, and the application constraints (such as "requiring the input table to have unique key constraints"), thereby transforming the program logic into domain knowledge with causal reasoning value.

[0044] As an optional embodiment, a rewriting rule library can be pre-built in this application. The rewriting rule library may contain predefined rewriting rules, each rewriting rule has a corresponding rewriting rule identifier, and the rewriting rule library may store the executable code of the rewriting rules and the bimodal representation of the natural semantic description.

[0045] The candidate rewrite state generation process includes a rule matching process and a rule application process. For each rewrite state in the current bundle search state set, the matching rewrite rules are applied to generate at least one candidate rewrite state, including: For each rewrite state in the current bundle search state set, the pre-built rewrite rule library is traversed, and the rewrite rule is matched for each operator node in the query plan to obtain the successfully matched rewrite rule and the corresponding target operator node; the rewrite rule library contains the executable code and natural language description of each rewrite rule. Apply the executable code of the successfully matched rewrite rule to the target operator node to generate a new query plan; If the new query plan is appearing for the first time, generate a candidate rewrite status for that new query plan.

[0046] During the rule rewriting matching process, the computer device can traverse all query plans in the bundle search state set, traverse the pre-built rewriting rule library, perform a depth-first search on the query plans, and identify legally applicable rewriting rules. Specifically, during matching, by traversing each operator node in the tree structure of the query plan, it checks whether the current operator node and its subtree meet the applicable conditions of a certain rewriting rule. If the applicable conditions of a certain rewriting rule are met, it is determined that the operator node matches the rewriting rule. In this case, the computer device can apply the executable code of the successfully matched rewriting rule to the matched operator node (only a single node) on a copy of the tree structure to generate a new query plan.

[0047] After generating a new query plan, in order to avoid rewrite loops, the computer device can query the current rewrite history: if the new query plan already exists in the rewrite history, the new query plan is discarded; if the new query plan does not exist in the rewrite history, the candidate rewrite status corresponding to the new query plan is recorded. The computer device can determine whether there is a duplication by comparing the consistency of the query plan structure between the new query plan and the historical query plans in the rewrite history.

[0048] After rewriting each query plan in the current cluster search state set and generating the corresponding candidate rewrite states, a candidate set is obtained and the filtering stage begins.

[0049] During the screening phase, the computer device can determine the target rewriting state from the candidate set through at least two screenings. As an optional embodiment, the computer device can perform two screenings using a first screening model and a large language model respectively, selecting the target rewriting state from the candidate set through at least two screenings, including: The first screening model is used to estimate the revenue of each candidate rewriting state in the candidate set, and the revenue estimation results of each candidate rewriting state are obtained. The first screening model is used to estimate the revenue based on the coding features of the new query plan corresponding to the candidate rewriting state. The coding features include at least one of the following: operator-level statistical features, rule availability frequency features, rule application trajectory features, and cost and cardinality features. The operator-level statistical features are used to characterize the number of times each type of operator appears in the query plan. The rule availability frequency features are used to characterize the number of operator nodes applicable to each rewriting rule. The rule application trajectory features are used to characterize the rewriting rules applied from the parent rewriting state to the current rewriting state. The cost and cardinality features are used to characterize the estimated query cost and estimated number of output rows of the query plan corresponding to the candidate rewriting state. Based on the profit prediction results of each candidate rewrite state, pruning and filtering are performed to obtain the first candidate subset; The candidate rewrite states in the first candidate subset are input into the large language model for secondary filtering to obtain the second candidate subset containing the target rewrite state.

[0050] The first screening model can be a lightweight machine learning model built on a multi-layer neural network. Its input can include operator-level statistical features, rule availability frequency features, rule application trajectory features, and cost and cardinality features. The output is an interval estimate of the predicted benefit. Then, pruning and screening are performed according to the predicted benefit of each candidate rewriting state, and a first preset number of candidate rewriting states are selected to form the first candidate subset as candidates for the decision of the large language model.

[0051] The first screening model can be trained based on a training sample set, which may include the encoded state of the rewritten state sample and the corresponding real reward label. During model training, the cross-entropy loss function can be used to calculate the error between the estimated reward and the real reward label, and the weights of each network layer of the first screening model can be updated through the gradient descent backpropagation algorithm until the training completion condition is met. The training completion condition may include model convergence or the number of training iterations reaching a training iteration threshold.

[0052] Before processing through the first screening model, the computer device can perform preprocessing and feature construction on each candidate rewrite state to generate standardized and consistent feature vectors as input to the first screening model. This eliminates the inconsistency of data dimensions and ensures that the first screening model can capture the nonlinear relationship between complex database states and optimization benefits, laying a data foundation for subsequent efficient inference.

[0053] For operator-level statistical features, the computer device can perform topological feature statistics on the query plan of the current candidate rewrite state to obtain the occurrence frequency of each type of operator node in the query plan, the depth of the query structure, and the number of operators in the query plan, thus constituting operator-level statistical features. For example, if the tree structure corresponding to the query plan is represented as p = {tree, op_1, op_2, ..., op_m}, where op represents an operator node, and a predefined set of operator types T = {t_1, t_2, ..., t_k} is defined, such as t_1 representing hash join, t_2 representing nested loop join, t_3 representing filtering, etc.; when performing topological feature statistics, for any type t_j, the occurrence frequency of operators of this type in p is count(t_j) = 1[type(op_1)=t_j] + 1[type(op_2)=t_j] + ... + 1[type(op_m)=t_j]. In addition, the tree depth is calculated to obtain the tree depth feature d = depth(tree), and the number of operators feature m. This constitutes the operator-level statistical features [count(t_1), count(t_2), ..., count(t_k), d, m].

[0054] For the rule availability frequency feature, the computer device can encode the set of available rewrite rules used in the candidate rewrite state, and construct a numerical encoding vector based on the rule usage frequency. For example, let the rule base R = {r_1, r_2, ..., r_n}. For each rewrite rule r_j, count the number of nodes that meet the applicable conditions in the current query plan: freq(r_j) = valid(op_1, r_j) + valid(op_2, r_j) + ... + valid(op_m, r_j). This forms the rule availability frequency feature [freq(r_1), freq(r_2), ..., freq(r_n)].

[0055] For the rule application trajectory features, the rewriting rules used from the parent rewriting state to the current candidate rewriting state are encoded. The parent rewriting state refers to the previous rewriting state that was relied upon when generating the current candidate rewriting state. In this embodiment, in order to avoid the model from erroneously capturing the numerical relationship between the numbers and ignoring the essence of discrete analogy features, one-hot encoding is adopted. According to used(r_j) = if (r_j = x) then 1 else 0, the rule application trajectory features [used(r_1), used(r_2), ..., used(r_n) ] are constructed.

[0056] For cost and cardinality features, there are cost estimation features and cardinality estimation features. The computer equipment can obtain the cost estimate and cardinality estimate of the query plan corresponding to each candidate rewrite state through the cost estimation and cardinality estimation functions built into the optimizer. The cost estimate is used to characterize the resource consumption of the query plan, and the cardinality estimate is used to characterize the number of data rows output by the query plan. Since the cost and cardinality values ​​have a large range and exhibit a long-tail distribution, the computer equipment can perform logarithmic normalization on the cost estimate and cardinality estimate to obtain the cost estimation feature ln(cost) and cardinality estimation feature ln(card), so as to be consistent with the subsequent division of the revenue interval.

[0057] After the above feature extraction, the coding features of each candidate rewrite state can be generated. Taking the coding features containing the above four types of features as an example, the coding features can be represented as [count(t_1), count(t_2), ..., count(t_k), d, m, freq(r_1), freq(r_2), ..., freq(r_n), used(r_1), used(r_2), ..., used(r_n), ln(cost), ln(card)].

[0058] By standardizing feature extraction for each candidate rewrite state and employing a heterogeneous data alignment strategy, the differences in data form are eliminated, and the complex database state is effectively converted into a fixed-length feature vector. This ensures that the first screening model can accurately capture the nonlinear relationship between state features and the profit range, thereby improving the accuracy of profit prediction.

[0059] After completing the preprocessing and feature construction for each candidate rewrite state, the computer device can input the encoded features of each candidate rewrite state into the first screening model to achieve revenue prediction for each candidate rewrite state. The encoded features can be a subset of features determined based on the four types of features mentioned above. For example, the encoded features can include the four types of features mentioned above, or the encoded features can include operator-level statistical features, rule availability frequency features, and rule application trajectory features, or the encoded features can include operator-level statistical features and cost cardinality features, or the encoded features can include any one of the four types of features, etc. Different feature subsets can be selected as feature encodings for revenue prediction based on actual needs, and this application embodiment does not impose any restrictions on this.

[0060] When the encoded features contain any two types of features, the first screening model can perform multi-branch deep fusion on the input features to achieve future profit prediction within a preset number of steps based on the obtained fused feature vector. Among them, for operator-level statistical features, rule availability frequency features, and rule application trajectory features, they can be mapped first and then feature fusion can be performed to map features from different semantic spaces to the same dimension, which is convenient for capturing cross-domain correlation information during subsequent feature fusion, thereby improving the accuracy and stability of profit prediction.

[0061] As an optional embodiment, taking the encoding features containing the above four types of features as an example, the first screening model is used to estimate the profit of each candidate rewriting state in the candidate set, and the profit prediction results of each candidate rewriting state are obtained, including: The operator-level statistical features, rule availability frequency features, and rule application trajectory features in the coding features are mapped using the first screening model to obtain the first feature vector, the second feature vector, and the third feature vector. The first feature vector, the second feature vector, the third feature vector, and the cost and cardinality features in the encoded features are fused to obtain the fused feature vector; Based on the fused feature vector, the profit distribution is mapped to obtain the probability distribution of the candidate rewrite state in each preset profit category; Based on the probability distribution, the estimated payoff range for candidate rewrite states is determined.

[0062] By independently mapping operator-level wanted features, rule availability frequency features, and rule application trajectory features, and then fusing them with cost and cardinality features to form a unified fused feature vector, we can fully explore the deep correlation between different types of features. When estimating revenue, we can simultaneously consider the structural statistics of the query plan, the historical usability of the rewriting rules, the contextual information of the rewriting path, and the cost cardinality of the current plan, thereby improving the accuracy and robustness of revenue estimation.

[0063] The first screening model can include three mapping branches, a feature fusion layer, and a classification output layer. The three mapping branches can respectively map high-dimensional features, rule availability frequency features, and rule application trajectory features from the operator-level statistical features to obtain feature vectors of the same dimension; that is, the first screening model includes: The first mapping branch is used to map the operator-level statistical features and output the first feature vector; The second mapping branch is used to map the frequency features of rule availability and output the second feature vector. The third mapping branch is used to apply trajectory features to the rules and output the third feature vector. The feature fusion layer is used to fuse the first feature vector, the second feature vector, the third feature vector, and the cost and cardinality features in the encoded features, and output the fused feature vector. The classification output layer is used to output the estimated profit range of candidate rewrite states based on the fused feature vector.

[0064] The operator-level statistical features consist of operator frequency features ([count(t_1), count(t_2), ..., count(t_k)]), tree depth features (d), and operator number features (m). These operator frequency features are high-dimensional. When mapping these operator-level statistical features, they can be input into the first mapping branch. After mapping the operator frequency features, the output is combined with the tree depth and operator number features. In this case, the first feature vector output by the first mapping branch includes the feature vector v_count after mapping the operator frequency features, the tree depth feature, and the operator number feature, represented as [v_count, d, m]. The feature vectors v_count, second feature vector, and third feature vector have the same dimension. During feature fusion, these first, second, and third feature vectors, along with the cost and cardinality features, are fused to obtain the fused feature vector.

[0065] In another possible implementation, when mapping operator-level statistical features, the operator occurrence frequency feature can be input separately into the first mapping branch for mapping processing. Then, in the feature fusion stage, it is fused with the tree depth feature and the operator number feature. In this case, the first feature vector output by the first mapping branch includes the feature vector v_count after mapping the operator occurrence frequency feature. When performing feature fusion, the first feature vector, the second feature vector, the third feature vector, the tree depth feature, the operator number feature, and the cost and cardinality feature are fused to obtain the fused feature vector.

[0066] Take, for example, inputting the frequency feature of the operator into the corresponding mapping branch for mapping processing. Figure 2 This application illustrates an exemplary embodiment of the architecture of a first screening model, as shown below. Figure 2 As shown, the first screening model may include a first mapping branch 210, a second mapping branch 220, and a third mapping branch 230.

[0067] The first mapping branch 210 is used to input the operator occurrence frequency features [count(t_1), count(t_2), ..., count(t_k)] from the operator-level statistical features to the first fully connected layer. Through linear transformation and nonlinear activation function, the k-dimensional input is mapped to the first feature vector v_count of the first dimension. The first feature vector represents the statistical information of the operator type.

[0068] The second mapping branch 220 is used to input the rule availability frequency features [freq(r_1), freq(r_2), ...,freq(r_n)] into the second fully connected layer. Through linear transformation and nonlinear activation function, the n-dimensional input is mapped to the second feature vector v_freq of the first dimension. This second feature vector represents the rewritten rule availability information.

[0069] The third mapping branch 230 is used to input the rule application trajectory features [used(r_1), used(r_2), ..., used(r_n)] into the third fully connected layer. Through linear transformation and nonlinear activation function, the n-dimensional input is mapped to the third feature vector v_used of the first dimension. This third feature vector represents the rewriting rule applied from the parent rewriting state to the current candidate rewriting state.

[0070] The feature vectors output by the three mapping branches have the same dimension. For example, the first dimension can be 32-dimensional, or it can be other values ​​set based on actual needs. This application does not impose any restrictions on this.

[0071] The first screening model may further include a feature fusion layer 240, used to fuse the first feature vector, the second feature vector, the third feature vector, the tree depth feature, the operator number feature, and the cost and cardinality feature. When performing feature fusion, the above feature vectors can be concatenated and then input into the fourth fully connected layer. Through linear transformation and nonlinear activation function, a fused feature vector of the second dimension is output. The second dimension can be 128-dimensional or other values ​​set based on actual needs. The second dimension is higher than the first dimension. This application embodiment does not limit the value of the second dimension.

[0072] Taking the first feature vector v_count, the second feature vector v_freq, and the third feature vector v_used as a 32-dimensional vector as an example, in the feature fusion stage, it is concatenated with the tree depth feature d, the number of operators feature m, and the cost and cardinality features [ln(cost), ln(card)] to form a 100-dimensional vector, which is then input into the fourth fully connected layer for feature fusion, and outputs a 128-dimensional fused feature vector v_fusion; through feature fusion, the cross-correlation information between different features can be captured.

[0073] The first screening model also includes a classification output layer 250, which is used to input the fused feature vector into the fifth fully connected layer. Through linear transformation, it outputs a logistic value vector (i.e., a logits vector) of a preset dimension. After normalization by the softmax function, the probability distribution of each preset benefit category is obtained. The dimension of the logistic value vector is consistent with the number of benefit categories. For example, if there are 6 preset benefit categories, a 6-dimensional logits vector is output through linear transformation. After normalization, the probability distribution of each of the 6 preset benefit categories is obtained. In order to reduce the sensitivity of the model to numerical fluctuations and considering the large dynamic range of SQL query costs, different benefit categories in this application correspond to different benefit intervals.

[0074] The first screening model provided in this application sets independent mapping branches for different types of features, enabling each branch to perform targeted feature extraction and dimensionality transformation for its respective input features. Mapping the output feature vectors to the same dimension allows the fusion layer to align and fuse multiple features in a unified semantic space, enhancing the comparability and interactivity between different features and thus improving the representational power of the fused feature vector. Based on this, the classification output layer maps the fused feature vector to a predicted profit interval, transforming the continuous value prediction problem into an interval classification problem, reducing the learning difficulty of the model and enhancing the stability and reliability of profit prediction. This model structure design enables the first screening model to efficiently and accurately predict the profit of a large number of candidate rewrite states, improving the model's profit prediction accuracy and generalization ability in complex query scenarios, and providing a reliable decision-making basis for subsequent pruning and screening based on profit prediction results.

[0075] Furthermore, since the cost of complex query plans can reach hundreds of thousands, and effective rewriting operations can typically bring orders of magnitude performance improvements, as a possible implementation approach, when dividing the revenue ranges for each revenue category, an exponentially increasing range division method can be adopted. That is, each revenue category corresponds to a different revenue range, and the range boundaries between adjacent revenue categories increase exponentially. For example, taking a preset of 6 revenue categories as an example, the revenue ranges corresponding to each revenue category can be [0, 10), [10, 100), [100, 1000), [1000, 10000), [10000, 10000), [100000, 100000), [100000, +∞).

[0076] The exponentially growing interval division method not only adapts to the long-tail distribution characteristics of cost data, but also distinguishes rewriting schemes with substantial optimization significance, thereby improving the stability of revenue prediction and providing more reliable search direction guidance for subsequent large language models.

[0077] After determining the probability distribution, the estimated revenue range of the candidate rewrite state can be determined based on the probability distribution. In one possible implementation, the maximum probability method can be used to determine the revenue range corresponding to the category with the highest probability as the estimated revenue range. Alternatively, the expected value method can be used to calculate the expected revenue value by weighting the product of the medians of each expected probability range, and the revenue range in which the expected revenue value falls can be determined as the estimated revenue range.

[0078] In terms of computational overhead, the total number of parameters for the first screening model is (k*h1+h1) + (n*h1+h1) + (n*h1+h1) + (d*h2+h2) + (h2*C+C), where h1 represents the output dimension of the three mapping branches, h2 represents the output dimension of the feature fusion layer, C represents the number of benefit categories, and d represents the dimension of the concatenated feature vector. Its parameter count is relatively small, classifying it as a lightweight model.

[0079] After obtaining the estimated revenue range of each candidate rewriting state, the computer device can filter the rewriting states based on the estimated revenue range of each candidate rewriting state to retain a certain number of optimal candidates as the input candidate set of the large language model. As one possible implementation, the computer device can sort each candidate rewriting state in descending order of the representative value of the estimated revenue range to obtain the sorting result. Here, the representative value can be the median of the interval or the left endpoint of the interval. The first preset number of candidate rewriting states with the highest ranking in the sorting result are selected to form the first candidate subset.

[0080] As another possible implementation, when sorting the candidate rewrite states, they can also be sorted according to a preset sorting key, where the estimated benefit can be one of the sorting keys. If there are multiple sorting keys, they can be prioritized so that during pruning and filtering, the candidate rewrite states are sorted and filtered sequentially using each sorting key to obtain the first candidate subset. In this case, pruning and filtering are performed based on the estimated benefit of each candidate rewrite state to obtain the first candidate subset, which includes: The candidate rewrite statuses are sorted and filtered in multiple levels according to the preset sorting keywords to obtain the first candidate subset; The sorting keywords include a first keyword and a second keyword, with the first keyword having a higher sorting priority than the second keyword. The first keyword is determined based on a comparison between the current query cost of the candidate rewrite state and the query cost of the corresponding parent rewrite state. The second keyword is determined based on the estimated revenue.

[0081] Different levels of sorting and filtering are based on different keywords. Since the current query cost directly reflects the quality of candidate rewrite states and, compared to the predicted benefits of the first filtering model, reflects the actual resource consumption, it can be used as the primary keyword for sorting. However, because there may be dependencies between rewrite rules, a rewrite rule that leads to negative optimization may be a prerequisite for another effective rewrite rule. To avoid prematurely eliminating intermediate states that currently produce negative optimization but can achieve better rewrite states through subsequent rewrites, the query cost of the parent rewrite state can be used as the primary keyword for further sorting and filtering at subsequent levels. Therefore, the primary keyword needs to be determined based on the comparison between the current query cost and the query cost of the corresponding parent rewrite state. If the current query cost is less than the query cost of the parent rewritten state, it means that a positive benefit has been generated, and the current query cost is used as the first key. If the current query cost is greater than or equal to the query cost of the parent rewritten state, then the query cost of the parent rewritten state is used as the primary key to ensure that negative optimization paths still have a chance to be explored.

[0082] After determining the primary keyword, when sorting and filtering based on the primary keyword, the search costs are sorted in ascending order. The candidate rewrite status with the lower cost is retained first according to the first-level sorting results, thus obtaining the first-level filtering results.

[0083] Based on the first-level filtering results, sorting and filtering are performed according to the second keyword. In one possible implementation, the second keyword can be a representative value of the estimated benefit range. In this case, when performing the second-level sorting and filtering, the second keyword can be sorted in descending order, and the candidate rewriting state with higher estimated benefit is retained first according to the second-level sorting result, thus obtaining the second-level filtering result. In another possible implementation, the second keyword can also be the estimated minimum cost calculated based on the current query cost and the representative value of the estimated benefit range. In this case, when performing the second-level sorting and filtering, the second keyword can be sorted in ascending order, and the candidate rewriting state with lower estimated minimum cost is retained first according to the second-level sorting result, thus obtaining the second-level filtering result. For example, if the estimated benefit range is range_i, and the left endpoint left(range_i) is taken as the representative value, the estimated minimum cost can be calculated as cost(c_i)-left(range_i). Since the query cost cannot be negative, the second keyword is modified to max(0, estimated minimum cost).

[0084] In one possible implementation, the computer device can determine the second-level filtering result obtained after two levels of sorting and filtering as the first candidate subset. In this case, the number of candidate rewrite states filtered out from the second-level filtering result is equal to a first preset number.

[0085] By setting multi-level ranking keywords to prune and filter candidate rewrite states, the first keyword prioritizes retaining candidate rewrite states that generate positive benefits to the parent state, ensuring that the search direction always converges towards a better state. Based on this, the second keyword prioritizes retaining states with greater potential for improvement, optimizing the internal ranking of candidate rewrite states within the same category. Through these two levels of ranking, a preset number of candidate rewrite states with higher rankings can be retained in each iteration to enter the next round. This effectively controls the size of candidate rewrite states while ensuring the quality of selection, allowing cluster search to focus on high-value rewrite paths within limited computational resources, avoiding search space expansion, and balancing rewrite quality and search efficiency.

[0086] In another possible implementation, considering that the candidate rewrite states in the candidate set are generated by enumerating them in rule number order, to avoid the sorting results being biased towards rewrite rules with smaller rule numbers, a third-level sorting and filtering can be performed by introducing a random number as a third key, based on the second-level filtering results. In this case, the sorting key also includes a third key, which has a lower sorting priority than the second key, and the third key is a random number. The number of candidate rewrite states filtered in the second-level filtering results is greater than a first preset number. Then, the computer device can generate a random number as a third key for each candidate rewrite state in the second-level filtering results. Afterwards, the states are sorted according to the size of the random numbers, thus shuffling the original order. Based on the sorting results, a first preset number of candidate rewrite states are selected from the second-level filtering results as the first candidate subset, to avoid systematic bias in the filtering results due to the rewrite rule number order.

[0087] For example, if the candidate set contains 59 candidate rewrite states, after the benefit prediction of the first screening model and the multi-level sorting screening, the subsequent estimated minimum cost is obtained as {82609.99: 12, 84750.89: 3, 104758.98: 32, 107540.34: 12}. If the first estimation quantity is 15, then based on the above estimated minimum cost, the optimal 15 results are retained, namely the 12 candidate rewrite states with an estimated minimum cost of 82609.99 and the 3 candidate rewrite states with a subsequent minimum cost of 84750.89. Thus, after pruning screening, 15 solutions are selected from the 59 candidate rewrite states, reducing the context length of the large language model to approximately one-quarter. Since the cost difference of the estimated minimum cost is within 10... 3-10 5 The order of magnitude range shows that the exponentially growing interval division strategy output by the first screening model can meet practical needs.

[0088] Through the above multi-level sorting and filtering, the candidate rewriting states can be reasonably sorted, ensuring immediate benefits while taking into account potential long-term optimization paths and eliminating enumeration order bias, thus obtaining the input candidate set of the large language model, namely the first candidate subset, reducing the number of candidates entering the large language model processing stage, thereby reducing lexical consumption.

[0089] After pruning and selection, the computer device can further utilize the semantic understanding and reasoning capabilities of the large language model to select the more valuable operational path from the high-potential candidate rewriting states after selection. Through the cascade architecture of the first selection model prediction and the semantic decision of the large language model, the efficiency and quality of the search space exploration are improved. This not only reduces lexical consumption and latency, but also overcomes the shortcomings of inaccurate numerical evaluation of the large language model, achieving better search convergence results.

[0090] When performing secondary screening using a large language model, the prompt words can be structured based on the natural language description of the rewriting rules and the query plan to adapt to the cluster search strategy. This involves organizing the query plans, rewriting rule descriptions, and expected return information corresponding to multiple candidate rewriting states into a unified prompt word context, enabling the large oracle model to simultaneously evaluate the relative merits of multiple candidate rewriting states in word inference requests, achieving batch decision-making. As an optional implementation, the candidate rewriting states in the first candidate subset are input into the large language model for secondary screening to obtain a second candidate subset containing the target rewriting state, including: Obtain the natural language description of the rewriting rules and the query plan corresponding to each candidate rewriting state in the first candidate subset; The natural language descriptions of the rewriting rules corresponding to each candidate rewriting state and the prompts generated by the query plan are input into the large language model for inference, and the second candidate subset of the output is obtained.

[0091] During the secondary screening, multiple candidate rewriting states from the first candidate subset are aggregated into a single batch inference request. The natural language descriptions of the rewriting rules involved in each candidate rewriting, the current query plan, and other information are encapsulated in a structured dictionary format and placed into the same prompt word context to support the parallel inference and decision-making of multiple candidate rewriting schemes by the large language model. By making decisions in batches based on the large language model aggregated by cluster states, the interaction latency is significantly reduced and the evaluation consistency is improved. The number of model interactions is reduced, and the score scale drift caused by multiple calls is avoided, achieving simultaneous optimization of throughput and search stability.

[0092] By inputting the natural language description of the rewritten rules into a large language model, we can first reduce cognitive load and contextual noise, avoiding the occupation of the context window by invalid information in the executable code and its interference with the optimization logic of the large model. For example, Java source code contains a large amount of framework-specific boilerplate code, type definitions, and low-level memory management logic, which are invalid information for high-level search strategy decisions. Second, natural language descriptions can explicitly express the mapping relationship between "rule-scenario-benefit". Combined with the characteristics of large language models, which are good at causal inference and heuristic judgment based on natural language but have difficulty directly inferring dynamic changes in execution costs from static code, natural language descriptions are more in line with the reasoning paradigm of large language models and can give full play to the advantages of large language models in semantic understanding and empirical judgment. Finally, natural language descriptions abstract specific implementation details, enabling large language models to make more flexible generalization decisions based on semantic features rather than code matching when faced with unfamiliar query structures, avoiding misjudgments caused by implementation differences between different database systems.

[0093] For example, the prompt could be worded as: "You are a Q&A assistant proficient in SQL rewriting and must select the optimal rewriting scheme in the prescribed format to improve the execution efficiency of SQL statements in PostgreSQL. Pay attention to evaluating the future benefits of the rewriting scheme, that is, the maximum performance improvement that the current rewritten query plan may bring in subsequent rewritings. In this case, PostgreSQL should be replaced with the actual database management system you are using."

[0094] The prompt word templates provided to users can be: SQL to be rewritten: [Enter the current SQL statement] Original query plan: [Enter the current query plan] Query cost: [Enter the cost value of the current SQL query] Database table information: [Enter the database CREATE TABLE statement, generated based on SCHEMA information] Rewrite rule description: [Enter a dictionary in the form of {"rule name": "natural language description of the rule"}, containing only the actual rewrite rules that are usable; the rule descriptions are from the Apache Calcite documentation.] Candidate rewrite status: [Enter the candidate rewrite status, in the format: {"id":"incrementing number","rule":"rule name","plan":"rewritten Calcite query plan"}, a list of dictionaries] Please note: You must select the optimal solution, and the number of solutions selected must be between 0 and [fill in the bundle size, default is 5], sorted from best to worst. Please output the numbers of the rewritten solutions, separated by spaces, without any other text.

[0095] [Template End] It should be noted that the above-mentioned prompt words and the prompt word templates provided to users are all exemplary. Different settings can be made based on different actual needs, and this application embodiment does not limit them.

[0096] Based on the API (Application Programming Interface), the above-mentioned aggregated prompt words are sent to the large language model to obtain the output content of the large language model. In one possible implementation, the computer device can obtain the complete output in a non-streaming manner and set a timeout mechanism, that is, set a call duration threshold. After the call to the large language model is completed, the output result of the large language model is parsed and the rewrite status number separated by spaces or specified delimiters is extracted.

[0097] In one possible implementation, the computer device can select the target rewriting state based on a comprehensive screening of the output of the large language model and the estimated benefits of each candidate rewriting state in the first candidate subset: If the output of the large language model is successfully parsed, the call duration does not exceed the call duration threshold, and the number of candidate rewrite states obtained by parsing meets the preset bundle width value, that is, the preset number of target rewrite states, then the candidate rewrite states output by the large language model are determined as the target rewrite states.

[0098] If parsing fails or the call duration exceeds the call duration threshold, the result will be set to an empty set, and the filling strategy based on the first screening model will be triggered. That is, from the first candidate subset, the top N candidate rewriting states with the highest expected revenue will be selected as the target rewriting states in descending order of the revenue prediction results of each candidate rewriting state. N is a positive integer representing the preset bundle width value.

[0099] If the number of candidate rewrite states selected by the large language model exceeds the bundle width value, the candidate rewrite state with the lowest benefit is deleted based on the machine learning prediction results until the bundle width value constraint is met.

[0100] If the number of candidate rewriting states selected by the large language model is insufficient for the bundle width value, then from the unselected candidate rewriting states in the first candidate subset, the candidate rewriting state with the highest profit is selected to fill the gap, referring to the profit prediction results of each candidate rewriting state, until the constraint of the bundle width value is met.

[0101] The following example illustrates a constructed prompt (showing only the natural language description of one of the applied rewriting rules and one candidate rewriting state): The original SQL statement is: select lineitem.l_shipinstruct, orders.o_orderpriority, customer.c_nationkey from customer join orders on orders.o_custkey=customer.c_custkeyjoin lineitem on lineitem.l_orderkey=orders.o_orderkey where customer.c_nationkey<= 24 order by orders.o_orderpriority asc, customer.c_nationkeydesc; The corresponding initial query plan is: LogicalSort(sort0=[$1], sort1=[$2], dir0=[ASC], dir1=[DESC]) LogicalProject(l_shipinstruct=[$30], o_orderpriority=[$13], c_nationkey=[$3]) LogicalFilter(condition=[<=($3, 24)]) LogicalJoin(condition=[=($8, $17)], joinType=[inner]) LogicalJoin(condition=[=($0, $9)], joinType=[inner]) JdbcTableScan(table=[[tpch100m, customer]]) JdbcTableScan(table=[[tpch100m, orders]]) JdbcTableScan(table=[[tpch100m, lineitem]]) The query cost of the original SQL statement is: 104758.98 The target database table information is as follows: CREATE TABLE CUSTOMER ( C_CUSTKEYSERIAL, C_NAMEVARCHAR(25), C_ADDRESSVARCHAR(40), C_NATIONKEYINTEGER NOT NULL, C_PHONECHAR(15), C_ACCTBALDECIMAL, C_MKTSEGMENTCHAR(10), C_COMMENTVARCHAR(117) ); CREATE TABLE NATION ( N_NATIONKEYSERIAL, N_NAMECHAR(25), N_REGIONKEYINTEGER NOT NULL, N_COMMENTVARCHAR(152) ); CREATE TABLE SUPPLIER ( S_SUPPKEYSERIAL, S_NAMECHAR(25), S_ADDRESSVARCHAR(40), S_NATIONKEYINTEGER NOT NULL, S_PHONECHAR(15), S_ACCTBALDECIMAL, S_COMMENTVARCHAR(101) ); The natural language description of the rewrite rule is: {'SORT_PROJECT_TRANSPOSE': "If a SQL query performs a sort operation on columns directly referenced in the SELECT list (or expressions of these columns after strictly monotonic transformations), and this sort operation immediately follows a projection operation (SELECT statement), then the sort operation can be pushed down, thus acting directly on the table (FROM clause) before the projection." ...} The candidate rewrite status is: [ { "id": 1, "rule": "SORT_PROJECT_TRANSPOSE", "plan": "LogicalSort(sort0=[$1], sort1=[$2], dir0=[ASC], dir1=[DESC]){LogicalProject(l_shipinstruct=[$4], o_orderpriority=[$2], c_nationkey=[$0]){LogicalJoin(condition=[=($1, $3)], joinType=[inner]){LogicalProject(c_nationkey=[$3], o_orderkey=[$8], o_orderpriority=[$13]){LogicalFilter(condition=[<=($3, 24)]){LogicalJoin(condition=[=($0, $9)], joinType=[inner],semiJoinDone=[true]){LogicalJoin(condition=[=($0, $9)], joinType=[semi]){JdbcTableScan(table=[[tpch100m, customer]]), JdbcTableScan(table=[[tpch100m, orders]])}JdbcTableScan(table=[[tpch100m, orders]])}}}LogicalProject(l_orderkey=[$0], l_shipinstruct=[$13]){JdbcTableScan(table=[[tpch100m,lineitem]])}}}}" }, ... ] Please note: You must select the optimal solution, and the number of selections must be between 0 and 5, ordered from best to worst. Please output the numbers of the rewritten solutions, separated by spaces, without any additional text.

[0102] [End of prompt] If the large language model outputs the following result based on the above prompt words: 1 2 3 4 5, it indicates that the selected target rewriting states are numbered 1, 2, 3, 4, and 5 in order of superiority to inferiority, forming the second candidate subset.

[0103] In this embodiment, the large language model makes semantic decisions based on the natural language description of the rewriting rules and outputs the number of the target rewriting state instead of directly generating SQL code. Therefore, in one possible implementation, a dual-modal decoupling mechanism can be used to rewrite the SQL statement. For example, after the large language model outputs the number of the selected target rewriting state, the executor is called to input the new query plan recorded in the selected target rewriting state into the converter according to the number and reverse convert it into the corresponding SQL statement. For example, based on the given target database, the RelToSqlConverter component of Apache Calcite is used to convert the RelNode tree (i.e., the new query plan) after applying the rewriting rules into an SQL string. Alternatively, to ensure physical isolation between the decision-making process and the execution process, the executor can also call the executable code of the corresponding rewriting rule in the rule base again to perform the rewriting according to the number of the selected target rewriting state, obtain the new query plan, and then perform the conversion.

[0104] By decoupling the natural language description of the rewriting rules from the representation of the executable code, the model's semantic decision-making and the deterministic execution of the code are physically isolated at the process level. This ensures the determinism and reproducibility of the rewriting results from the architectural level, achieving a synergistic improvement in flexibility and reliability.

[0105] Furthermore, after generating candidate rewritten SQL statements by reverse transformation of the new query plan based on the target rewritten state, the corresponding query cost can be obtained by sending a cost query request to the target database. For example, if the following candidate rewritten SQL statements are generated based on the new query plan with target rewritten state number 1: SELECT "lineitem"."l_shipinstruct", "t0"."o_orderpriority", "t0"."c_nationkey" FROM (SELECT * FROM (SELECT * FROM "customer" WHERE EXISTS (SELECT 1 FROM "orders" WHERE "customer"."c_custkey" = "orders"."o_custkey")) AS "t" CROSS JOIN "orders" AS "orders0" WHERE "t"."c_custkey" = "orders0"."o_custkey") AS "t0" INNER JOIN "lineitem" ON "t0"."o_orderkey" = "lineitem"."l_orderkey" WHERE "t0"."c_nationkey"<= 24 ORDER BY "t0"."o_orderpriority", "t0"."c_nationkey" DESC Sending a cost query request (EXPLAIN FORMAT=JSON request) corresponding to the above candidate rewritten SQL statement to the database yields a query cost of 82609.99, which is less than the query cost of the original SQL statement (104758.98).

[0106] After completing the reverse transformation of each new query plan and the evaluation of query costs, the generated candidate rewritten SQL statements and their corresponding query costs are added to the historical candidate set. The rewriting termination condition is then checked. If the condition is met, the candidate rewritten SQL statement with the lowest query cost in the historical candidate set is taken as the final rewriting result. If the condition is not met, the new query plans corresponding to each candidate rewritten SQL statement generated in the current iteration round are taken as the rewriting state of the cluster search state set in the next iteration round, and the statement rewriting is carried out in the next round.

[0107] Figure 3 This illustration shows a schematic diagram of an SQL statement rewriting system provided in an exemplary embodiment of this application. This SQL statement rewriting system can be applied to a computer device to execute all or part of the steps of the various embodiments described above, such as... Figure 3 As shown, the SQL statement rewriting system may include a candidate rewriting generation module 310, a rewriting rule base 320, a first screening model 330, a large language model decision maker 340, an executor 350, and a validator 360.

[0108] The candidate rewriting generation module 310 is used to receive the original SQL statement input by the user, extract the schema information from the corresponding target database through JDBC (Java Database Connectivity), parse the SQL statement into a query plan based on the Apache Calcite (open source dynamic data management framework) library, obtain rewriting rule information from the rewriting rule library, enumerate query plan operators and rewriting rules, generate a candidate set, and output the first screening model. The rewriting rule library 320 contains the executable code and natural language description of the rewriting rules.

[0109] The first screening model 330 is used to query the execution plan operator information, available rewriting rules, and selected rewriting rules of each candidate rewriting state in the input candidate set, output the interval estimate of the maximum future benefit, sort by future benefit, and select a fixed number of the most promising results as candidates for the large language model decision-maker, i.e., the first candidate subset.

[0110] The large language model decision maker 340 receives the first candidate subset, retrieves the natural language descriptions of the corresponding rewriting rules from the rewriting rule base, and calls the large language model via the OpenAI API to perform batch decision-making for the rewriting state. The output results are multiple optimal rewriting schemes output by the corresponding bundle search strategy. The new query plan in the target rewriting state obtained from the decision is output to the executor 350. After receiving the new query plan for the target rewriting state, the executor 350 performs a reverse transformation of the execution SQL and inputs the output candidate rewriting SQL statements into the verifier 360. The verifier 360 is used to send cost query requests corresponding to each candidate rewriting SQL statement to the target database, and receive the query costs of each candidate rewriting SQL statement returned by the target database. Then, the candidate rewriting SQL statements and their corresponding query costs are recorded in the historical candidate set.

[0111] If the rewriting termination condition is met, the optimal SQL statement with the lowest query cost is determined from the historical candidate set and output as the rewriting result. If the rewriting termination condition is not met, the statement rewriting process of the next iteration is based on the new query plan corresponding to the candidate rewritten SQL statement obtained in this iteration.

[0112] In addition, the candidate rewritten SQL statements generated in the current round can also serve as the basis for dynamic iterative optimization training of the first screening model. That is, a new "rewritten state feature - real benefit label" enhanced sample library is generated based on the selected candidate rewritten SQL statements and persisted. When the enhanced training conditions for the first screening model are triggered, the first screening model is iteratively optimized and trained based on the new enhanced sample library to continuously improve the benefit prediction accuracy of the first screening model.

[0113] Based on such Figure 3 The SQL statement rewriting system shown is... Figure 4 A flowchart of an SQL statement rewriting method provided in another exemplary embodiment of this application is shown, such as... Figure 4 As shown, the process may include the following steps.

[0114] S401, the user enters the original SQL statement.

[0115] S402, the candidate rewrite generation module obtains the schema information of the target database.

[0116] S403 parses the original SQL statement based on schema information and generates an initial query plan.

[0117] Using the initial query plan as the initial rewrite state of the cluster search set, the statement rewrite process is executed iteratively.

[0118] The statement rewriting process is as follows: S404 performs rewrite rule matching for each rewrite state.

[0119] S405, determine whether a new candidate rewrite state has been generated. If so, execute S406; otherwise, end the iteration and execute S411.

[0120] S406, the first screening model performs pruning screening to obtain the first candidate subset.

[0121] S407, the large language model makes a rewriting decision on the first candidate subset and obtains the number of the target candidate rewriting state.

[0122] S408, the executor performs rewriting based on the target rewriting status number, and obtains candidate rewriting SQL statements.

[0123] S409, the validator verifies the query cost of each candidate SQL statement rewrite and records it in the historical candidate set.

[0124] S410: Determine whether the preset maximum number of iterations has been reached. If yes, execute S411. Otherwise, after updating the rewrite status in the cluster search status set to the new query plan corresponding to the candidate rewrite SQL statement, return to S404.

[0125] S411 outputs the rewritten SQL statement with the lowest query cost from the historical candidate set as the rewritten result.

[0126] The details of each of the above steps can be found in the descriptions of the various embodiments of this application, and will not be repeated here.

[0127] The rewritten SQL statement can be input into the target database to query relevant content and the query results will be returned to the user. Querying information based on the optimized SQL statement can improve query efficiency and the accuracy of the query results.

[0128] In summary, the SQL statement rewriting method provided in this application improves the efficiency and quantity of search space exploration through a cascaded architecture of first-selection model pruning and large language model semantic decision-making, reduces token size and latency, overcomes the inaccuracy of numerical evaluation in large language models, and achieves better search convergence under limited resources. In addition, the adoption of a semantic and execution dual-modal rewriting rule representation method ensures the logical equivalence and security of SQL statement rewriting, retains the semantic generalization ability of large language models, and guarantees the determinism of rewriting results from the architectural level by physically isolating the decision-making and execution links, achieving a synergistic improvement in flexibility and reliability. Finally, the large model batch parallel decision-making based on bundled state aggregation reduces interaction latency and improves evaluation consistency, reduces the number of model interactions, and avoids score scale drift caused by multiple calls, achieving simultaneous optimization of system throughput and search stability.

[0129] Figure 5 A structural block diagram of a computer device 500 illustrated in an exemplary embodiment of this application is shown. This computer device can be implemented as a server as described above in this application. The computer device 500 includes a central processing unit 501, a system memory 504 including random access memory (RAM) 502 and read-only memory (ROM) 503, and a system bus 505 connecting the system memory 504 and the central processing unit 501. The computer device 500 also includes a mass storage device 506 for storing an operating system 509, application programs 510, and other program modules 511.

[0130] Without loss of generality, the computer-readable medium may include computer storage media and communication media. Computer storage media include volatile and non-volatile, removable and non-removable media implemented using any method or technology for storing information such as computer-readable instructions, data structures, program modules, or other data. Computer storage media include RAM, ROM, erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other solid-state storage technologies, CD-ROM, digital versatile disc (DVD) or other optical storage, magnetic tape cassettes, magnetic tape, disk storage, or other magnetic storage devices. Of course, those skilled in the art will recognize that the computer storage media are not limited to the above-mentioned types. The system memory 504 and mass storage device 506 described above can be collectively referred to as memory.

[0131] According to various embodiments of this application, the computer device 500 can also be connected to a remote computer on a network, such as the Internet. That is, the computer device 500 can be connected to a network 508 via a network interface unit 507 connected to the system bus 505, or the network interface unit 507 can be used to connect to other types of networks or remote computer systems (not shown).

[0132] The memory also includes at least one instruction, at least one program, code set, or instruction set, which are stored in the memory. The central processing unit 501 executes the at least one instruction, at least one program, code set, or instruction set to implement all or part of the steps in the SQL statement rewriting method shown in the above embodiments.

[0133] Figure 6 A structural block diagram of a computer device 600 illustrating another exemplary embodiment of this application is shown. This computer device 600 can be implemented as the aforementioned terminal device, such as a smartphone, tablet computer, laptop computer, desktop computer, etc. The computer device 600 may also be referred to as user equipment, portable terminal, laptop terminal, desktop terminal, or other names.

[0134] Typically, computer device 600 includes a processor 601 and a memory 602.

[0135] In some embodiments, the computer device 600 may optionally include a peripheral device interface 603 and at least one peripheral device. The processor 601, memory 602, and peripheral device interface 603 can be connected via a bus or signal line. Each peripheral device can be connected to the peripheral device interface 603 via a bus, signal line, or circuit board. Specifically, the peripheral device includes at least one of the following: a radio frequency circuit 604, a display screen 605, a camera assembly 606, an audio circuit 607, and a power supply 608.

[0136] In some embodiments, the computer device 600 further includes one or more sensors 609. The one or more sensors 609 include, but are not limited to, an accelerometer 610, a gyroscope 611, a pressure sensor 612, an optical sensor 613, and a proximity sensor 614.

[0137] Those skilled in the art will understand that Figure 6 The structure shown does not constitute a limitation on the computer device 600, and may include more or fewer components than shown, or combine certain components, or use different component arrangements.

[0138] In one exemplary embodiment, a computer-readable storage medium is also provided, which stores at least one computer program, the at least one computer program being loaded and executed by a processor to perform the following: Figure 1 or Figure 4 All or part of the steps in the illustrated embodiments.

[0139] In one exemplary embodiment, a computer program product is also provided, comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions that, when executed by a computer, cause the computer to perform actions such as... Figure 1 or Figure 4 All or part of the steps in the illustrated embodiments.

[0140] It should be understood that the training and prediction processes of the AI ​​models involved in the various embodiments of this specification all adhere to multiple legal and compliant principles, including legal data sources, compliant data content, compliant data governance, compliant training objectives and schemes, compliant training processes, compliant training environments and tools, and compliant ethical verification of training results, and comply with the requirements of Article 5 of the Patent Law. Among them: Data source legitimacy: All datasets used for AI model training were obtained through legal means, covering three categories: publicly authorized data, data authorized by partners, and self-collected compliant data. Publicly authorized data originates from compliant data sources that comply with relevant open-source agreements, with complete copyright attribution and authorization scope clearly marked, and no unauthorized open-source code or data reuse. Data authorized by partners has been subject to formal data usage agreements, clearly defining the scope, duration, and confidentiality obligations, and possessing a complete authorization chain. For self-collected data involving personal information, strict informed consent procedures have been followed, and anonymization processes (including but not limited to field masking, feature anonymization, and differential privacy technology) have been implemented to remove personally identifiable information, fully complying with relevant laws and regulations.

[0141] Data content compliance: The AI ​​model's dataset undergoes multiple screenings and cleaning processes to remove all non-compliant information and ensures that there is no illegal acquisition or use of genetic resources. For data in sensitive fields (such as healthcare and finance), an additional privacy-preserving computation module (including federated learning and secure multi-party computation technologies) is used to ensure that the data is "usable but not visible," avoiding compliance risks during the original data transmission process and ensuring that the data application scenarios and uses comply with public order and good morals and industry regulatory requirements.

[0142] Data governance norms: A complete data traceability system is established during the AI ​​model training process to automatically record the source, collection time, annotation process, cleaning rules, and permission allocation of training data, generating traceable compliance reports to ensure that the data is verifiable throughout its entire lifecycle. The dataset annotation process for AI models is completed by a professional human R&D team, clearly defining the proportion of human creative contributions and avoiding reliance on AI-generated data that has not undergone substantial human modification, thus meeting the examination requirements for "human main contributions" in AI patent applications.

[0143] Training objectives and plans are compliant: The AI ​​model training objective focuses on rewriting state selection. The training scheme and final output results do not violate any mandatory provisions of laws and administrative regulations, do not harm the public interest or the legitimate rights and interests of others, and do not pose any potential risks of being used for illegal activities, infringing on privacy, or disrupting public safety. It strictly adheres to the ethical principle of "intelligent for good".

[0144] Training process compliance: A closed-loop training framework is adopted to ensure compliance and controllability of the training process. The specific process is as follows: First, training samples are obtained through compliant data sources. After the aforementioned data cleaning and desensitization, they are input into the neural network model to generate preliminary training results. Second, an expert system is introduced to verify the preliminary results. Based on preset rules and human expert experience, the feasibility of the results is evaluated, and outputs that may pose ethical risks or compliance hazards are corrected (such as removing decision-making logic that violates public order and good morals, and adjusting model parameters that do not comply with safety regulations). Finally, the loss function weights are dynamically optimized based on expert system feedback to strengthen the model's learning of compliant results, avoid overfitting errors or non-compliant labels, and form a closed-loop control of "data input - model training - expert verification - parameter optimization - result feedback" to ensure that the entire training process complies with the ethical review requirements of relevant laws.

[0145] Training environment and tool compliance: AI model training is implemented using nationally licensed chips and a compliant training platform. All open-source frameworks and components used in the training process have obtained their corresponding licenses, and copyright statements and patent citation information are fully retained, with no instances of infringement or reuse. The training environment is built using virtual devices (containers / virtual machines) with fixed random seeds and initial parameter configurations to ensure the reproducibility of the training process. Furthermore, through access control and operation log recording, risks such as data leakage and parameter tampering during training are prevented, ensuring the security and compliance of the training process.

[0146] Training results ethical verification compliance: After the model is trained, it undergoes additional third-party ethical compliance assessment and algorithm filing review to verify that the model output does not violate social morality or harm public interests. For potentially sensitive scenarios, a dedicated result verification mechanism is established to ensure that the model always complies with relevant laws and regulations in practical applications.

[0147] In summary, the data and training process used in the AI ​​model in this specification strictly comply with relevant regulations and do not violate any laws, social ethics, public interests, or regulations on the use of genetic resources. Therefore, it fully meets the compliance requirements for patent authorization.

[0148] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the claims.

[0149] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.

Claims

1. A method for rewriting SQL statements, characterized in that, The method includes: The original SQL statement is parsed to generate an initial query plan; Using the initial query plan as the initial rewrite state of the cluster search state set, the statement rewrite process is iteratively executed until the rewrite termination condition is met; From the historical candidate set of all candidate rewritten SQL statements that record the entire rewriting process, select the candidate rewritten SQL statement with the lowest query cost as the rewritten SQL statement of the original SQL statement; The statement rewriting process includes: For each rewrite state in the current bundle search state set, apply the matching rewrite rule to generate at least one candidate rewrite state, and aggregate them to obtain a candidate set; the candidate rewrite states include the new query plan generated by rewriting; the new query plan is obtained by rewriting the operator nodes that match the rewrite rule in the query plan contained in the rewrite state; The target rewrite state is selected from the candidate set through at least two screenings, wherein the first screening is a pruning screening based on the estimated revenue of the new query plan corresponding to the candidate rewrite state, and the second screening is a further screening of the first screening result using a large language model. The new query plan corresponding to the target rewrite state is reverse-converted to generate the candidate rewrite SQL statement; The candidate rewritten SQL statement is added to the historical candidate set, and the new query plan corresponding to the candidate rewritten SQL statement is used as the rewritten state of the cluster search state set in the next iteration round.

2. The method according to claim 1, characterized in that, The step of selecting the target rewrite state from the candidate set through at least two screenings includes: The first screening model is used to estimate the revenue of each candidate rewrite state in the candidate set, and the revenue estimation results of each candidate rewrite state are obtained. The first screening model is used to estimate the revenue based on the coding features of the new query plan corresponding to the candidate rewrite state. The coding features include at least one of the following: operator-level statistical features, rule availability frequency features, rule application trajectory features, and cost and cardinality features. The operator-level statistical features are used to characterize the number of times each type of operator appears in the query plan. The rule availability frequency features are used to characterize the number of operator nodes applicable to each rewrite rule. The rule application trajectory features are used to characterize the rewrite rules applied from the parent rewrite state to the current rewrite state. The cost and cardinality features are used to characterize the estimated query cost and estimated number of output rows of the query plan corresponding to the candidate rewrite state. Based on the estimated revenue results of each candidate rewrite state, a pruning and filtering process is performed to obtain the first candidate subset; The candidate rewriting states in the first candidate subset are input into the large language model for secondary filtering to obtain a second candidate subset that includes the target rewriting state.

3. The method according to claim 2, characterized in that, The step of estimating the revenue of each candidate rewriting state in the candidate set using a first screening model to obtain the revenue estimation result for each candidate rewriting state includes: The operator-level statistical features, rule availability frequency features, and rule application trajectory features in the coding features are mapped using the first screening model to obtain a first feature vector, a second feature vector, and a third feature vector. The first feature vector, the second feature vector, the third feature vector, and the cost and cardinality features in the encoded features are fused to obtain a fused feature vector; Based on the fused feature vector, a profit distribution mapping is performed to obtain the probability distribution of the candidate rewrite state in each preset profit category; Based on the probability distribution, the estimated profit range of the candidate rewrite state is determined.

4. The method according to claim 3, characterized in that, The first screening model includes: The first mapping branch is used to map the operator-level statistical features and output the first feature vector. The second mapping branch is used to map the rule availability frequency feature and output the second feature vector. The third mapping branch is used to apply trajectory features to the rule for mapping processing and output the third feature vector. The feature fusion layer is used to fuse the first feature vector, the second feature vector, the third feature vector, and the cost and cardinality features in the encoded features, and output the fused feature vector. The classification output layer is used to output the estimated profit range of candidate rewrite states based on the fused feature vector.

5. The method according to claim 3 or 4, characterized in that, Each income category corresponds to a different income range, and the boundaries between adjacent income categories increase exponentially.

6. The method according to claim 2, characterized in that, The pruning and filtering based on the estimated returns of each candidate rewrite state yields a first candidate subset, including: The candidate rewrite statuses are sorted and filtered in multiple levels according to preset sorting keywords to obtain the first candidate subset; The sorting keywords include a first keyword and a second keyword, with the first keyword having a higher sorting priority than the second keyword. The first keyword is determined based on a comparison between the current query cost of the candidate rewrite state and the query cost of the corresponding parent rewrite state. The second keyword is determined based on the estimated revenue.

7. The method according to claim 6, characterized in that, The sorting key also includes a third key, the sorting priority of which is lower than that of the second key, and the third key is a random number.

8. The method according to claim 1, characterized in that, For each rewrite state in the current bundle search state set, applying the matching rewrite rules to generate at least one candidate rewrite state includes: For each rewrite state in the current bundle search state set, the pre-built rewrite rule library is traversed, and the rewrite rules are matched for each operator node in the query plan to obtain the successfully matched rewrite rules and the corresponding target operator nodes; the rewrite rule library contains the executable code and natural language description of each rewrite rule. Apply the executable code of the successfully matched rewriting rule to the target operator node to generate a new query plan; If the new query plan is appearing for the first time, a candidate rewrite status for the new query plan is generated.

9. The method according to claim 2, characterized in that, The step of inputting the candidate rewriting states from the first candidate subset into a large language model for secondary filtering to obtain a second candidate subset containing the target rewriting state includes: Obtain the natural language description of the rewriting rules and the query plan corresponding to each candidate rewriting state in the first candidate subset; The natural language descriptions of the rewriting rules corresponding to each candidate rewriting state and the prompts generated by the query plan are input into the large language model for inference, and the second candidate subset is output.

10. A computer device, characterized in that, The computer device includes a processor and a memory, the memory storing at least one computer program, which is loaded and executed by the processor to implement the SQL statement rewriting method as described in any one of claims 1 to 9.

11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one computer program, which is loaded and executed by a processor to implement the SQL statement rewriting method as described in any one of claims 1 to 9.

12. A computer program product, characterized in that, The computer program product includes a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions that, when executed by a computer, cause the computer to perform the SQL statement rewriting method as described in any one of claims 1 to 9.