Database query routing optimization method, apparatus and computer-readable storage medium
By constructing a routing state vector and routing policy network, dynamically adjusting the expert activation distribution, and optimizing database query routing, the problem of balancing accuracy and real-time performance in complex database query scenarios in existing technologies is solved, thereby improving the accuracy and real-time performance of query results.
Patent Information
- Application Number
- CN202611290159.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-24
- Publication Date
- 2026-11-17
AI Technical Summary
Existing technologies struggle to balance query accuracy, robustness, and real-time performance in complex database query scenarios. They lack routing, online adaptive, or execution feedback mechanisms, resulting in poor query optimization.
By constructing a routing state vector, utilizing a routing policy network to determine the activation distribution and target experts, and combining an execution feedback memory and hierarchical utility evaluation, the routing policy network is optimized to improve the accuracy and real-time performance of query results.
It improves accuracy and real-time performance in complex database query scenarios by dynamically adjusting expert activation distribution and optimizing routing strategies online, thereby enhancing the quality and efficiency of query generation.
Smart Images

Figure CN123115380A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of data processing technology, and in particular relates to a database query routing optimization method, apparatus and computer-readable storage medium. Background Technology
[0002] In existing technologies, natural language queries can be converted into structured database query statements through large models, which has become an important direction for database intelligence. However, in large-scale application scenarios, query requirements often involve multiple table joins, cross-domain schema migration, adaptation to different database dialects, and complex aggregation operations and constraint handling, making it difficult to effectively balance query accuracy, response latency, and system maintenance costs.
[0003] To improve the quality of complex query generation, query optimization can be achieved through methods such as injecting single-model prompts and historical example retrieval enhancements, multi-stage joins and offline feedback optimization, and hybrid expert reasoning with static routing.
[0004] However, existing query optimization methods still suffer from a lack of routing offloading, non-adaptive routing, or a lack of structured data processing of execution feedback for reuse in subsequent decision-making mechanisms. This makes it difficult to balance accuracy, robustness, and real-time performance in complex database query scenarios. Summary of the Invention
[0005] This application provides a database query routing optimization method, apparatus, and computer-readable storage medium, which can optimize the activation distribution in the routing policy network based on the execution results, thereby optimizing database query routing and improving the accuracy, robustness, and real-time performance of the generated query results.
[0006] In a first aspect, embodiments of this application provide a database query routing optimization method, the method comprising: Based on the natural language query text, the pattern structure information of the database, and the target historical entries in the execution feedback memory, a routing state vector is constructed. The target historical entries are historical entries in the execution feedback memory that have a matching degree with the natural language query text that exceeds a preset matching degree. The historical entries represent the mapping information between the routing decisions and execution results of the historical database queries. Based on the routing state vector, the activation distribution of each expert to be activated is determined through the routing policy network, and the target expert is determined from the experts to be activated based on the activation distribution and the number of dynamic experts. Based on the target expert's processing of the routing state vector, a query plan is obtained, and the query plan is executed on the database to obtain the execution result; The execution results are evaluated hierarchically according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution results; The expected state value and expected action value corresponding to the routing state vector are determined by the utility evaluation network. The routing policy network is iteratively optimized based on the expected state value, expected action value, and execution utility value to obtain an updated routing policy network, and the activation distribution is adjusted based on the updated routing policy network.
[0007] Secondly, embodiments of this application provide a database query routing optimization apparatus, the apparatus comprising: The construction module is used to construct a routing state vector based on the natural language query text, the pattern structure information of the database, and the target historical entries in the execution feedback memory. The target historical entries are historical entries in the execution feedback memory that have a matching degree with the natural language query text that exceeds a preset matching degree. The historical entries represent the mapping information between the routing decisions and execution results of the historical database queries. The activation module is used to determine the activation distribution of each expert to be activated through the routing policy network based on the routing state vector, and to determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. The query module is used to process the routing state vector based on the target expert, obtain a query plan, execute the query plan on the database, and obtain the execution result; The evaluation module is used to perform hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method, and obtain the execution utility value of the routing policy network on the execution results; The expectation module is used to determine the expected state value and expected action value corresponding to the routing state vector through the utility evaluation network; The update module is used to iteratively optimize the routing policy network based on the expected state value, expected action value, and execution utility value to obtain the updated routing policy network, and then adjust the activation distribution based on the updated routing policy network.
[0008] Thirdly, embodiments of this application provide a terminal device, the device including: a processor, and a memory storing computer program instructions; the processor reads and executes the computer program instructions to implement the database query routing optimization method as described in the first aspect.
[0009] Fourthly, embodiments of this application provide a computer-readable storage medium storing computer program instructions, which, when executed by a processor, implement the database query routing optimization method as described in the first aspect.
[0010] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the database query routing optimization method as described in the first aspect.
[0011] The database query routing optimization method, apparatus, and computer-readable storage medium of this application construct a routing state vector based on natural language query text, database schema structure information, and target historical entries in the execution feedback memory. The target historical entries are historical entries in the execution feedback memory whose matching degree with the natural language query text exceeds a preset matching degree. These historical entries represent the mapping information between routing decisions and execution results of historical database queries, thus enabling them to serve as prior experience for generating execution results. They can be directly retrieved from the routing state vector. Based on the routing state vector, the activation distribution of each expert to be activated is determined through a routing policy network. Based on the activation distribution and the number of dynamic experts, a target expert is determined from the experts to be activated, thereby selecting a suitable expert to process the current route. The target expert of the state vector processes the routing state vector to obtain query activation, executes the query plan on the database, and obtains the execution result. The execution result is evaluated hierarchically according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution result. This allows for the quantification of the routing policy network's routing strategy, enabling targeted optimization of the routing strategy. The expected state value and expected action value corresponding to the routing state vector are determined through the utility evaluation network. The routing policy network is iteratively optimized based on the expected state value, expected action value, and execution utility to obtain an updated routing policy network. The activation distribution is adjusted based on the updated routing policy network, thereby improving the accuracy, robustness, and practicality of the subsequent execution results determined by the routing policy network. Attached Figure Description
[0012] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0013] Figure 1 This is a flowchart illustrating a database query routing optimization method provided in an embodiment of this application; Figure 2 This is a schematic diagram of the structure of a multilayer sensor provided in an embodiment of this application; Figure 3 This is a schematic diagram of a positive feedback path in a routing policy network provided in an embodiment of this application; Figure 4 This is a schematic diagram of a database query routing optimization device provided in an embodiment of this application; Figure 5 This is a schematic diagram of the structure of a terminal device provided in an embodiment of this application. Detailed Implementation
[0014] The features and exemplary embodiments of various aspects of this application will be described in detail below. To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are only intended to explain this application and not to limit it. For those skilled in the art, this application can be implemented without some of these specific details. The following description of the embodiments is merely to provide a better understanding of this application by illustrating examples.
[0015] It should be noted that, in this document, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising..." does not exclude the presence of additional identical elements in the process, method, article, or apparatus that includes said element.
[0016] It should be noted that the acquisition, storage, use, and processing of data in this application embodiment all comply with the relevant provisions of laws and regulations.
[0017] It should be noted that in the embodiments of this application, certain software, components, models and other existing solutions in the industry may be mentioned. These should be regarded as exemplary and are only intended to illustrate the feasibility of implementing the technical solution of this application. However, it does not mean that the applicant has used or necessarily used the solution.
[0018] In existing technologies, to improve the generation quality of complex queries in intelligent databases, queries can be performed in the following ways: The approach of injecting single-model prompts and historical example retrieval enhancement (RAG) can inject database schema (tables, columns, foreign keys, etc.) and a small number of historical SQL examples into the context of a large model, guiding the large model to directly generate SQL. However, this approach lacks routing and distribution mechanisms for different query types. A single model needs to handle all query forms, leading to uncontrollable inference resource overhead. Furthermore, each inference operation follows a single-generation pattern, making it difficult to transform feedback such as SQL execution errors, execution time, and returned rows into reusable experience, thus making it easy for similar errors to recur. There is also the issue of schema understanding remaining at the textual level, insufficiently utilizing structural information such as complex JOIN paths and constraint relationships, leading to errors in table / column selection or join paths in complex multi-table scenarios. When the schema data volume is large, schema injection causes context bloat, increasing inference latency and cost, and is unfriendly to real-time query scenarios.
[0019] Multi-stage connection and offline feedback optimization, i.e., multi-stage pipeline (schema linking / table selection, etc.) and offline feedback optimization, can break down Natural Language to Structured Query Language (NL2SQL) into multiple subtasks, generate intermediate results segment by segment using rules or dedicated models, and then synthesize the SQL for execution, relying on offline annotation and retraining for optimization. However, this method suffers from significant chain error accumulation during the query process. Errors in earlier stages (such as table selection bias) can propagate and amplify to subsequent assembly stages, leading to overall failure. The optimization cycle typically relies on periodic offline loops for offline data production, evaluation, and retraining. The time lag in feedback to the system makes it difficult to quickly correct current high-frequency problems through online execution feedback. Consequently, adapting to new business databases or schema changes often requires reconfiguring rules / stage models, resulting in high maintenance costs and low iteration efficiency.
[0020] Static routing hybrid expert (MoE) inference can improve inference efficiency through a hybrid expert structure. In this approach, routing strategies are typically determined before deployment via rules or offline training, and a fixed combination of experts is selected based on query table characteristics during runtime. However, the routing strategy and expert combination are relatively static, making it difficult to automatically adjust to changes in database schema or business query distribution. Furthermore, it lacks a mechanism to update routing decisions based on SQL execution results (success / failure, error type, execution time, etc.), leading to the potential continuous triggering of inefficient or erroneous expert combinations. The lack of reusable execution feedback memory also results in lengthy trial-and-error periods during cross-database migrations or new database deployments, making it difficult to achieve stable, high-quality performance within a short period.
[0021] In summary, existing technologies generally exhibit characteristics such as static routing strategies and offline or fragmented feedback utilization. That is, they either lack routing distribution, or the routing cannot be adaptive online, or they lack a mechanism to structure and reuse execution feedback for subsequent decisions. This makes it difficult to simultaneously meet the requirements of accuracy, robustness, and real-time performance in complex enterprise-level NL2SQL scenarios.
[0022] To address the problems in the prior art, embodiments of this application provide a database query routing optimization method, apparatus, and computer-readable storage medium.
[0023] The database query routing optimization method provided in the embodiments of this application will be introduced first below.
[0024] Figure 1 A schematic flowchart of a database query routing optimization method provided in one embodiment of this application is shown. Figure 1 As shown, the method may include the following steps: S101: Construct a routing state vector based on natural language query text, database pattern structure information, and target history entries in the execution feedback memory.
[0025] The target historical entry is a historical entry in the execution feedback memory that matches the natural language query text with a preset matching degree. The historical entry represents the mapping information of the routing decision and execution result of the historical database query.
[0026] Specifically, it receives natural language query text, obtains the schema structure information of the database, and executes the target historical entries in the feedback memory.
[0027] The natural language query text is encoded and converted into a high-dimensional semantic vector representation to obtain the query semantic encoding. The process of query semantic encoding can be represented as follows: ;in, Indicates the semantic encoding of the query. BAAI General Embedding is an open-source text embedding model used to convert natural language text into fixed-length numerical vectors (embedding vectors) to represent the semantics of the text. It is input to The model's natural language query text, 768 represents the vector dimension of the standard output, and the query semantic encoding is used to capture the semantic intent, keyword distribution, and question complexity of the query.
[0028] The schema structure information can be a Scheme heterogeneous graph. The database schema (tables, columns, foreign key relationships) is modeled as a heterogeneous graph G(V,E), and encoded using a three-layer graph attention encoding network. This process can be represented as follows: ; in, The schema structure code that represents the schema structure information. This represents a 3-layer graph attention network. A database schema diagram, also known as a database structure diagram, is used to integrate structural information such as JOIN paths between tables, column semantics, and primary and foreign key constraints.
[0029] The target historical entries are determined from the Execution Feedback Memory (EFM). The EFM stores historically processed query entries, and each historical entry records at least five dimensions of information: the historical query text and its encoded representation, the schema structure information and characteristics of the database corresponding to the historical query, the activation distribution output by the routing strategy network after the historical query is processed, the target experts who actually participated in the inference after being filtered by the activation distribution, and the execution result obtained after the query plan generated by this query is executed on the database.
[0030] The above vectors can be fused to obtain the historical vector corresponding to the historical entry. Historical entries whose matching degree between the natural language query text and the historical entry exceeds the preset matching degree are identified as target historical entries, and the experience information they carry is used to assist the routing decision of the current query.
[0031] The preset matching degree can also be determined using a top-k method, where k can be set to 3. This means retrieving the top 3 historical entries from the Execution Feedback Memory (EFM) that are most similar to the current natural language query text. The retrieval method can be expressed as follows: ; in, Indicates the target historical entry. This indicates that the FAISS vector retrieval tool is invoked to perform a similarity search. This indicates the execution of a feedback memory, enabling routing decisions to directly utilize historical entries of the target as prior features during inference, rather than just for offline training sample replay.
[0032] In addition, the query success rate of the most recent preset number of queries and the usage frequency of each expert to be activated are obtained. The preset number of queries can be 10. The query semantic encoding, pattern structure encoding, target historical entries, query success rate and usage frequency are concatenated to obtain the routing state vector, which can be represented as: ; in, Indicates splicing, recent_stats indicates The recent success rate of preset queries and the usage frequency of each expert to be activated.
[0033] This application embodiment incorporates historically similar decision-making experiences as part of the current routing state and uses historical experiences stored in the execution feedback memory as prior knowledge to participate in the construction of the routing state vector. This allows the current query's routing decision to benefit from historical successful or failed execution experiences, achieving a fusion of knowledge and data-driven decision-making, thereby reducing the uncertainty of the decision.
[0034] S102: Based on the routing state vector, determine the activation distribution of each expert to be activated through the routing policy network, and determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts.
[0035] Specifically, the routing policy network processes the routing state vector to determine the activation distribution of each expert to be activated. The dimension of the activation distribution corresponds to the total number of experts to be activated. Each element in the activation distribution corresponds to one expert to be activated, and its value reflects the degree to which the routing policy network tends to favor that expert to be activated in the current query.
[0036] Furthermore, target experts are determined from the pool of experts to be activated by dynamically adjusting the number and activation distribution of experts. The number of dynamic experts is dynamically adjusted based on recent execution statistics. This allows for the tracking of recent query success rate trends. An upward trend in the success rate indicates that the current routing decision is effective, and the number of dynamic experts can be appropriately increased to obtain more refined subtask processing results. Conversely, a downward trend in the success rate indicates a need to narrow the decision space and reduce uncertainty, thus requiring a reduction in the number of dynamic experts. The specific value of the number of dynamic experts can be limited to a preset minimum and maximum value, such as a minimum of one and a maximum of three, to reduce the possibility of extreme values causing routing mechanism failure.
[0037] After determining the activation distribution and the number of dynamic experts, the experts to be activated are sorted based on the activation distribution, and the experts whose sorting position is before the number of dynamic experts are selected as the target experts for the current query.
[0038] This application embodiment dynamically generates the activation distribution based on the routing state vector through the routing policy network. It can flexibly adjust the activation weight of each expert according to the semantic features of the current query and the database structure features. Through the adaptive adjustment of the number of dynamic experts, a dynamic balance between accuracy and efficiency is achieved.
[0039] S103: Based on the target expert's processing of the routing state vector, a query plan is obtained, and the query plan is executed on the database to obtain the execution result.
[0040] Specifically, based on the routing state vector processed by the target experts, each target expert independently completes the reasoning process on the routing state vector. Different target experts are assigned to perform different sub-tasks. Each target expert generates corresponding sub-reasoning results in its area of expertise based on the query semantic information and database structure information included in the routing state vector. The sub-reasoning results can be in the form of a structured intermediate representation.
[0041] In addition, the sub-inference results are weighted and fused according to the activation weights of each target expert in the activation distribution to obtain a query plan. The query plan is then executed on the database to obtain the execution results. The query plan represents the complete SQL statement to be verified generated after the weighted fusion of multiple target experts. The execution results are the standardized structured data output by the database after the SQL is actually executed, including whether the execution was successful, the number of rows returned, the execution time, error messages, and quality scores.
[0042] This application embodiment uses multiple target experts to process in parallel and fuse the results according to the routing weight. The generated query plan integrates knowledge from multiple professional fields, which can reduce the reasoning bias of a single expert in non-advantageous areas, thereby improving the overall quality of the final query plan.
[0043] S104: Perform hierarchical utility evaluation on the execution results according to the preset hierarchical evaluation method to obtain the execution utility value of the routing policy network on the execution results.
[0044] Specifically, the execution results are evaluated hierarchically according to a preset hierarchical evaluation method, thereby quantifying the overall effect of this routing decision and query generation process and providing numerical feedback signals for subsequent parameter updates of the routing policy network.
[0045] Parsing the execution results is the starting point for utility evaluation. It determines whether the execution was successful. If the execution fails, the error type is further identified from the error message. Error types can be categorized at least as follows: syntax errors (indicating that the structured query statement does not conform to the database's syntax rules); schema errors (indicating that the query statement references a non-existent table or field name); logical errors (indicating that the query statement is syntactically correct and can run, but the returned result does not match the user's query intent); and execution errors (indicating that the query statement triggered a database runtime exception during execution, such as division by zero or type conversion failure). Different levels of negative utility values can be assigned as penalties for different types of errors.
[0046] If the execution is successful, a quality score of the execution result can be obtained, thereby providing a quantitative description of the degree of semantic matching.
[0047] The execution utility value of the routing strategy network on the execution result is determined based on the quality score. In addition, the calculation of the execution utility value can also introduce a resource efficiency regularization term, that is, the fewer experts actually activated, the smaller the penalty deducted, and the more experts activated, the larger the penalty deducted. This is used to guide the routing strategy to minimize unnecessary expert activation while ensuring query quality, thereby optimizing the overall resource consumption.
[0048] This application embodiment, through a hierarchical utility evaluation design, can perform differentiated quantitative evaluation of different types of execution results. Successful and high-quality results receive positive incentives, while unsuccessful results receive negative penalties, and the severity of the penalties varies according to the type of error. This provides the policy network with more discriminative learning objectives and accelerates the convergence process of the policy network.
[0049] S105: Determine the expected state value and expected action value corresponding to the routing state vector through the utility evaluation network.
[0050] Specifically, the expected state value and expected action value corresponding to the routing state vector are determined through a utility evaluation network. The utility evaluation network can be implemented using a dual-tower structure, which includes two parallel sub-networks: a state value network and an action value network.
[0051] During training, the State Value Network and the Action Value Network fit different targets. The State Value Network fits the State Value Target, while the Action Value Network fits the Action Value Target.
[0052] Furthermore, after obtaining the expected state value and expected action value, the two can be weighted and fused according to a preset fusion coefficient to obtain a comprehensive estimate of the expected execution utility.
[0053] This application embodiment estimates the expected utility of routing states and specific routing actions through a state value network and an action value network, respectively. The state value provides a baseline estimate from a long-term perspective, while the action value provides a detailed evaluation from an immediate perspective, thus providing a more stable baseline reference for subsequent advantage calculations.
[0054] S106: Iteratively optimize the routing policy network based on the expected state value, expected action value, and execution utility value to obtain an updated routing policy network, and adjust the activation distribution based on the updated routing policy network.
[0055] Specifically, an advantage evaluation signal is obtained based on the expected state value, expected action value, and execution utility value. The advantage evaluation signal is used as a weight coefficient to construct a policy gradient loss function. The routing policy network is iteratively optimized based on the gradient loss function to obtain an updated routing policy network. The activation distribution is then adjusted based on the updated routing policy network.
[0056] Specifically, the parameter gradients of neurons in each layer of the routing policy network can be calculated based on the policy gradient loss function, and the network parameters can be updated along the direction of the parameter gradients. The magnitude of the parameter update can be controlled by hyperparameters such as the learning rate, thereby obtaining the updated routing policy network.
[0057] Furthermore, in actual operation, the routing strategy network is optimized based on the routing state vector, routing strategy network, and execution results of each query to optimize the query route in the next database query. As the number of queries increases, the parameters of the routing strategy network are continuously adjusted in the direction of improving utility, so that the overall query success rate gradually increases and tends to converge.
[0058] Furthermore, the number of dynamic experts can be adjusted based on query results and execution utility values. This adjustment can be made through statistical feedback, thereby optimizing the overall routing strategy.
[0059] This application embodiment drives the online parameter update of the routing policy network through the policy gradient loss function and advantage evaluation signal. It can continuously accumulate experience and improve the decision-making strategy during actual operation, and transform the execution result of each query into valuable gradient information to be fed back to the routing policy network, thereby improving the overall performance.
[0060] The embodiments of this application can use historical execution feedback as prior experience to participate in current decision-making and use the current execution effect as a feedback signal to drive strategy evolution. Through the two-stage mechanism of prior guidance and feedback learning, the quality of query generation is continuously improved, and the performance continues to improve with the increase of usage time, thereby improving the accuracy and real-time performance of queries.
[0061] In some embodiments, after constructing the routing state vector based on the natural language query text, the pattern structure information of the database, and the target history entries in the execution feedback memory, the method further includes: If the total number of historical entries in the execution feedback memory exceeds the preset entry number threshold, based on the routing state vector, the steps are to determine the activation distribution of each expert to be activated through the routing policy network, and to determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. If the total number of historical entries in the execution feedback memory does not exceed a preset entry number threshold, determine the keyword features in the natural language query text. By using a pre-defined rule classifier, the activation weight of each expert to be activated is determined based on the weight mapping relationship between keyword features and pre-defined expert weights, and the target expert is determined based on the activation weight.
[0062] Specifically, the total number of historical entries in the execution feedback memory needs to be determined, as the total number of historical entries stored in the execution feedback memory may not be sufficient to support the routing policy network in making reliable decisions.
[0063] If the total number of historical entries in the feedback memory exceeds a preset entry number threshold, it indicates that the routing policy network lacks sufficient training signals to establish a reliable mapping relationship between the routing state vector and the expert activation distribution. The following steps are then performed: determining the activation distribution of each expert to be activated through the routing policy network, and determining the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts.
[0064] The specific value of the preset threshold for the number of entries can be set according to the actual application scenario and the complexity of the expert system. For example, it can be set to 100 entries.
[0065] If the total number of historical entries in the execution feedback memory does not exceed the preset entry number threshold, it indicates that the current stage is still cold start and historical experience is insufficient to support reliable adaptive routing decisions. Keyword features in natural language query text are determined. Keyword features can be extracted by matching through a pre-built keyword dictionary, which includes typical keyword sets corresponding to different types of queries.
[0066] For example, queries involving multi-table joins typically include keywords such as join, association, and union, or specific inter-table referencing patterns. Queries involving statistical calculations typically include aggregation keywords such as total, average, group, sort, maximum, and minimum.
[0067] After extracting keyword features, they are input into a preset rule classifier. The preset rule classifier stores the mapping relationship between keyword features and expert activation weights. The mapping relationship is preset based on the prior knowledge of the expert division of labor.
[0068] The activation weights output by the preset rule classifier are normalized to form an activation distribution. The target expert is determined based on the activation distribution. In the case of cold start, the activation distribution is completely determined by the preset rule mapping and does not involve the inference calculation of the routing policy network, thus reducing the prediction uncertainty of the routing policy network due to insufficient training in the cold start stage.
[0069] In some embodiments, the activation distribution can be output using a lightweight multilayer perceptron, and the process of determining the activation distribution can be expressed as follows: ;in, Indicates the activation distribution. This represents a multilayer perceptron.
[0070] Figure 2 This is a schematic diagram of the structure of a multilayer sensor provided in an embodiment of this application, as shown below. Figure 2 As shown, the routing state vector s is taken as input and passes through the first fully connected layer (Linear) (220), the activation function (230), the Dropout layer with drop probability p=0.1 (240), the second fully connected layer (Linear) (250), the activation function (260), and the third fully connected layer (270) in sequence. The output is the original weights corresponding to each expert to be activated, and the activation distribution is obtained by softmax.
[0071] Among them, the fully connected Linear layer is used to complete the feature transformation and dimension mapping of the routing state vector and extract the deep features required for routing decisions; Activation functions are used to introduce non-linearity, allowing the network to fit complex expert choice logic; Dropout is used to randomly mask 10% of neurons during the training phase, reducing the risk of overfitting in the routing strategy network and ensuring the generalization ability of routing decisions in new online query scenarios.
[0072] When the total number of historical entries in the execution feedback memory exceeds a preset threshold, it means that the execution feedback memory has accumulated enough historical experience to support data-driven expert selection.
[0073] Furthermore, select the K_dynamic experts with the highest weights, where the value of K_dynamic can be expressed as: ; in, This indicates the selected target experts. This represents the activation distribution.
[0074] Parallel expert reasoning: Each selected expert receives the same input but focuses on different subtasks. The task division is shown in Table 1, Expert Responsibility Division Table.
[0075] Table 1. Division of Responsibilities among Experts Simultaneously set the dynamic K value To make decisions, the number of target experts to be activated is adaptively determined based on recent performance trends. The dynamic K-value is set as follows: ; in, This indicates the recent success rate trend, with a value range of [-2, 2]. Positive values indicate an increasing success rate, while negative values indicate a decreasing success rate. (Selected...) Each target expert receives the same input (state vector s), but focuses on different subtasks, and reasones independently in parallel. The outputs of each target expert are weighted and fused to generate a query plan. The process of determining the query plan can be represented as: ; in, Indicates the query plan. is the weight of the i-th target expert, and K represents the number of target experts.
[0076] If the total number of historical entries in the feedback memory does not exceed a preset threshold, the system enters cold start mode. At this point, due to a lack of sufficient historical experience data, learning-based adaptive routing decisions are unreliable; therefore, deterministic rule-based routing is employed to reduce the uncertainty caused by random exploration.
[0077] Based on the keyword features of the query embedding vector q_emb, a three-layer rule classifier is designed to determine the activation distribution of each target expert. Specifically, if the keyword features of the query embedding vector q_emb contain "JOIN" or "association," it indicates that the query involves multiple table joins, requiring focus on table selection and JOIN path planning. In this case, the activation distribution of the Schema expert can be set to 0.6, and the activation distribution of the Template expert to 0.4. If the query keyword features include "GROUP" or "aggregate", it means that the query involves statistical calculations and requires special attention to the SQL framework and aggregate functions. You can set the activation distribution of the Template expert to 0.7 and the activation distribution of the Corr expert to 0.3. Otherwise, adopt a general balanced configuration, prioritize ensuring the SQL framework is correct, and set the activation distribution of Template experts to 0.8 and the activation distribution of Corr experts to 0.2.
[0078] All rule weights are predefined fixed values that do not dynamically adjust with changes in queries, ensuring stability and predictability during the cold start phase and avoiding routing oscillations caused by insufficient experience. As the EFM_size gradually grows to over 100 rules, it will smoothly transition to the routing mode of the routing policy network.
[0079] This application embodiment utilizes a preset rule classifier that, based on prior knowledge, provides deterministic outputs under all input conditions and is unaffected by sample scarcity. During cold start, the preset rule classifier determines the activation distribution. With sufficient historical entries in the execution feedback memory, the routing strategy network determines the activation distribution of each expert to be activated. This ensures that the performance curve does not experience drastic jumps during the transition from cold start to hot operation and guarantees the stability of the query plan.
[0080] In some embodiments, the execution results are evaluated hierarchically according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution results, including: Analyze the execution results to obtain the corresponding quality score and error type. If the quality score exceeds the first scoring threshold, the performance utility value is determined based on the high-quality evaluation formula and the quality score. If the quality score does not exceed the first scoring threshold but exceeds the second scoring threshold, the execution utility value is determined based on the medium quality assessment formula and the quality score. If the quality score does not exceed the second scoring threshold, the penalty value is determined based on the error type and is then used as the execution utility value.
[0081] Specifically, Parse the SQL execution results, extract key information, and obtain whether the execution was successful, the quality score, and the error type.
[0082] The execution results are evaluated hierarchically by using a pre-defined hierarchical evaluation method, thereby obtaining the execution utility value of the routing policy network for the execution results.
[0083] Among them, stratified utility evaluation can be divided into high-quality success utility, general success utility, and failure penalty.
[0084] In high-quality success utility, if the SQL executes successfully and the quality score exceeds the first scoring threshold, it indicates that the SQL not only runs but also highly matches the user's intent, thus receiving a high utility value. The first scoring threshold can be 0.8. A corresponding process for determining the execution utility value (i.e., the high-quality evaluation formula) can be expressed as: ; in, Indicates the performance utility value. This indicates a quality score.
[0085] Generally, a successful execution utility score is assigned when the SQL statement is executed successfully but the quality score does not exceed the first scoring threshold and exceeds the second scoring threshold. This indicates that the SQL syntax is correct but the logic is flawed, and a medium utility value is assigned. The first scoring threshold can be 0.8, and the second scoring threshold can be 0. The process for determining a corresponding execution utility value (i.e., the medium quality assessment formula) can be expressed as follows: ; in, Indicates the performance utility value. This indicates a quality score.
[0086] Failure penalty, representing the situation of execution failure, directly assigns a value less than 0 to the quality score. The quality score is positively correlated with the number of error types involved. Differential negative utility values (penalty values) are assigned based on the error type. The process of determining a corresponding execution utility value can be expressed as follows: .
[0087] Different penalty values can be assigned to different error types. For example, a light penalty is imposed for syntax errors, which are relatively easy to fix; a medium penalty is imposed for schema errors; a medium penalty is imposed for schema misunderstanding; a medium penalty is imposed for logic errors; a medium penalty is imposed for query intent misunderstanding; a medium penalty is imposed for execution errors; and a medium penalty is imposed for database constraint errors.
[0088] This application embodiment divides the quality score into three intervals—high quality, medium quality, and low quality—by setting a first scoring threshold and a second scoring threshold, and assigns different utility calculation formulas to each interval. This enables gradient feedback adjustment in semantic matching accuracy. By differentiating the penalty intensity for different error types, the routing policy network can be guided to prioritize avoiding higher-cost error types during the learning process, thereby improving its sensitivity to severe error types.
[0089] In some embodiments, determining the expected state value and expected action value corresponding to the routing state vector through a utility evaluation network includes: The expected state value of the routing state vector is obtained by processing the state value network in the utility evaluation network. The expected action value is obtained by processing the routing state vector and activation distribution in the utility evaluation network.
[0090] Specifically, the utility evaluation network has a dual-tower network structure, consisting of two parallel sub-networks: a state value network and an action value network.
[0091] The routing state vector is input into the state value network to perform multi-level nonlinear transformations on the routing state vector through the state value network to obtain the expected state value. The expected state value is a scalar value, which is the expected cumulative execution utility that the network can obtain under the current routing state and the current routing policy, reflecting the quality of the routing state itself.
[0092] During training, the goal of the State Value Network is to minimize the mean square error between its output and the cumulative utility observed in subsequent tests. By continuously using actual observations as supervision signals for regression fitting, the State Value Network gradually learns the ability to accurately evaluate the value of different routing states.
[0093] The routing state vector and the activation distribution output by the current routing policy network are used as inputs to the action value network. The action value network processes the routing state vector and activation distribution to obtain the expected action value. The expected action value is also a scalar value, which represents the expected execution utility that can be obtained when taking the target expert corresponding to the current activation distribution in the current routing state.
[0094] During training, the action value network needs to fit the value corresponding to the activation distribution output by the current routing policy network, and also needs to estimate the value corresponding to different activation distributions under the same routing state by using historical decision samples stored in the experience replay buffer.
[0095] In some embodiments, a dual-tower Critic network is used to estimate the expected execution utility of the current routing state vector s in parallel, including a state value network V(s) and an action value network Q(s, gates), which can be expressed as follows: ; ; in, This represents the expected state value that can be obtained starting from the current routing state s. This represents the expected action value that can be obtained by selecting the activation distribution gates of the target expert.
[0096] This application embodiment uses expected state value and expected action value. Based on the estimation characteristics of the state value network and action value network in terms of bias and variance, in practical applications, the state value network and action value network extract value from different perspectives on the same set of query results and query process through their respective network structures, thereby improving the utilization efficiency of the samples.
[0097] In some embodiments, the routing policy network is iteratively optimized based on the expected state value, expected action value, and execution utility value to obtain an updated routing policy network, including: Determine the expected performance utility value based on the expected state value and the expected action value; The advantage evaluation signal is determined based on the difference between the expected performance utility value and the actual performance utility value; The policy gradient loss is determined based on the advantage evaluation signal and activation distribution; The parameter gradient of the routing policy network is determined by the policy gradient loss, and the routing policy network is updated along the gradient direction of the parameter gradient to obtain the updated routing policy network.
[0098] Specifically, the expected state value and expected action value are fused to obtain the expected execution utility value. The fusion method can be a weighted average. The specific value of the fusion coefficient can be set according to the trade-off between the stability of state value and the sensitivity of action value in the actual application scenario. The expected execution utility value represents the prior expectation of the decision effect in the current routing state.
[0099] In some embodiments, the expected state value and the expected action value can be fused to obtain the expected execution utility value. The fusion process can be represented as follows: ; in, This represents the expected performance utility value. The weighting coefficients represent the expected state value. Represents the expected state value. Indicates the expected value of the action. This represents the weighting coefficient corresponding to the expected action value. One possible value is 0.7.
[0100] In addition, the difference between the expected performance utility and the actual performance utility value is determined, and this difference is used as an advantage evaluation signal to indicate the degree of superiority or inferiority of the actual decision relative to the expected benchmark.
[0101] When the actual utility value exceeds the expected utility value, the advantage evaluation signal is positive, indicating that the routing decision has exceeded expectations, and the routing policy network should learn positive lessons from this decision. When the actual utility value is lower than the expected utility value, the advantage evaluation signal is negative, indicating that the decision has not met expectations, and the routing policy network should learn negative lessons from this decision.
[0102] Among these, the use of the value function to calculate the advantage function is a key technique for reducing variance in the policy gradient iteration of NL2SQL expert routing, which can be expressed as: ; Where R_total represents the execution utility value, Let A(s, gates_topK) represent the expected performance utility estimated by the Critic dual-tower network, and let A(s, gates_topK) represent the advantage evaluation signal.
[0103] Table 2 is a schematic table of advantage evaluation signals provided in the embodiments of this application.
[0104] Table 2. Schematic diagram of advantage evaluation signals Directly calculating the policy gradient can lead to high variance because performance utility evaluation is random. By subtracting the baseline, variance can be significantly reduced without changing the expectation, making training more stable.
[0105] Furthermore, the corresponding policy gradient loss is determined based on the advantage evaluation signal and the current activation distribution. The policy gradient loss can be determined through the policy gradient loss function, which can be expressed as: ; in, Represents the policy gradient loss. It represents the expected value, used to calculate the mean. This represents the sample set of the routing state vector-activation distribution. Indicates a routing policy network. This indicates a signal indicating an advantage.
[0106] The process of calculating the logarithmic probability of the strategy can be expressed as follows: ; in, This indicates that the output contains K unnormalized values. ,through Convert to a logarithmic probability distribution, retaining only the probability components corresponding to the Top-K target experts for gradient calculation.
[0107] Advantage evaluation signal As a weighting coefficient, it enables strategy optimization: when When the value is greater than 0, this term is positive, and gradient ascent tends to increase the corresponding value. The probability of choosing; when When the value is less than 0, this term is negative, and gradient ascent is equivalent to decreasing the corresponding value. The selection probability is used to strengthen excellent routing decisions and suppress inefficient ones.
[0108] Furthermore, the partial derivatives of the loss with respect to each trainable parameter in the routing policy network are calculated using the backpropagation algorithm to obtain the parameter gradients of each parameter. The parameter gradients are used to indicate the direction in which the loss function increases the fastest under the current parameter configuration. Adjusting the parameters along this direction can make the loss function decrease in subsequent iterations, thereby obtaining the updated routing policy network.
[0109] Furthermore, to break sample correlation and improve sample utilization, a cross-period routing strategy calibration mechanism can be adopted. This mechanism stores the current routing state vector and the estimated advantage quintuple of the execution utility value corresponding to the target expert in a buffer. The buffer can be represented as: ; To implement adaptive capacity management, the conditions for adaptive capacity management can be expressed as follows: ; This means removing the earliest 50% of experience entries, keeping the buffer capacity stable, and prioritizing the retention of recently valid experience.
[0110] The process of obtaining samples by randomly and uniformly sampling using a random sampling strategy can be represented as follows: ; in, The sample is represented by 32 samples randomly and uniformly sampled from the Buffer. The cross-cycle routing policy calibration mechanism has the following functions: breaking correlation, i.e., consecutive samples are often highly correlated, and random sampling breaks the correlation; sample reuse, i.e., each experience can be learned multiple times, improving utilization; and training stability, i.e., reducing drastic parameter oscillations. This mechanism breaks the temporal correlation of consecutive experiences through random sampling, realizes multi-round reuse of a single experience, and smooths the gradient update process, thereby significantly improving the stability and convergence speed of policy learning.
[0111] In some embodiments, to reduce the routing policy network from getting trapped in local optima and to maintain long-term exploration capability, a distributed sampling can be performed using an εgreedy exploration strategy combined with Dirichlet. The decision-making process is as follows: ;in, Indicates the probability of exploration. This represents the attenuation coefficient, which can be expressed as: ; The specific sampling rules are as follows: When Uniform(0,1)<ε_t, it indicates the exploration phase, and Dirichlet distribution sampling of mild and random expert combinations can be performed, gates_topK ← Dirichlet(α=[2,2,2]); otherwise, it is the exploitation phase, and the optimal target expert is selected based on the current routing policy network, gates_topK ← argtopK(MLP_policy(s), K_dynamic); Among them, the distribution parameter α of Dirichlet achieves a relatively mild exploration preference, reducing the excessive dispersion caused by completely uniform random sampling; ε decays linearly from 0.1 to 0.01 in the first 10,000 training steps, and then remains at 0.01 to maintain a small amount of exploration.
[0112] Furthermore, Figure 3 This is a schematic diagram of a positive feedback path in a routing policy network provided in an embodiment of this application, as shown below. Figure 3 As shown, in step S301, after expanding and improving the historical entries stored in the Feedback Memory (EFM), in step S302, the top three highly matching target historical entries are retrieved to improve accuracy. In step S303, the constructed routing state vector s represents the improved quality. In step S304, iterative optimization is completed based on the activation distribution output by the routing policy network. In step S305, the total execution utility of the query is calculated, and the total execution utility value increases. This corresponds to an increase in the memory value estimate. Then, in step S306, high-quality execution samples are added to the execution feedback memory through a positive feedback loop to further enrich the data in the EFM, forming a closed-loop positive iterative optimization process.
[0113] Convergence can be determined based on a typical convergence curve, and can be expressed as: ; Where t represents the number of days and τ represents the time constant, approximately 7 days. The accuracy rate increases from 70% to approximately 80% in 7 days, and the accuracy rate can be stabilized at over 85% in 30 days.
[0114] This application's embodiments use a weighted fusion of expected state value and expected action value as the expected benchmark, reducing baseline bias caused by the quality of the routing state itself. Through policy gradient loss, and using the advantage evaluation signal as the weighting coefficient of the activation distribution's log probability, asymmetric learning of positive and negative samples is achieved, driving the policy distribution towards the optimal direction.
[0115] In some embodiments, after performing a hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution results, the method further includes: The entries to be stored are determined based on the natural language query text, activation distribution, query plan, and execution results. The value of the items to be stored is evaluated according to the preset memory evaluation dimensions to obtain multi-dimensional evaluation results. The preset memory evaluation dimensions include at least one of the following: novelty dimension, database structure adaptability dimension, routing decision confidence dimension, and timeliness dimension. The multi-dimensional evaluation results are weighted and summed to obtain the comprehensive memory value of the item to be stored; If the total memory value exceeds the preset storage threshold, the entry to be stored will be stored in the execution feedback memory.
[0116] Specifically, the entries to be stored are determined based on the natural language query text, activation distribution, query plan, and execution results.
[0117] In addition, the value of the items to be stored is quantitatively evaluated according to the preset memory evaluation dimensions to obtain multi-dimensional evaluation results. The preset memory evaluation dimensions include at least one of the following: novelty dimension, database structure adaptability dimension, routing decision confidence dimension, and timeliness dimension.
[0118] The novelty dimension measures the semantic difference between the current query and existing queries in the execution feedback memory. The calculation process of the novelty dimension can be expressed as follows: ; Where q_emb represents the semantic vector encoding of the current query, M3 semantic vector (1024 dimensions), m.q_emb represents the query semantic vector of the m-th historical entry in the execution feedback memory, cos_similarity represents the cosine similarity, ranging from [-1, 1], with larger values indicating greater similarity, and max(...) represents finding the memory most similar to the current query and calculating its similarity. Novelty is designed to reduce the storage of duplicate or highly similar queries, save memory space, and encourage the exploration of new query patterns.
[0119] The database structure fit dimension measures the degree of matching between the schema structure information of the current database and the schema structure information corresponding to the most similar historical query in the execution feedback memory. The calculation process of the database structure fit dimension can be expressed as follows: ; Where S_curr represents the graph attention encoding vector (1024-dimensional) of the current database schema, and S_similar represents the schema encoding vector corresponding to the most similar query in the execution feedback memory. This represents the L2 Euclidean distance, measuring the difference between two schema vectors; 0.5 represents the temperature coefficient, controlling the decay rate. This represents an exponential function that maps distances to the interval (0, 1).
[0120] The SchemaFit dimension is designed with NL2SQL domain specificity, ensuring that when the database structure changes (such as table structure changes or the addition of new tables), the combination scheme of the target experts in the historical entries is still applicable to the current schema, thereby reducing cross-database generalization errors and improving the robustness of the execution feedback memory in cross-database generalization scenarios.
[0121] The routing decision confidence dimension measures the degree of confidence the routing policy network has in the expert selection. The calculation process for the routing decision confidence dimension can be expressed as follows: ; Where max(gates_topK) represents the highest activation distribution among the Top-K target experts; second_max(gates_topK) represents the second highest activation distribution among the Top-K target experts.
[0122] The difference between the two values represents the confidence level of the decision. The larger the difference, the clearer and more confident the routing decision is. Historical entries with clear routing decisions and good results from multi-expert collaboration can be stored first to reduce the noise of storing fuzzy decisions.
[0123] The timeliness dimension measures the freshness of memory items. The calculation process for the timeliness dimension can be expressed as follows: ; Where Δt represents the time interval since the last similar query (in days), λ_time represents the time decay coefficient, which can be set to 0.1, and exp(...) represents the exponential decay function, which allows the execution feedback memory to naturally discard outdated experiences that have not been accessed for a long time, maintaining the sensitivity of the execution feedback memory to the current query pattern.
[0124] Furthermore, the multi-dimensional evaluation results are weighted and summed to obtain the comprehensive memory value of the item to be stored. The formula for the weighted summation can be expressed as: ; Design a dynamic weighted adaptive value function, with weights determined by recent_stats (success rate / error frequency). For example, if there are many recent schema errors, increase the weight of SchemaFit. Similarly, if there are frequent database structure changes recently, the weight of the database structure adaptability dimension can be appropriately increased. When the operating environment is relatively stable, the weights of novelty and routing decision confidence dimensions can be emphasized.
[0125] Furthermore, by comparing the overall memory value with the preset storage threshold, if the overall memory value exceeds the preset storage threshold, it indicates that the entry to be stored has sufficient learning value, and it is written into the execution feedback memory. If the overall memory value does not exceed the preset storage threshold, the entry is rejected from being stored in the execution feedback memory, and the processing result of this query is discarded after the parameter update of the current routing policy network is completed, without occupying storage space or retrieval resources in the execution feedback memory.
[0126] This application embodiment uses a multi-dimensional value assessment mechanism to screen each piece of experience to be stored, reducing the storage bloat and retrieval noise problems caused by storing all execution records in the execution feedback memory. The introduction of the database structure adaptability dimension also enables the execution feedback memory to maintain the validity of historical experience when the database structure changes, reducing the memory failure problem caused by database evolution.
[0127] In some embodiments, storing entries to be stored in the execution feedback memory includes: If the overall memory value meets the preset high-value condition, the entry to be stored will be stored in the intermediate high-value pool of the execution feedback memory. If the overall memory value does not meet the preset high-value condition, the entry to be stored will be stored in the short-term calibration buffer pool in the execution feedback memory.
[0128] Specifically, based on the comprehensive memory value V_memory and combined with other auxiliary conditions, a hierarchical storage decision is made for the current memory item.
[0129] Storage can be achieved through tiered storage pools, which include a short-term calibration buffer pool, a medium-term high-value pool, and a long-term essence pool.
[0130] The short-term calibration buffer pool stores all recently executed entries and primarily serves cross-cycle calibration and policy updates of routing policies. The mid-term high-value pool is used to store high-value memories that have been filtered by the four-dimensional constraint value. In actual online reasoning retrieval, the high-value pool is retrieved first (the retrieval results directly participate in the construction of the routing decision state vector). The long-term essence pool is used to store a small number of stable and valid essence memories across time. It is used for priority retrieval during the cold start phase or when a new execution feedback memory is launched, so as to improve the cold start retrieval efficiency.
[0131] If the overall memory value meets the preset high-value conditions, the entry to be stored will be stored in the intermediate high-value pool of the execution feedback memory.
[0132] If the overall memory value does not meet the preset high-value condition, the entry to be stored will be stored in the short-term calibration buffer pool in the execution feedback memory.
[0133] In some embodiments, the storage conditions can be combined when the memory value exceeds a preset high-value condition, the combination success rate is greater than a preset success rate threshold, and the existence time of the entry to be stored exceeds a preset number of days. That is, the storage conditions are expressed as follows: when the entry to be stored simultaneously meets the following three conditions, namely, V_memory>0.6 (i.e., the preset high-value condition), the combination success rate comb_success_rate>0.2, and the age of the entry to be stored age<30 (days), the entry to be stored is migrated from the short-term calibration buffer pool and permanently stored in the medium-term high-value pool of the execution feedback memory (EFM); otherwise, it is only written to the short-term calibration buffer pool for short-term training playback and does not enter the long-term memory space.
[0134] In addition, EFM performs a migration assessment every preset period (e.g., every 100 new memories). For entries in the mid-term high-value pool, if V_memory>0.9 and age<7 days, they are promoted to the long-term premium pool; if comb_success_rate<0.2, they are directly removed from the EFM pool to reduce the storage and retrieval resources occupied by low-value memories.
[0135] This application embodiment uses a hierarchical storage method to store the entries with storage into different storage pools, thereby improving the sample quality during the sample collection process, reducing storage noise, improving the accuracy of the target historical entries in the routing state vector as prior experience, and thus improving the accuracy of the query plan.
[0136] In some embodiments, a query plan is obtained based on the target expert processing routing state vector, including: The routing state vector is input into each target expert to obtain the sub-inference results corresponding to the target experts; Obtain the activation weights of each target expert in the activation distribution, perform a weighted summation on the sub-inference results based on the activation weights, and determine the weighted summation result as the query plan.
[0137] Specifically, the routing state vector is input into each target expert to obtain the sub-inference result corresponding to the target expert. Under the premise of receiving the same input, different target experts focus on different sub-task dimensions. Each target expert extracts the features they are interested in from the routing state vector according to their own functional positioning, and outputs the corresponding sub-inference result after internal inference process.
[0138] Obtain the activation weights of each target expert in the activation distribution. The activation weights are the values corresponding to each expert in the probability distribution output by the routing policy network during the routing decision stage, reflecting the routing policy network's assessment of the importance of each expert in the current query scenario.
[0139] In addition, the corresponding sub-inference results are weighted and summed according to the activation weight of each target expert, and the calculation result is determined as the query plan, which is executed on the database.
[0140] This application embodiment uses multiple target experts to reason about the routing state vector, leveraging the characteristics of each expert, and then performs a weighted summation to obtain a query plan, thereby improving the accuracy of the query.
[0141] Figure 4 This is a schematic diagram of a database query routing optimization device provided in an embodiment of this application. Figure 4 As shown, the device may include a construction module 410, an activation module 420, a query module 430, an evaluation module 440, an expectation module 450, and an update module 460.
[0142] The construction module 410 is used to construct a routing state vector based on the natural language query text, the pattern structure information of the database, and the target historical entries in the execution feedback memory. The target historical entries are historical entries in the execution feedback memory that have a matching degree with the natural language query text that exceeds a preset matching degree. The historical entries represent the mapping information between the routing decisions and execution results of the historical database queries. The activation module 420 is used to determine the activation distribution of each expert to be activated through the routing policy network based on the routing state vector, and to determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. The query module 430 is used to process the routing state vector based on the target expert, obtain a query plan, execute the query plan on the database, and obtain the execution result; Evaluation module 440 is used to perform hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method, and obtain the execution utility value of the routing policy network on the execution results; The expectation module 450 is used to determine the expected state value and expected action value corresponding to the routing state vector through the utility evaluation network. The update module 460 is used to iteratively optimize the routing policy network based on the expected state value, expected action value, and execution utility value to obtain the updated routing policy network, and to adjust the activation distribution based on the updated routing policy network.
[0143] In some embodiments, after constructing the routing state vector based on the natural language query text, the pattern structure information of the database, and the target history entries in the execution feedback memory, the construction module 410 is further used for: If the total number of historical entries in the execution feedback memory exceeds the preset entry number threshold, based on the routing state vector, the steps are to determine the activation distribution of each expert to be activated through the routing policy network, and to determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. If the total number of historical entries in the execution feedback memory does not exceed a preset entry number threshold, determine the keyword features in the natural language query text. By using a pre-defined rule classifier, the activation weight of each expert to be activated is determined based on the weight mapping relationship between keyword features and pre-defined expert weights, and the target expert is determined based on the activation weight.
[0144] In some embodiments, the evaluation module 440 performs a hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network on the execution results, which is used for: Analyze the execution results to obtain the corresponding quality score and error type. If the quality score exceeds the first scoring threshold, the performance utility value is determined based on the high-quality evaluation formula and the quality score. If the quality score does not exceed the first scoring threshold but exceeds the second scoring threshold, the execution utility value is determined based on the medium quality assessment formula and the quality score. If the quality score does not exceed the second scoring threshold, the penalty value is determined based on the error type and is then used as the execution utility value.
[0145] In some embodiments, the expectation module 450 determines the expected state value and expected action value corresponding to the routing state vector through a utility evaluation network, for the purpose of: The expected state value of the routing state vector is obtained by processing the state value network in the utility evaluation network. The expected action value is obtained by processing the routing state vector and activation distribution in the utility evaluation network.
[0146] In some embodiments, the updating module 460 iteratively optimizes the routing policy network based on the expected state value, expected action value, and execution utility value to obtain an updated routing policy network, used for: Determine the expected performance utility value based on the expected state value and the expected action value; The advantage evaluation signal is determined based on the difference between the expected performance utility value and the actual performance utility value; The policy gradient loss is determined based on the advantage evaluation signal and activation distribution; The parameter gradient of the routing policy network is determined by the policy gradient loss, and the routing policy network is updated along the gradient direction of the parameter gradient to obtain the updated routing policy network.
[0147] In some embodiments, after the evaluation module 440 performs a hierarchical utility evaluation on the execution result according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network on the execution result, it is further configured to: The entries to be stored are determined based on the natural language query text, activation distribution, query plan, and execution results. The value of the items to be stored is evaluated according to the preset memory evaluation dimensions to obtain multi-dimensional evaluation results. The preset memory evaluation dimensions include at least one of the following: novelty dimension, database structure adaptability dimension, routing decision confidence dimension, and timeliness dimension. The multi-dimensional evaluation results are weighted and summed to obtain the comprehensive memory value of the item to be stored; If the total memory value exceeds the preset storage threshold, the entry to be stored will be stored in the execution feedback memory.
[0148] In some embodiments, the evaluation module 440 stores the entries to be stored in the execution feedback memory for: If the overall memory value meets the preset high-value condition, the entry to be stored will be stored in the intermediate high-value pool of the execution feedback memory. If the overall memory value does not meet the preset high-value condition, the entry to be stored will be stored in the short-term calibration buffer pool in the execution feedback memory.
[0149] In some embodiments, the query module 430 obtains a query plan based on the target expert processing routing state vector, for use in: The routing state vector is input into each target expert to obtain the sub-inference results corresponding to the target experts; Obtain the activation weights of each target expert in the activation distribution, perform a weighted summation on the sub-inference results based on the activation weights, and determine the weighted summation result as the query plan.
[0150] Figure 5 A schematic diagram of the hardware structure of the terminal device provided in an embodiment of this application is shown.
[0151] The terminal device may include a processor 501 and a memory 502 storing computer program instructions.
[0152] Specifically, the processor 501 may include a central processing unit (CPU), an application specific integrated circuit (ASIC), or one or more integrated circuits that can be configured to implement the embodiments of this application.
[0153] Memory 502 may include mass storage for data or instructions. For example, and not limitingly, memory 502 may include a hard disk drive (HDD), floppy disk drive, flash memory, optical disk, magneto-optical disk, magnetic tape, or Universal Serial Bus (USB) drive, or a combination of two or more of these. In one instance, memory 502 may include removable or non-removable (or fixed) media, or memory 502 may be non-volatile solid-state storage. Memory 502 may be internal or external to the integrated gateway disaster recovery device.
[0154] In one instance, memory 502 may be read-only memory (ROM). In one instance, the ROM may be a mask-programmed ROM, a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable PROM (EEPROM), an electrically rewritable ROM (EAROM), or flash memory, or a combination of two or more of these.
[0155] Memory 502 may include read-only memory (ROM), random access memory (RAM), disk storage media device, optical storage media device, flash memory device, electrical, optical, or other physical / tangible memory storage device. Therefore, generally, memory includes one or more tangible (non-transitory) computer-readable storage media (e.g., memory devices) encoded with software including computer-executable instructions, and when the software is executed (e.g., by one or more processors), it is operable to perform the operations described with reference to the method according to the first aspect of this disclosure.
[0156] The processor 501 reads and executes computer program instructions stored in the memory 502 to achieve... Figure 1 The database query routing optimization method in the illustrated embodiment.
[0157] In one example, the terminal device may also include a communication interface 503 and a bus 504. Wherein, for example... Figure 5 As shown, the processor 501, memory 502, and communication interface 503 are connected through bus 504 and complete communication with each other.
[0158] The communication interface 503 is mainly used to realize communication between various modules, devices, units and / or equipment in the embodiments of this application.
[0159] Bus 504 includes hardware, software, or both, that couples components of an online data traffic metering device together. For example, and not as a limitation, the bus may include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an Infinite Bandwidth Interconnect, a Low Pin Count (LPC) bus, a memory bus, a Microchannel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local (VLB) bus, or other suitable buses, or a combination of two or more of these. Where appropriate, bus 504 may include one or more buses. Although specific buses are described and illustrated in embodiments of this application, this application contemplates any suitable bus or interconnect.
[0160] Furthermore, in conjunction with the database query routing optimization method in the above embodiments, this application embodiment can provide a computer-readable storage medium for implementation. This computer-readable storage medium stores computer program instructions; when these computer program instructions are executed by a processor, they implement any of the database query routing optimization methods in the above embodiments.
[0161] This application also provides a computer program product, including a computer program that, when executed by a processor, implements any of the database query routing optimization methods described in the above embodiments.
[0162] It should be clarified that this application is not limited to the specific configurations and processes described above and shown in the figures. For the sake of brevity, detailed descriptions of known methods are omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of this application is not limited to the specific steps described and shown. Those skilled in the art can make various changes, modifications, and additions, or change the order of steps, after understanding the spirit of this application.
[0163] The functional blocks shown in the above-described block diagram can be implemented as hardware, software, firmware, or a combination thereof. When implemented in hardware, they can be, for example, electronic circuits, application-specific integrated circuits (ASICs), appropriate firmware, plug-ins, function cards, etc. When implemented in software, the elements of this application are programs or code segments used to perform the required tasks. Programs or code segments can be stored on a machine-readable medium or transmitted over a transmission medium or communication link via data signals carried on a carrier wave. "Machine-readable medium" can include any medium capable of storing or transmitting information. Examples of machine-readable media include electronic circuits, semiconductor memory devices, read-only memory (ROM), flash memory, erasable read-only memory (EROM), floppy disks, compact disc read-only memory (CD-ROM), optical disks, hard disks, fiber optic media, radio frequency (RF) links, etc. Code segments can be downloaded via computer networks such as the Internet, intranets, etc.
[0164] It should also be noted that the exemplary embodiments mentioned in this application describe methods or systems based on a series of steps or apparatus. However, this application is not limited to the order of the above steps; that is, the steps can be performed in the order mentioned in the embodiments, or in a different order, or several steps can be performed simultaneously.
[0165] The aspects of this disclosure have been described above with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this disclosure. It should be understood that each block in the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing apparatus to produce a machine such that these instructions, executable via the processor of the computer or other programmable data processing apparatus, enable the implementation of the functions / actions specified in one or more blocks of the flowchart illustrations and / or block diagrams. Such a processor can be, but is not limited to, a general-purpose processor, a special-purpose processor, a special application processor, or a field-programmable logic circuit. It is also understood that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can also be implemented by special-purpose hardware performing the specified functions or actions, or can be implemented by a combination of special-purpose hardware and computer instructions.
[0166] The above description is merely a specific implementation of this application. Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, modules, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here. It should be understood that the protection scope of this application is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in this application, and these modifications or substitutions should all be covered within the protection scope of this application.
Claims
1. A database query routing optimization method, characterized in that, include: Based on the natural language query text, the pattern structure information of the database, and the target historical entries in the execution feedback memory, a routing state vector is constructed. The target historical entries are historical entries in the execution feedback memory that have a matching degree with the natural language query text that exceeds a preset matching degree. The historical entries represent the mapping information between the routing decisions and execution results of historical database queries. Based on the routing state vector, the activation distribution of each expert to be activated is determined through the routing policy network, and the target expert is determined from the experts to be activated based on the activation distribution and the number of dynamic experts. Based on the target expert's processing of the routing state vector, a query plan is obtained, and the query plan is executed on the database to obtain the execution result; The execution results are evaluated hierarchically according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution results; The expected state value and expected action value corresponding to the routing state vector are determined by the utility evaluation network. The routing policy network is iteratively optimized based on the expected state value, the expected action value, and the execution utility value to obtain an updated routing policy network, and the activation distribution is adjusted based on the updated routing policy network.
2. The method according to claim 1, characterized in that, After constructing the routing state vector based on the natural language query text, the database's pattern structure information, and the target history entries in the execution feedback memory, the following is also included: If the total number of historical entries in the execution feedback memory exceeds a preset entry number threshold, the following steps are performed based on the routing state vector: determining the activation distribution of each expert to be activated through the routing policy network, and determining the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. If the total number of historical entries in the execution feedback memory does not exceed the preset entry number threshold, the keyword features in the natural language query text are determined. By using a preset rule classifier, the activation weight of each expert to be activated is determined based on the weight mapping relationship between the keyword features and the preset expert weights, and the target expert is determined based on the activation weights.
3. The method according to claim 1, characterized in that, The step of performing a hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution results includes: The execution results are analyzed to obtain the quality score and error type corresponding to the execution results; If the quality score exceeds a first scoring threshold, the execution utility value is determined based on the high-quality evaluation formula and the quality score. If the quality score does not exceed the first scoring threshold but exceeds the second scoring threshold, the execution utility value is determined based on the medium quality assessment formula and the quality score. If the quality score does not exceed the second score threshold, a penalty value is determined based on the error type, and the penalty value is determined as the execution utility value.
4. The method according to claim 1, characterized in that, The step of determining the expected state value and expected action value corresponding to the routing state vector through the utility evaluation network includes: The routing state vector is processed by a state value network in a utility evaluation network to obtain the expected state value of the routing state vector. The desired action value is obtained by processing the routing state vector and the activation distribution through the action value network in the utility evaluation network.
5. The method according to claim 1, characterized in that, The iterative optimization of the routing policy network based on the expected state value, the expected action value, and the execution utility value to obtain the updated routing policy network includes: The expected execution utility value is determined based on the expected state value and the expected action value. The advantage evaluation signal is determined based on the difference between the expected execution utility value and the execution utility value; The policy gradient loss is determined based on the advantage evaluation signal and the activation distribution; The parameter gradient of the routing policy network is determined by the policy gradient loss, and the routing policy network is updated along the gradient direction of the parameter gradient to obtain the updated routing policy network.
6. The method according to claim 1, characterized in that, After performing a hierarchical utility evaluation on the execution result according to a preset hierarchical evaluation method to obtain the execution utility value of the routing policy network for the execution result, the method further includes: The entries to be stored are determined based on the natural language query text, the activation distribution, the query plan, and the execution result; The value of the items to be stored is evaluated according to the preset memory evaluation dimensions to obtain a multi-dimensional evaluation result. The preset memory evaluation dimensions include at least one of the following: novelty dimension, database structure adaptability dimension, routing decision confidence dimension, and timeliness dimension. The multi-dimensional evaluation results are weighted and summed to obtain the comprehensive memory value of the item to be stored; If the total memory value exceeds a preset storage threshold, the entry to be stored will be stored in the execution feedback memory.
7. The method according to claim 6, characterized in that, The step of storing the entry to be stored into the execution feedback memory includes: If the overall memory value meets the preset high-value condition, the entry to be stored will be stored in the mid-term high-value pool of the execution feedback memory. If the overall memory value does not meet the preset high-value condition, the entry to be stored will be stored in the short-term calibration buffer pool of the execution feedback memory.
8. The method according to claim 1, characterized in that, The step of processing the routing state vector based on the target expert to obtain a query plan includes: The routing state vector is input into each of the target experts to obtain the sub-inference results corresponding to the target experts; Obtain the activation weights of each target expert in the activation distribution, perform a weighted summation on the sub-inference results based on the activation weights, and determine the weighted summation result as the query plan.
9. A database query routing optimization device, characterized in that, include: The construction module is used to construct a routing state vector based on natural language query text, database pattern structure information, and target historical entries in the execution feedback memory. The target historical entries are historical entries in the execution feedback memory that have a matching degree with the natural language query text that exceeds a preset matching degree. The historical entries represent the mapping information between the routing decisions and execution results of historical database queries. The activation module is used to determine the activation distribution of each expert to be activated through the routing policy network based on the routing state vector, and to determine the target expert among the experts to be activated based on the activation distribution and the number of dynamic experts. The query module is used to process the routing state vector based on the target expert, obtain a query plan, execute the query plan on the database, and obtain the execution result; The evaluation module is used to perform hierarchical utility evaluation on the execution results according to a preset hierarchical evaluation method, so as to obtain the execution utility value of the routing policy network on the execution results; The expectation module is used to determine the expected state value and expected action value corresponding to the routing state vector through the utility evaluation network; An update module is used to iteratively optimize the routing policy network based on the expected state value, the expected action value, and the execution utility value to obtain an updated routing policy network, and to adjust the activation distribution based on the updated routing policy network.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions, which, when executed by a processor, implement the database query routing optimization method as described in any one of claims 1-8.
