Natural language intelligent analysis and data query instruction generation method based on large model
By constructing a dynamically enhanced query semantic graph and introducing external knowledge graphs, and combining deep reinforcement learning to optimize the query execution plan, the accuracy and efficiency issues in the conversion of natural language to database queries are solved, achieving efficient query intent understanding and execution.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-25
- Publication Date
- 2026-04-07
AI Technical Summary
Existing natural language to database query conversion technologies have low accuracy when handling complex semantic queries, struggle to understand the user's true intent, and have poor query execution efficiency, failing to meet the needs of real-time interaction.
We employ a large-model-based natural language intelligent parsing method to construct a dynamically enhanced query semantic graph, introduce external knowledge graphs for semantic enhancement, and optimize the query execution plan through deep reinforcement learning to generate the optimal execution plan.
It achieves accurate understanding of natural language query intent and effective identification of complex query conditions, improves the accuracy and efficiency of query semantic parsing, reduces system resource consumption, and enhances user experience.
Smart Images

Figure CN121188079B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to natural language processing technology, and more particularly to a method for intelligent parsing of natural language and generation of data query instructions based on large models. Background Technology
[0002] In recent years, large-scale pre-trained language models have made groundbreaking progress in the field of natural language processing, providing a new technical path for the conversion of natural language into database queries. Meanwhile, the development of knowledge graph technology has also provided rich external knowledge support for query semantic understanding. Combining natural language processing, knowledge graphs, and database query technologies to build intelligent data query systems has become a current research hotspot.
[0003] However, existing natural language to database query conversion technologies still have significant shortcomings: They suffer from low accuracy when handling complex semantic queries, especially those containing implicit conditions, multi-level nested relationships, or domain-specific terminology, often failing to accurately understand the user's true intent. The lack of deep semantic understanding and enhancement mechanisms makes it difficult to handle ambiguity and incompleteness in query statements, leading to deviations between generated query instructions and the user's actual needs. Furthermore, insufficient consideration is given to query execution efficiency optimization, failing to dynamically adjust query strategies based on data characteristics and query patterns, resulting in poor query performance in large-scale data environments and an inability to meet real-time interactive requirements. Summary of the Invention
[0004] This invention provides a method for intelligent parsing of natural language and generation of data query instructions based on a large model, which can solve the problems in the prior art.
[0005] A first aspect of this invention provides a method for intelligent natural language parsing and data query instruction generation based on a large model, comprising:
[0006] The system receives natural language query statements from users and inputs them into a pre-trained large-scale language model to perform semantic parsing and obtain semantic parsing results.
[0007] Based on the semantic parsing results, a dynamically enhanced query semantic graph is constructed. The query semantic graph includes query entity nodes, query relationship edges, and attribute constraint nodes. An external knowledge graph is introduced for semantic enhancement. The external knowledge graph includes a domain ontology library and a concept hierarchy tree.
[0008] Extract the structured feature vector of the query semantic graph, calculate the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and map the query semantic graph into a database query syntax tree based on the semantic similarity calculation results;
[0009] An intermediate query representation is generated based on the database query syntax tree. An optimization strategy based on deep reinforcement learning is used to optimize the execution plan of the intermediate query representation. The query execution time and resource consumption are used as reward signals to generate an optimal execution plan. The intermediate query representation is converted into a standardized database query instruction according to the optimal execution plan. The standardized database query instruction is executed to obtain the query results and convert them into a query response in natural language form.
[0010] In one alternative implementation,
[0011] The steps for receiving natural language query statements from users and inputting them into a pre-trained large-scale language model to perform semantic parsing and obtain semantic parsing results include:
[0012] The natural language query statement is input into the pre-trained large-scale language model to extract the deep semantic representation of the natural language query statement;
[0013] Based on the deep semantic representation, the query intent is identified and the semantic slots are filled to obtain the query intent result and the slot filling result. The query intent result and the slot filling result are fused to construct a query semantic framework including the main query object, query conditions and query target. The semantic consistency of the query semantic framework is verified to generate semantic parsing results.
[0014] In one alternative implementation,
[0015] The steps for semantic enhancement by introducing external knowledge graphs include:
[0016] The query semantic graph is mapped using conceptual knowledge from the domain ontology library. The concept mapping is achieved by calculating the similarity between concepts. The similarity between concepts is obtained by weighting the longest common subsequence distance and the vector space representation. Based on the concept mapping result, the query semantic graph is expanded with upper and lower level concepts according to the hierarchical association strength calculated in the concept hierarchy tree. The hierarchical association strength is calculated based on the depth of the concept in the hierarchy tree and the nearest common ancestor node.
[0017] The query semantic graph, which has been expanded by hierarchical concepts, is structurally expanded. Missing entity nodes and attribute nodes are supplemented based on contextual association. By establishing database query relationship paths between entity nodes, the semantic conceptual relationships are converted into database query relationships. The converted database query relationships are then updated into the relationship edge structure of the query semantic graph.
[0018] The query semantic graph after structural expansion is constrained and optimized. The constraint priority is calculated based on the constraint value range and the degree of class specialization. Constraints are merged according to the constraint priority. Semantic enhancement is completed when the evaluation score of the constraint-optimized query semantic graph reaches a preset scoring threshold.
[0019] In one alternative implementation,
[0020] The query semantic graph is expanded with hierarchical concepts based on the hierarchical association strength calculated in the concept hierarchy tree. The step of calculating the hierarchical association strength based on the depth of the concept in the hierarchy tree and the nearest common ancestor node includes:
[0021] The nodes in the concept hierarchy tree represent concepts, and the edges in the concept hierarchy tree represent hierarchical relationships. The depth weight is calculated using the sigmoid function based on the depth of the concept node, and the nearest common ancestor weight is calculated based on the ratio of the depth of the nearest common ancestor node to the maximum depth of the concept node. The hierarchical association strength is obtained by weighting the depth weight and the nearest common ancestor weight using a balancing factor.
[0022] From the concept hierarchy tree, we obtain extended candidate concepts of concept nodes. When the hierarchical association strength of the extended candidate concept is greater than the association strength threshold, it is determined as an extended concept, and the node corresponding to the extended concept is added to the query semantic graph.
[0023] Calculate the evaluation metrics for the extended concept, including semantic consistency and extended coverage based on hierarchical association strength; iteratively adjust the balance factor and the association strength threshold based on the evaluation metrics, and stop adjusting when the improvement value of the evaluation metrics is lower than a preset threshold.
[0024] In one alternative implementation,
[0025] The steps of extracting the structured feature vector of the query semantic graph, calculating the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and mapping the query semantic graph to a database query syntax tree based on the semantic similarity calculation results include:
[0026] The node feature vector and edge feature vector of the query semantic graph are constructed. The node feature vector is obtained by weighted combination of low-dimensional dense vector representation of node attributes and node position encoding vector. The edge feature vector is obtained by feature extraction based on edge type and direction information. The node feature vector and the edge feature vector are combined to obtain the structured feature vector of the query semantic graph.
[0027] The templates in the pre-built database query template library are represented as corresponding structured feature vectors. The multi-dimensional similarity between the structured feature vector of the query semantic graph and the structured feature vector of the template is calculated. The multi-dimensional similarity is obtained by weighted combination of semantic similarity of node features, connection relationship similarity of edge features, and matching degree of query constraints.
[0028] The optimal matching template is selected based on the multi-dimensional similarity. An initial syntax tree is constructed based on the mapping relationship between the structured feature vector of the optimal matching template and the structured feature vector of the query semantic graph. The initial syntax tree is then structurally optimized to obtain the database query syntax tree.
[0029] In one alternative implementation,
[0030] The steps of generating an intermediate query representation based on the database query syntax tree, optimizing the execution plan of the intermediate query representation using a deep reinforcement learning-based optimization strategy, and using query execution time and resource consumption as reward signals to generate the optimal execution plan include:
[0031] The database query syntax tree is converted into an intermediate query representation, and a state vector containing query operation features and resource usage status is constructed based on the intermediate query representation.
[0032] Construct a state space and action space for a deep reinforcement learning network. The state space includes the state vector, and the action space includes physical operator selection actions, execution order adjustment actions, and parallelism configuration actions. Construct a composite reward function based on a weighted combination of query execution time and resource consumption.
[0033] The state vector is encoded using a multilayer perceptron to obtain a state code. An Actor network and a Critic network are trained based on the state code. The Actor network outputs an action probability distribution, and the Critic network evaluates the state value. A policy gradient algorithm is used to jointly optimize the Actor network and the Critic network according to the composite reward function, and historical optimization experience is saved to an experience pool.
[0034] The execution plan generated based on the Actor network and the Critic network is monitored in real time. When the performance index exceeds the preset index threshold, the plan is adjusted. The execution plan is adjusted based on the local optimization method to maintain a smooth transition of execution. The optimization experience of the execution results is fed back to the experience pool to update the deep reinforcement learning network online. The optimal execution plan is generated based on the updated deep reinforcement learning network.
[0035] In one alternative implementation,
[0036] The steps of encoding the state vector using a multilayer perceptron to obtain a state code, and training the Actor network and Critic network based on the state code include:
[0037] The state vector is input into a multilayer perceptron, and the initial state code is obtained through nonlinear transformation.
[0038] Local attention weights are calculated based on adjacent temporal states, and global attention weights are calculated based on historical states. The local attention weights and global attention weights are adaptively fused to obtain combined attention weights. A temporal memory is constructed based on the combined attention weights, and the state information in the temporal memory is sorted by importance and updated periodically according to the combined attention weights. The initial state code is associated and matched with the state information in the temporal memory to generate an enhanced state code that considers historical state transitions.
[0039] The Actor network and the Critic network are trained based on the enhanced state encoding, and the network update results are fed back to dynamically adjust the ratio of the local attention weights and the global attention weights, thereby optimizing the update strategy of the temporal memory.
[0040] A second aspect of the present invention provides an electronic device, comprising:
[0041] processor;
[0042] Memory used to store processor-executable instructions;
[0043] The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.
[0044] A third aspect of the present invention provides a computer-readable storage medium having stored thereon computer program instructions that, when executed by a processor, implement the aforementioned method.
[0045] This invention performs semantic parsing using a large-scale language model and constructs a dynamically enhanced query semantic graph. It also introduces an external knowledge graph for semantic enhancement, thereby achieving accurate understanding of natural language query intent and effective identification of complex query conditions, and improving the accuracy of query semantic parsing.
[0046] This invention calculates semantic similarity based on structured feature vectors, maps the query semantic graph to a database query syntax tree, and adopts an intermediate query representation, enabling the system to adapt to different database dialects and query requirements, thereby improving the system's scalability and versatility.
[0047] This invention employs a deep reinforcement learning-based optimization strategy to optimize the query execution plan, using query execution time and resource consumption as reward signals to generate the optimal execution plan. This significantly improves query efficiency, reduces system resource consumption, and presents query results in natural language, enhancing the user experience. Attached Figure Description
[0048] Figure 1 This is a flowchart illustrating the method for generating intelligent natural language parsing and data query instructions based on a large model according to an embodiment of the present invention.
[0049] Figure 2 A flowchart for semantic enhancement by introducing external knowledge graphs. Detailed Implementation
[0050] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0051] The technical solution of the present invention will be described in detail below with reference to specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0052] Figure 1 This is a flowchart illustrating the natural language intelligent parsing and data query instruction generation method based on a large model according to an embodiment of the present invention. Figure 1 As shown, the method includes:
[0053] The system receives natural language query statements from users and inputs them into a pre-trained large-scale language model to perform semantic parsing and obtain semantic parsing results.
[0054] Based on the semantic parsing results, a dynamically enhanced query semantic graph is constructed. The query semantic graph includes query entity nodes, query relationship edges, and attribute constraint nodes. An external knowledge graph is introduced for semantic enhancement. The external knowledge graph includes a domain ontology library and a concept hierarchy tree.
[0055] Extract the structured feature vector of the query semantic graph, calculate the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and map the query semantic graph into a database query syntax tree based on the semantic similarity calculation results;
[0056] An intermediate query representation is generated based on the database query syntax tree. An optimization strategy based on deep reinforcement learning is used to optimize the execution plan of the intermediate query representation. The query execution time and resource consumption are used as reward signals to generate an optimal execution plan. The intermediate query representation is converted into a standardized database query instruction according to the optimal execution plan. The standardized database query instruction is executed to obtain the query results and convert them into a query response in natural language form.
[0057] This embodiment details a method for intelligent natural language parsing and data query instruction generation based on a large model. This method achieves automatic conversion from user natural language queries to database query instructions, involving key technologies such as semantic parsing, query semantic graph construction, template matching, and execution plan optimization.
[0058] Receiving and semantically parsing user-input natural language queries is the first step in converting natural language into database queries. A pre-trained large-scale language model is used as the semantic parsing engine. This model, based on the Transformer architecture, contains multiple layers of encoders and decoders with a parameter scale of 175 billion. The model captures long-distance dependencies in the query statement through a self-attention mechanism, understanding complex semantic structures. When a user inputs the query "find the top ten best-selling gaming laptops and their prices," it is segmented and converted into input embeddings, which are then processed by the model to obtain a semantic representation. The semantic parsing result includes two parts: intent recognition and slot filling. The intent is recognized as "query product information," and the slots include product type "gaming laptops," sorting attribute "sales volume," sorting direction "descending," quantity limit "10," and return attribute "price." It can also recognize nested relationships and conditional logic in complex queries, such as the double condition constraint in "products with prices below 8000 and ratings above 4.5." During semantic parsing, for ambiguous expressions such as "high cost-performance ratio," it can be transformed into specific query condition combinations through contextual understanding.
[0059] Constructing a dynamically enhanced query semantic graph based on semantic parsing results serves as a bridge connecting natural language understanding and database querying. The query semantic graph adopts a directed graph structure, containing three key elements: query entity nodes representing the core object of the query, such as "gaming laptop"; query relation edges representing semantic relationships between nodes, such as "has attributes" and "is constrained"; and attribute constraint nodes representing query conditions and returned attributes, such as "sales volume" and "price". First, an initial semantic graph is constructed based on the semantic parsing results, and then an external knowledge graph is introduced for semantic enhancement. The external knowledge graph consists of two parts: a domain ontology and a concept hierarchy tree. The domain ontology stores domain-specific concepts, relationships, and rules, such as product classifications, attribute definitions, and attribute value ranges in the electronics field. The concept hierarchy tree represents the hierarchical relationships between concepts, such as "gaming laptop" being a subclass of "laptop," and "laptop" being a subclass of "computer." The semantic enhancement process includes three steps: entity alignment, relation expansion, and constraint completion. Entity alignment matches entities in the query with concepts in the knowledge graph, such as matching "gaming laptop" to the standard term "gaming laptop". Relationship expansion adds implicit semantic relationships, such as adding a descending sorting direction to "top ten in sales". Constraint completion adds implicit query conditions, such as adding the category condition "category='gaming laptops'" to "gaming laptops". Through semantic enhancement, the initial query semantic graph evolves into an enhanced semantic graph containing richer and more accurate semantic information, providing a foundation for subsequent template matching and query generation.
[0060] A structured feature vector for the query semantic graph is constructed, consisting of node feature vectors and edge feature vectors. The node feature vector is obtained by weighted combination of low-dimensional dense vector representations of node attributes and node position encoding vectors, while the edge feature vector is extracted based on edge type and direction information. For the "gaming laptop" node, it is mapped to a 128-dimensional vector using a pre-trained word embedding model and weighted combined with the encoding vector representing its graph structure position. All node and edge feature vectors are combined into a unified structured feature vector to comprehensively express the structure and semantic information of the query semantic graph. A pre-built database query template library is constructed, with each template corresponding to a typical query scenario and stored in the form of structured feature vectors. The multi-dimensional similarity between the query semantic graph's feature vector and each template in the template library is calculated, including semantic similarity of node features, similarity of edge feature connections, and matching degree of query constraints. Based on the similarity calculation results, the optimal matching template is selected, and an initial database query syntax tree is constructed according to the mapping relationship between the query semantic graph and the template. For the example query, select the "Sort Query Template" and map "Gaming Laptops" to the table name "product", "Sales" to the sorting condition "sales_volume", and "Top Ten" to the constraint "LIMIT 10". The initial syntax tree will also be structurally optimized, including syntax validation, table join optimization, and query condition rewriting, ultimately generating a query syntax tree that conforms to database syntax rules and has high execution efficiency.
[0061] The query syntax tree is transformed into an intermediate query representation, which adopts a tree structure, with each node representing a query operation such as table scan, filtering, join, and aggregation. Based on the intermediate query representation, a state vector containing query operation features and resource usage status is constructed as input for deep reinforcement learning. An action space including physical operator selection, execution order adjustment, and parallelism configuration is designed, and the optimal decision-making strategy is learned through a deep reinforcement learning network. The deep reinforcement learning network includes an Actor network and a Critic network. The Actor network generates the action probability distribution, and the Critic network evaluates the state value. A multilayer perceptron is used to encode the state vector, and a temporal memory is built based on local attention and global attention mechanisms to generate enhanced state codes. The reinforcement learning process uses a weighted combination of query execution time and resource consumption as a reward signal to optimize execution decisions. The generated execution plan is monitored in real time. When the performance index exceeds a preset threshold, the plan is adjusted, and the optimization experience is fed back to the experience pool to continuously improve the optimization strategy. According to the optimal execution plan, the intermediate query representation is converted into standardized database query instructions, such as SQL, SPARQL, or MongoDB query statements, and the query is executed to obtain the results. Finally, the query results are converted into a natural language query response, such as "We found the top ten best-selling gaming laptops, with prices as follows...", to present the information in a user-friendly way.
[0062] In one optional implementation, the steps of receiving a natural language query statement input by the user and feeding it into a pre-trained large-scale language model to perform semantic parsing and obtain the semantic parsing result include:
[0063] The natural language query statement is input into the pre-trained large-scale language model to extract the deep semantic representation of the natural language query statement;
[0064] Based on the deep semantic representation, the query intent is identified and the semantic slots are filled to obtain the query intent result and the slot filling result. The query intent result and the slot filling result are fused to construct a query semantic framework including the main query object, query conditions and query target. The semantic consistency of the query semantic framework is verified to generate semantic parsing results.
[0065] For example, a natural language query input from a user is received and fed into a pre-trained large-scale language model. In practical applications, the pre-trained large-scale language model can be an encoder-decoder model based on the Transformer architecture, which has been pre-trained on a large-scale text corpus and possesses powerful semantic understanding capabilities. When a user inputs a query such as "query electronic products with sales exceeding 1 million in 2023," it is fed into the pre-trained model.
[0066] When pre-trained models extract deep semantic representations of natural language queries, they segment the input query into words, generating a sequence of tokens. Then, a multi-layered self-attention mechanism captures the contextual relationships between these tokens. For the example query above, it identifies keywords such as "2023," "sales," "exceeding," "1 million," and "electronic products," and analyzes their dependencies. The model generates a high-dimensional vector representation containing semantic information from the query, including time constraints, numerical comparisons, and product categories. This vector representation typically has a dimension between 512 and 1024, effectively capturing the semantic features of the query.
[0067] In the process of identifying query intent and filling semantic slots based on deep semantic representation, a joint model of intent classification and slot filling is used. For intent identification, a fully connected layer maps the deep semantic representation to a predefined intent space, such as "product query," "sales statistics," and "inventory check." For the example query, the main intent is identified as "product query," and the sub-intent is "sales statistics." For slot filling, a sequence labeling method is used to map each lexical unit to the corresponding slot type. In the example, "2023" is labeled as a "time slot," "sales revenue" is labeled as an "attribute slot," "over 1 million" is labeled as a "condition slot," and "electronic products" is labeled as an "object slot." Relationships between slots are also extracted, such as a "numerical comparison" relationship between "sales revenue" and "over 1 million." To improve the accuracy of slot filling, part-of-speech information and entity recognition results are combined to ensure that special types of information such as time, numerical values, and product names are correctly extracted.
[0068] The identified intent serves as the basic structure of the framework, while the filled slots represent the specific content. For the example query, the constructed semantic framework includes: the main query object is "electronic products," the query conditions are "sales > 1 million AND time = 2023," and the query target is "a list of electronic products." A tree structure is used to represent the query semantic framework, where the root node represents the query intent, and the child nodes represent the slots and their relationships. During framework construction, implicit information is handled. For example, when a user only says "search for best-selling electronic products," the implicit time range (such as "current quarter") is supplemented based on the context and default settings. Ambiguity is also handled; for example, "high-end" in "high-end electronic products" refers to high price or high performance, and the specific meaning is determined through context or user history preferences.
[0069] The query semantic framework undergoes semantic consistency verification, checking for logical conflicts among its components, such as the reasonableness of the time range, the validity of numerical comparisons, and the existence of product categories. It also verifies whether the relationships between slots conform to domain knowledge, such as whether the "sales revenue" attribute is applicable to the "electronic products" object. For example, for the sample query, it verifies whether "2023" is a valid time period, whether "1 million" is a reasonable sales revenue threshold, and whether "electronic products" is a supported product category. Upon successful verification, the final semantic parsing result is generated, containing the complete query intent, conditional relationships, and query target, forming a structured query representation. This representation can be directly converted into subsequent database query language or knowledge graph queries.
[0070] This invention extracts deep semantics through a pre-trained large-scale language model and constructs a query semantic framework by combining intent recognition and slot filling, achieving accurate understanding of complex natural language queries. This method effectively handles implicit information and semantic ambiguity, improving query accuracy.
[0071] In one alternative implementation, the step of introducing an external knowledge graph for semantic enhancement includes:
[0072] The query semantic graph is mapped using conceptual knowledge from the domain ontology library. The concept mapping is achieved by calculating the similarity between concepts. The similarity between concepts is obtained by weighting the longest common subsequence distance and the vector space representation. Based on the concept mapping result, the query semantic graph is expanded with upper and lower level concepts according to the hierarchical association strength calculated in the concept hierarchy tree. The hierarchical association strength is calculated based on the depth of the concept in the hierarchy tree and the nearest common ancestor node.
[0073] The query semantic graph, which has been expanded by hierarchical concepts, is structurally expanded. Missing entity nodes and attribute nodes are supplemented based on contextual association. By establishing database query relationship paths between entity nodes, the semantic conceptual relationships are converted into database query relationships. The converted database query relationships are then updated into the relationship edge structure of the query semantic graph.
[0074] The query semantic graph after structural expansion is constrained and optimized. The constraint priority is calculated based on the constraint value range and the degree of class specialization. Constraints are merged according to the constraint priority. Semantic enhancement is completed when the evaluation score of the constraint-optimized query semantic graph reaches a preset scoring threshold.
[0075] Combination Figure 2The flowchart illustrating semantic enhancement using an external knowledge graph is provided as an example. For instance, after obtaining the query semantic graph, it needs to be semantically enhanced using an external knowledge graph to improve the accuracy and completeness of query understanding. Assume a user inputs the query "laptops with good recent sales," which has been parsed into an initial query semantic graph containing "laptops" as the main node, "good sales" as the constraint node, and "recent" as the time constraint node.
[0076] When mapping concepts from the domain ontology to the query semantic graph, the concepts in the query semantic graph are matched with standard concepts in the domain ontology. The domain ontology stores a professional concept system for the electronics product domain; for example, "laptop" is associated with concepts like "portable computing device" and "computer." The concept mapping process is achieved by calculating the similarity between concepts. This similarity is calculated and weighted using two methods: longest common subsequence distance and vector space representation. The longest common subsequence distance method compares two concept strings, finds the longest common subsequence length, and divides it by the length of the longer string to obtain the similarity value. For example, the longest common subsequence of "laptop" and "portable computer" is "computer," with a length of 3, compared to the length of "portable computer" (7), resulting in a string similarity of approximately 0.43. Vector space representation uses a pre-trained word embedding model (such as a 300-dimensional word vector model) to map concepts to a high-dimensional semantic space and calculates the vector cosine similarity. For the same pair of concepts, the vector space similarity is 0.85. The final concept similarity was calculated using the weighted formula: Similarity = 0.3 × String Similarity + 0.7 × Vector Space Similarity, resulting in 0.72. A similarity threshold of 0.6 was set; a mapping relationship was confirmed when the calculated result exceeded this threshold. After mapping, "laptop" was precisely mapped to the standard concept "portable computer" in the ontology library.
[0077] Based on the concept mapping results, the query semantic graph is expanded with hierarchical concepts using the concept hierarchy tree in the ontology. A concept hierarchy tree is a tree structure representing the hierarchical relationships between concepts, where each node represents a concept, and edges between nodes represent hierarchical relationships. For example, "computer" is the root node, "desktop computer" and "portable computer" are its child nodes, while "ultrabook," "gaming laptop," and "business laptop" are child nodes of "portable computer." The strength of the hierarchical association between concepts is calculated, determined as follows: concepts C1 and C2 are set to depths d1 and d2 in the hierarchy tree, respectively, and their nearest common ancestor node has a depth of d. lca Then the hierarchical association strength = 1 - (d1 + d2 - 2 × d) lca() / (maximum tree depth). Taking "portable computer" (depth 2) and "ultrabook" (depth 3) as examples, their lowest common ancestor is "portable computer" (depth 2). Assuming the maximum tree depth is 5, their hierarchical association strength = 1 - (2 + 3 - 2 × 2) / 5 = 0.8. A hierarchical association strength threshold of 0.6 is set, and concept expansion is performed when the calculated result exceeds this threshold. Based on the calculation results, "ultrabook", "gaming laptop", and "business laptop" (hierarchical association strengths of 0.8, 0.8, and 0.8 respectively) are added as subordinate concepts to the query semantic graph, while "computer" (hierarchical association strength of 0.6) is added as a superordinate concept to expand the semantic coverage of the query. For the fuzzy concept of "good sales", it is concretized as "monthly sales > 500 units" through the attribute mapping table in the ontology, enhancing semantic accuracy.
[0078] When structurally expanding the query semantic graph after hierarchical concept expansion, missing entity and attribute nodes are supplemented based on contextual relationships. Contextual relationships are determined by the entity-attribute association matrix in the domain knowledge base, which records the sets of attributes typically associated with different entity types and their association strength. In the electronics product domain, "portable computer" is typically highly correlated with attribute nodes such as "processor model," "memory capacity," "graphics card model," and "battery life" (association strength > 0.8). Based on an association strength threshold of 0.7, these attribute nodes are supplemented into the query semantic graph. By establishing database query relationship paths between entity nodes, semantic-level conceptual relationships are converted into executable database query relationships. This conversion process relies on a pre-established semantic relationship-database operation mapping table, which defines the database table joins and condition generation rules corresponding to different semantic relationships. For example, the "have" semantic relationship between "portable computer" and "sales volume" is converted into a data table join: "SELECT product.*, sales.quantity FROM product JOIN sales ON product.id = sales.product_id WHERE product.category = 'portable computer'". Similarly, hierarchical relationships are converted into IN operations: "product.subcategory IN ('Ultrabook', 'Gaming Laptop', 'Business Laptop')". These converted database query relationships are updated in the relation edge structure of the query semantic graph, enabling the query semantic graph to possess both semantic expression and database operation capabilities. For the time constraint "recent", it is concretized into "past 30 days" through the time expression parsing module and converted into the database query condition "sales.date>=DATEADD(day, -30, GETDATE())".
[0079] Constraint priority is calculated based on the constraint value range and the degree of class specialization. The constraint value range is measured by the normalized range width: range value = (maximum value - minimum value) / (domain maximum value - domain minimum value), and priority is inversely proportional to the range value. The degree of class specialization is determined by the concept's depth in the hierarchy tree: specialization degree = current depth / maximum depth. The formula for calculating constraint priority is: priority = 0.6 × (1 - range value) + 0.4 × specialization degree. For "monthly sales > 500 units", its range value is (infinity - 500) / (infinity - 0) ≈ 0.5 (assuming the maximum sales are uncertain), and the specialization degree is the standard depth ratio of the attribute node, which is approximately 0.7. The calculated priority is 0.6 × (1 - 0.5) + 0.4 × 0.7 = 0.58. For "past 30 days", the range is 30 / 365≈0.082, the specialization level is 0.8, and the calculated priority is 0.6×(1-0.082)+0.4×0.8=0.87. Based on these priority values, constraints with a priority difference of less than 0.2 are merged to generate an optimized constraint expression. An evaluation score is calculated for the optimized query semantic graph, which is obtained by weighting three indicators: semantic completeness (weight 0.4) measures whether the query semantic graph contains the necessary entities, attributes, and relationships; query executability (weight 0.4) measures whether the transformed database query can be executed effectively; and concept coverage (weight 0.2) measures whether the scope of concepts covered by the query is sufficient. Each indicator ranges from 0 to 1, and the final evaluation score is the weighted sum of the three. When the evaluation score reaches the preset scoring threshold of 0.75, the semantic enhancement is considered to be complete. The final generated query semantic graph includes the main concept "portable computer" and its subordinate concepts "ultrabook", "gaming laptop" and "business laptop", the constraint "monthly sales volume in the past 30 days > 500 units", and the complete database query path.
[0080] This invention effectively solves the problems of conceptual ambiguity and semantic incompleteness in natural language queries through four stages: concept mapping, hierarchical expansion, structural expansion, and constraint optimization. It can accurately identify user intent, supplement implicit information, and transform abstract semantics into executable database queries, significantly improving query accuracy and response efficiency, and providing strong support for intelligent question answering and data retrieval applications.
[0081] In one optional implementation, the step of expanding the query semantic graph with hierarchical concepts based on the hierarchical association strength calculated in the concept hierarchy tree, wherein the hierarchical association strength is calculated based on the depth of the concept in the hierarchy tree and the nearest common ancestor node, includes:
[0082] The nodes in the concept hierarchy tree represent concepts, and the edges in the concept hierarchy tree represent hierarchical relationships. The depth weight is calculated using the sigmoid function based on the depth of the concept node, and the nearest common ancestor weight is calculated based on the ratio of the depth of the nearest common ancestor node to the maximum depth of the concept node. The hierarchical association strength is obtained by weighting the depth weight and the nearest common ancestor weight using a balancing factor.
[0083] From the concept hierarchy tree, we obtain extended candidate concepts of concept nodes. When the hierarchical association strength of the extended candidate concept is greater than the association strength threshold, it is determined as an extended concept, and the node corresponding to the extended concept is added to the query semantic graph.
[0084] Calculate the evaluation metrics for the extended concept, including semantic consistency and extended coverage based on hierarchical association strength; iteratively adjust the balance factor and the association strength threshold based on the evaluation metrics, and stop adjusting when the improvement value of the evaluation metrics is lower than a preset threshold.
[0085] For example, once the query semantic graph has established a correspondence with standard concepts in the domain ontology through concept mapping, it is necessary to further expand the relevant hierarchical concepts using a concept hierarchy tree to enrich the query semantics. Suppose a user queries "high-performance laptop", after concept mapping, the query semantic graph includes the standard concept "high-performance portable computer".
[0086] A concept hierarchy tree is a tree-like structure that represents the hierarchical relationships between concepts, where nodes represent concepts and edges represent hierarchical relationships. In the concept hierarchy tree of the electronics product field, the root node is "electronic device," the second level includes concepts such as "computing device" and "communication device," the third level "computing device" includes "desktop computer" and "portable computer," and the fourth level "portable computer" includes more specific concepts such as "business laptop," "gaming laptop," and "thin laptop." It is necessary to calculate the hierarchical association strength between concepts based on this hierarchical tree structure to determine which related concepts should be added to the query semantic graph.
[0087] The first step in calculating the strength of hierarchical associations is to determine the depth weights. For each concept node in the hierarchical tree, its depth value in the tree is recorded. For example, "electronic device" has a depth of 1, "computing device" has a depth of 2, "portable computer" has a depth of 3, and "gaming laptop" has a depth of 4. The greater the depth, the more specific the concept. The depth weights are calculated using the sigmoid function, which involves substituting the depth value into the function: 1 divided by (1 plus the negative depth value of e), resulting in a value between 0 and 1. For "portable computer" (depth 3), its depth weight is 1 divided by (1 plus e to the power of -3), approximately equal to 0.95; for "gaming laptop" (depth 4), its depth weight is 1 divided by (1 plus e to the power of -4), approximately equal to 0.98. The closer the depth weight is to 1, the higher the degree of concept specialization.
[0088] The second step in calculating the strength of hierarchical associations is to determine the weight of the nearest common ancestor (LCA). This requires finding the LCA of two concept nodes and calculating the ratio of its depth to the maximum depth of the hierarchical tree. Taking "portable computer" and "gaming laptop" as an example, their LCA is "portable computer," with a depth of 3. Assuming the maximum depth of the hierarchical tree is 5, the LCA weight is 3 divided by 5, which equals 0.6. A larger LCA weight indicates that the two concepts are semantically closer.
[0089] The final hierarchical association strength is obtained by weighting the depth weight and the nearest common ancestor weight using a balance factor. The balance factor determines the relative importance of the two weights in the calculation, and its value ranges from 0 to 1. Assuming the initial value of the balance factor is set to 0.4, the hierarchical association strength is calculated as: balance factor multiplied by depth weight, plus (1 minus balance factor) multiplied by the nearest common ancestor weight. For "portable computer" and "gaming laptop", the calculation is: 0.4 multiplied by (0.95 plus 0.98) divided by 2, plus 0.6 multiplied by 0.6, resulting in approximately 0.744. This value represents the semantic association degree between the two concepts; the larger the value, the stronger the association.
[0090] When retrieving extended candidate concepts from the concept hierarchy tree, the parent and child nodes of the current concept node are searched. For "high-performance portable computer," its parent concepts include "portable computer" and "computing device," and its child concepts include "high-performance gaming laptop" and "high-performance workstation laptop," etc. The hierarchical association strength between these candidate concepts and the current concept is calculated, and an association strength threshold (e.g., 0.7) is set. When the calculated hierarchical association strength is greater than the threshold, the candidate concept is determined to be an extended concept. For example, the hierarchical association strength between "high-performance portable computer" and "high-performance gaming laptop" is 0.82, which is greater than the threshold of 0.7, so "high-performance gaming laptop" is determined to be an extended concept. Similarly, the calculated hierarchical association strength of "portable computer" is 0.76, which is also greater than the threshold, making it another extended concept. However, the hierarchical association strength of "computing device" is 0.62, which is less than the threshold, and it is not selected as an extended concept. After determining the extended concepts, corresponding nodes are added to the query semantic graph, and the parent-child relationship with the original concept node is established.
[0091] To evaluate the effectiveness of the extensions, evaluation metrics for the extended concepts are calculated, including semantic consistency based on the strength of hierarchical associations and extension coverage. The semantic consistency metric is obtained by calculating the average hierarchical association strength of all extended concepts. For the previous example, the semantic consistency is (0.82 + 0.76) divided by 2, which equals 0.79. The extension coverage metric measures the extent to which the extended concepts cover domain knowledge, calculated as the ratio of the number of extended concepts to the total number of extended concepts. For example, if "high-performance portable computer" has 5 possible hypernyms and hypernyms, and 2 are extended, then the coverage is 2 divided by 5, which equals 0.4.
[0092] Based on the evaluation metrics, the balance factor and association strength threshold are iteratively adjusted. The adjustment strategy is as follows: if semantic consistency is high but coverage is low, the association strength threshold is reduced (e.g., from 0.7 to 0.65); if coverage is high but semantic consistency is low, the balance factor is increased (e.g., from 0.4 to 0.45) to enhance the influence of depth weights. After each adjustment, the hierarchical association strength and extended concepts are recalculated, and the metrics are evaluated again. The adjustment process stops when the improvement value of the metrics in two consecutive evaluations is lower than a preset threshold (e.g., 0.02). Through this adaptive adjustment, reasonable concept expansion coverage can be achieved while maintaining semantic consistency.
[0093] For the example query "high-performance laptop", after the above steps, the query semantic graph is expanded to include the original concept "high-performance portable computer", the higher-level concept "portable computer", and the lower-level concepts "high-performance gaming laptop" and "high-performance workstation laptop". This expansion enables the handling of query requests at different levels of abstraction, such as users being interested in the overall category of "portable computer" or focusing on the specific "high-performance gaming laptop". At the same time, the expanded semantic graph maintains the hierarchical relationships between concepts, ensuring the semantic coherence of the query results.
[0094] This invention calculates hierarchical association strength by weighting the depth weight and the nearest common ancestor weight, thus achieving intelligent concept expansion. It can accurately identify and add semantically related superordinate and subordinate concepts, enrich query semantics, and improve retrieval recall. At the same time, it balances semantic consistency and expansion coverage through an adaptive adjustment mechanism, making the expansion results more in line with the user's query intent.
[0095] In one optional implementation, the steps of extracting the structured feature vector of the query semantic graph, calculating the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and mapping the query semantic graph to a database query syntax tree based on the semantic similarity calculation results include:
[0096] The node feature vector and edge feature vector of the query semantic graph are constructed. The node feature vector is obtained by weighted combination of low-dimensional dense vector representation of node attributes and node position encoding vector. The edge feature vector is obtained by feature extraction based on edge type and direction information. The node feature vector and the edge feature vector are combined to obtain the structured feature vector of the query semantic graph.
[0097] The templates in the pre-built database query template library are represented as corresponding structured feature vectors. The multi-dimensional similarity between the structured feature vector of the query semantic graph and the structured feature vector of the template is calculated. The multi-dimensional similarity is obtained by weighted combination of semantic similarity of node features, connection relationship similarity of edge features, and matching degree of query constraints.
[0098] The optimal matching template is selected based on the multi-dimensional similarity. An initial syntax tree is constructed based on the mapping relationship between the structured feature vector of the optimal matching template and the structured feature vector of the query semantic graph. The initial syntax tree is then structurally optimized to obtain the database query syntax tree.
[0099] For example, the semantically enhanced query semantic graph needs to be converted into a database-executable query syntax tree in order to perform actual data retrieval operations. Suppose a user queries "find the top ten best-selling gaming laptops and their prices", and a query semantic graph has been constructed containing entity nodes "gaming laptops", attribute nodes "sales" and "price", and constraint nodes "top ten".
[0100] Structured feature vectors consist of node feature vectors and edge feature vectors, comprehensively representing the structure and semantic information of the query semantic graph. For constructing node feature vectors, firstly, low-dimensional dense vector representations of node attributes are extracted. Taking the "gaming laptop" node as an example, it is mapped to a 128-dimensional dense vector using a pre-trained word embedding model, capturing the semantic features of the concept. Simultaneously, a positional encoding vector is generated for the node, reflecting its structural position in the semantic graph. The positional encoding vector is calculated using the node's in-degree, out-degree, and distance to core nodes. For example, "gaming laptop," as a core entity node, has a positional encoding of [1,2,0], representing an in-degree of 1, an out-degree of 2, and a distance of 0 from the core node. The final feature vector of a node is obtained by weighted combination of the attribute vector and the positional encoding vector, with weight coefficients typically set to 0.7 and 0.3 to balance semantic and structural information. For the "sales" node, a feature vector is obtained by weighted combination of [0.2,0.5,...,0.3] and [1,0,1].
[0101] The construction of edge feature vectors is based on feature extraction using edge type and direction information. A predefined edge type encoding table is used, such as encoding "entity-attribute" relationships as [1,0,0], "entity-condition" relationships as [0,1,0], and "attribute-value" relationships as [0,0,1]. Edge direction is represented by binary encoding, such as [1,0] for positive and [0,1] for negative. For the edge from "gaming laptops" to "sales," its feature vector is a concatenation of [1,0,0] and [1,0], i.e., [1,0,0,1,0]. All node feature vectors and edge feature vectors are combined in a predefined order to form a structured feature vector for the query semantic graph. For the example query, the final structured feature vector has a dimension of 512, containing feature information from all nodes and edges.
[0102] The pre-built database query template library is a collection of common query templates pre-designed based on historical query data and database structure. This library includes various typical query patterns, such as simple query templates, conditional query templates, sorting query templates, grouping and statistics templates, and multi-table join templates. Each template consists of a template ID, template name, applicable scenario description, structured feature vector, and SQL skeleton. Taking the "sorting query template" as an example, its template ID is "T003," suitable for query scenarios requiring sorting by specific attributes and limiting the number of returned results. The corresponding SQL skeleton is "SELECT {attribute list} FROM {table name} WHERE {condition} ORDERBY {sort attribute} {sort direction} LIMIT {limit number}". The structured feature vector of this template has a dimension of 512, containing key dimension values reflecting the characteristics of the sorting operation, such as sorting-related node vectors [0.7, 0.2, ..., 0.5] and edge vectors [1, 0, 1, ..., 0]. These feature values collectively encode the typical structural pattern of sorting queries. This vectorized representation allows for effective comparison of the structural similarity between the input query and the template.
[0103] The structured feature vectors of the query semantic graph are calculated to achieve multi-dimensional similarity with each template in the template library. This multi-dimensional similarity is obtained by weighted combination of three similarity aspects: semantic similarity of node features, connectivity similarity of edge features, and matching degree of query constraints. Semantic similarity of node features is obtained by calculating the cosine similarity of the corresponding node feature vectors; for example, the similarity between the "gaming laptop" node in the query and the "product" node in the template is 0.85. Connectivity similarity of edge features is calculated by comparing the Hamming distance of the edge feature vectors; for example, the similarity between the "entity-attribute" edge in the query and the corresponding edge in the template is 0.9. The matching degree of query constraints is evaluated based on the matching of constraint types and parameters; for example, the matching degree between the "top ten" constraint and the "TOP N" constraint in the template is 1.0. These three types of similarity are weighted at 0.4, 0.3, and 0.3 respectively and combined to obtain the final multi-dimensional similarity.
[0104] Based on the calculated multi-dimensional similarity, the template with the highest similarity is selected as the optimal matching template. Assuming the multi-dimensional similarity of the "sorting query template" is 0.88, higher than other templates such as the "simple query template" (0.65) and the "grouping statistics template" (0.72), it is selected as the optimal matching template. The corresponding SQL skeleton for this template is: "SELECT [attribute list] FROM [table name] WHERE [condition] ORDER BY [sorting attribute] [sorting direction] LIMIT [limit number]". An initial syntax tree is constructed based on the mapping relationship between the structured feature vector of the optimal matching template and the structured feature vector of the query semantic graph. The mapping process includes two stages: structure mapping and parameter filling. Structure mapping determines the correspondence between nodes and edges in the query semantic graph and components in the SQL syntax tree, such as mapping entity nodes to the FROM clause and attribute nodes to the SELECT or ORDER BY clause. Parameter population involves filling specific values into the corresponding positions in the syntax tree. For example, mapping "gaming laptops" to the table name "product" with the condition "category='gaming laptops'", mapping "sales volume" to the sorting attribute "sales_volume", mapping "top ten" to "LIMIT 10", and mapping "price" to the selection column "price". The initial syntax tree is represented as an internal tree structure and can be serialized into an SQL statement: SELECT product.name, product.price FROM product WHERE product.category='gaming laptops' ORDER BY product.sales_volume DESC LIMIT 10.
[0105] Structural optimization of the initial syntax tree includes syntax validation, table join optimization, and query condition rewriting. Syntax validation ensures that the generated queries conform to database syntax rules, such as checking the existence of table and field names and the correctness of SQL keywords. Table join optimization handles multi-table query scenarios. For example, the example query requires joining the `product` and `sales` tables to retrieve sales data; a join condition is automatically added: `SELECT p.name, p.price FROM product p JOIN saless ON p.id=s.product_id WHERE p.category='gaming laptop' GROUP BY p.id ORDER BY SUM(s.quantity) DESC LIMIT 10`. Query condition rewriting optimizes complex conditions, such as rewriting "price between 5000 and 10000 yuan" as "price BETWEEN 5000 AND 10000" to improve query efficiency. Necessary index hints or query optimizer hints are also added to further optimize the query execution plan. After structural optimization, the final generated database query syntax tree is not only semantically correct, but also optimized in terms of execution efficiency.
[0106] The query semantic graph to database query syntax tree mapping method provided in this embodiment achieves accurate conversion from semantic understanding to executable queries through structured feature vector construction, multi-dimensional similarity calculation, and template matching techniques. This method can accurately capture query intent and structural features, support complex query scenarios, improve the accuracy and efficiency of query conversion, and effectively solve the semantic gap problem between natural language queries and database execution.
[0107] In one optional implementation, the steps of generating an intermediate query representation based on the database query syntax tree, optimizing the execution plan of the intermediate query representation using a deep reinforcement learning-based optimization strategy, and using query execution time and resource consumption as reward signals to generate the optimal execution plan include:
[0108] The database query syntax tree is converted into an intermediate query representation, and a state vector containing query operation features and resource usage status is constructed based on the intermediate query representation.
[0109] Construct a state space and action space for a deep reinforcement learning network. The state space includes the state vector, and the action space includes physical operator selection actions, execution order adjustment actions, and parallelism configuration actions. Construct a composite reward function based on a weighted combination of query execution time and resource consumption.
[0110] The state vector is encoded using a multilayer perceptron to obtain a state code. An Actor network and a Critic network are trained based on the state code. The Actor network outputs an action probability distribution, and the Critic network evaluates the state value. A policy gradient algorithm is used to jointly optimize the Actor network and the Critic network according to the composite reward function, and historical optimization experience is saved to an experience pool.
[0111] The execution plan generated based on the Actor network and the Critic network is monitored in real time. When the performance index exceeds the preset index threshold, the plan is adjusted. The execution plan is adjusted based on the local optimization method to maintain a smooth transition of execution. The optimization experience of the execution results is fed back to the experience pool to update the deep reinforcement learning network online. The optimal execution plan is generated based on the updated deep reinforcement learning network.
[0112] For example, in large-scale data query scenarios, optimizing the execution plan is crucial for improving query efficiency. Converting the database query syntax tree into an intermediate query representation is a fundamental step in execution plan optimization. The intermediate query representation uses a tree structure, with each node representing a query operation, such as table scan, filtering, join, aggregation, and sorting. Taking the aforementioned example of "finding the top ten best-selling gaming laptops and their prices," the SQL statement "SELECT p.name, p.priceFROM product p JOIN sales s ON p.id=s.product_id WHERE p.category='gaming laptops' GROUP BY p.id ORDER BY SUM(s.quantity) DESC LIMIT 10" is converted into an intermediate query representation containing multiple operation nodes. This representation includes a table scan node (scanning the product and sales tables separately), a filter node (filtering the category condition), a join node (joining the two tables), a grouping node (grouping by p.id), an aggregation node (calculating SUM(s.quantity)), a sorting node (sorting in descending order), and a limit node (limiting the results to 10 rows). Based on the intermediate query representation, a state vector is constructed, which contains query operation features and resource usage status. Query operation features include operation type encoding (e.g., table scan as [1,0,0,0,0], join as [0,1,0,0,0]), the number of records involved in the table, field selectivity, and filtering condition complexity. Resource usage status includes current CPU utilization, memory usage, and disk I / O load. For example, for a product table scan operation, features include 100,000 records, a field selectivity of 0.2, and a CPU utilization of 50%. These features are encoded into a fixed-dimensional vector, such as a 100-dimensional state vector, for input into the deep reinforcement learning network.
[0113] The state space of a deep reinforcement learning network contains the aforementioned state vector, representing the complete state of the current query execution environment. The action space contains three types of optimization actions: physical operator selection actions, execution order adjustment actions, and parallelism configuration actions. Physical operator selection actions determine which specific algorithm to use to implement the query operation; for example, a join operation can choose nested loop joins, hash joins, or sort-merge joins. For the join operation in the example query, possible actions are "use hash join," "use sort-merge join," etc. Execution order adjustment actions change the execution order of operations, such as deciding whether to perform filtering or joins first. For the example query, possible actions are "filter the product table first, then join," "join first, then filter the results," etc. Parallelism configuration actions set the degree of parallel execution of operations, such as "allocate 4 parallel threads for the join operation." The composite reward function weights execution time with resource consumption. Execution time refers to the time required for the query to return results, and is usually negative to encourage faster execution. Resource consumption comprehensively considers the usage of CPU, memory, and I / O resources. The specific calculation method is: Resource Consumption = 0.5 × CPU Utilization + 0.3 × Memory Utilization + 0.2 × I / O Utilization. Where CPU utilization is the average percentage of CPU usage during query execution, memory utilization is the ratio of memory used by the query to available memory, and I / O utilization is the ratio of query I / O throughput to peak I / O. Example of a composite reward function: Reward Value = -0.7 × Execution Time - 0.3 × Resource Consumption. Both execution time and resource consumption are normalized to a range of 0 to 1. Execution time is normalized by dividing by a preset maximum acceptable execution time (e.g., 10 seconds), and resource consumption itself is already a ratio within the range of 0 to 1. Through this design, the model tends to select an execution plan that is both fast and resource-efficient.
[0114] A multilayer perceptron (MLP) consists of an input layer, multiple hidden layers, and an output layer. The hidden layers typically employ the ReLU activation function. For example, a MLP for state encoding contains three hidden layers with 256, 128, and 64 nodes, encoding a 100-dimensional state vector into a 64-dimensional state code. Based on this state code, an Actor network and a Critic network are trained. The Actor network generates the action probability distribution. It is a three-layer neural network with 64, 32, and an action space dimension per layer. The last layer uses the Softmax activation function to output the probability of each action. For example, for physics operator selection, it outputs a probability distribution of "nested loop connections: 0.1, hash connections: 0.7, sort-merge connections: 0.2". The Critic network evaluates the state value. It is also a three-layer neural network with 64, 32, and 1 nodes, outputting a scalar representing the value estimate of the current state. A policy gradient algorithm, specifically the Advantage Actor-Critic (A2C) method, is used to jointly optimize the Actor and Critic networks based on a composite reward function. During training, multiple rounds of optimization attempts are performed. Each round includes: selecting an action, executing a plan, observing the results, calculating the reward, and updating the network parameters. For example, trying the combination of "using hash join + filtering before joining + parallelism of 4" resulted in an execution time of 200 milliseconds, resource consumption of 30%, and a calculated reward of -0.7 × 0.2 - 0.3 × 0.3 = -0.23. These optimization experiences (state, action, reward, next state) are saved to an experience pool for subsequent training. The experience pool uses a fixed capacity design, for example, storing the most recent 10,000 optimization experiences, and a random sampling method is used for training.
[0115] During execution, key performance indicators (KPIs) are continuously monitored, including execution time, CPU utilization, memory usage, and I / O latency. When these KPIs exceed preset thresholds, such as execution time exceeding 150% of the estimate or CPU utilization exceeding 90%, plan adjustments are triggered. For example, if the execution time of a join operation is found to be significantly longer than expected, the join algorithm is dynamically adjusted or parallelism is increased. Plan adjustments employ a local optimization approach, meaning only the performance-abnormal parts are adjusted, while other parts remain unchanged, to achieve a smooth execution transition. This local adjustment avoids the overhead of recompiling the entire query, improving response speed. After adjustments are completed, the optimization experience from the execution results is fed back into the experience pool, including the original plan state, adjustment actions, post-adjustment performance, and final reward value. This online-generated experience is used to update the deep reinforcement learning network in real time, employing a mini-batch gradient descent method, updating parameters with 32 or 64 samples per update. Through this continuous learning mechanism, the network can adapt to constantly changing data distributions and loads, generating increasingly optimized execution plans. For the example query, after multiple rounds of optimization, the optimal execution plan is as follows: first, apply filtering conditions to the product table and use index scanning, then connect it with the sales table using a hash join algorithm, then use streaming aggregation to calculate the total sales, and finally use the TopN algorithm to obtain the top 10 results. The entire process adopts an adaptive parallelism configuration.
[0116] This invention models the query optimization problem as a reinforcement learning task and uses deep neural networks to automatically learn complex optimization strategies. It can adapt to different data distributions and states, dynamically adjust the execution plan, optimize resource utilization while ensuring query performance, and significantly improve the processing efficiency and stability in complex query scenarios.
[0117] In one optional implementation, the state vector is encoded using a multilayer perceptron to obtain a state code, and the steps of training the Actor network and the Critic network based on the state code include:
[0118] The state vector is input into a multilayer perceptron, and the initial state code is obtained through nonlinear transformation.
[0119] Local attention weights are calculated based on adjacent temporal states, and global attention weights are calculated based on historical states. The local attention weights and global attention weights are adaptively fused to obtain combined attention weights. A temporal memory is constructed based on the combined attention weights, and the state information in the temporal memory is sorted by importance and updated periodically according to the combined attention weights. The initial state code is associated and matched with the state information in the temporal memory to generate an enhanced state code that considers historical state transitions.
[0120] The Actor network and the Critic network are trained based on the enhanced state encoding, and the network update results are fed back to dynamically adjust the ratio of the local attention weights and the global attention weights, thereby optimizing the update strategy of the temporal memory.
[0121] For example, the state vector contains query operation features and resource usage status, typically with 100 to 200 dimensions. A multilayer perceptron consists of an input layer, multiple hidden layers, and an output layer. A specific implementation uses a three-hidden-layer structure with 256, 128, and 64 nodes respectively. The input layer receives the original state vector. The first hidden layer maps the input to a 256-dimensional vector using a linear transformation and the ReLU activation function. The second hidden layer maps the 256-dimensional vector to a 128-dimensional vector, and the third hidden layer maps the 128-dimensional vector to a 64-dimensional vector. Finally, the output layer generates a 64-dimensional initial state code. In practical applications, for a state vector representing "using hash joins to handle large table join operations," the multilayer perceptron transforms it to generate an initial state code emphasizing the characteristics of the join algorithm. The dimension representing hash join efficiency has a high activation value, such as 0.8, while the dimensions representing other join algorithms have lower activation values, such as 0.2 or 0.1. This nonlinear transformation can extract key features from the state vector, providing a foundation for subsequent attention mechanisms and network training.
[0122] Local attention mechanisms focus on the short-term dependencies between the current state and recent states, typically considering the states of the last five time steps. The similarity between the current state encoding and these five recent state encodings is calculated, resulting in five similarity scores. These scores are then normalized using the Softmax function to a weight value that sums to 1. For example, the current state performing a join operation has a similarity of 0.7 to the previous state performing a filter operation and 0.3 to the state before that performing a table scan; after normalization, the local attention weights are 0.7 and 0.3, respectively. Global attention mechanisms, on the other hand, focus on the dependencies between the current state and long-term historical states, considering key states in historical executions. A historical state database of 100 states is maintained. The similarity between the current state and each state in the database is calculated, and the 10 states with the highest similarity are selected, with their normalized weights calculated. For example, the current hash join state has a similarity of 0.9 to a certain efficient hash join state in the database, corresponding to a global attention weight of 0.4. An adaptive fusion mechanism combines local and global attention weights, initially emphasizing local attention (e.g., 70% local + 30% global). As the learning process progresses, the proportion of global attention gradually increases (e.g., 40% local + 60% global). The fused combined attention weights are used to construct a temporal memory, which stores recent states and important historical states along with their weights. Based on the combined attention weights, the state information in the temporal memory is ranked by importance, with higher-weighted states appearing at the top. The memory is periodically updated, removing lower-weighted states and adding new important states. In the example, when a specific table join strategy is repeatedly found to execute efficiently, the state corresponding to that strategy will receive a higher combined attention weight, such as 0.85, and will be retained in a priority position in the temporal memory.
[0123] The similarity between the initial state code and each state code in the temporal memory is calculated to construct an attention distribution. Then, a weighted sum of the state codes in the temporal memory is calculated based on this distribution. This weighted sum reflects information from historically similar states and is concatenated or weightedly combined with the initial state code to generate an enhanced state code. For example, if the initial state code is a 64-dimensional vector [0.5, 0.7, ..., 0.3] and the historical information extracted from the temporal memory is a 64-dimensional vector [0.6, 0.5, ..., 0.4], concatenating the two yields a 128-dimensional enhanced state code [0.5, 0.7, ..., 0.3, 0.6, 0.5, ..., 0.4]. This enhanced state code contains both features of the current state and experience from past executions, helping the model make more accurate decisions.
[0124] The Actor network employs a three-layer structure. The input layer directly receives 128-dimensional augmented state codes as feature input, enabling the network to utilize rich features containing historical execution information. The hidden layers have 128 and 64 nodes respectively, using the ReLU activation function for non-linear transformation. The output layer uses the Softmax function to generate action probability distributions. For example, inputting augmented state codes for connection operations, which incorporate historical execution experience of similar connection operations, the network outputs probability distributions such as "using hash connections: 0.7, using nested loop connections: 0.1, using sort-merge connections: 0.2". The Critic network also directly uses augmented state codes as input, employing a three-layer structure. The hidden layers are the same as the Actor network, but the output layer has only one node, using a linear activation function to output state value estimates. The training process uses the Advantage Actor-Critic algorithm, with each batch containing 64 augmented state code samples, using the Adam optimizer with a learning rate of 0.001 and a discount factor of 0.95. The loss values and gradients calculated during network training are used to dynamically adjust the ratio of local and global attention. For example, by analyzing the loss trends of Actor and Critic networks under different attention ratios, if it is found that increasing the proportion of global attention weights reduces model loss and improves performance, the ratio will be automatically adjusted, increasing the global attention weight from 30% to 40% or 50%. Simultaneously, training results are also used to optimize the temporal memory update strategy, including adjusting the memory size, update frequency, and eviction policy. If historical information of a certain type of state (such as large table join operations) is found to be helpful for decision-making, reflected in smaller TD errors for related states, the retention time of such states in the memory will be increased, thereby improving the model's decision quality.
[0125] This invention introduces local and global attention mechanisms to achieve efficient utilization of historical execution states, effectively capture the temporal dependencies of query execution, enhance the information richness of state representation, and improve the training effect of the Actor-Critic network. This enables deep reinforcement learning to generate better database query execution plans and significantly improves execution efficiency in complex query scenarios.
[0126] A second aspect of the present invention provides an electronic device, comprising:
[0127] processor;
[0128] Memory used to store processor-executable instructions;
[0129] The processor is configured to invoke instructions stored in the memory to execute the aforementioned method.
[0130] A third aspect of the present invention provides a computer-readable storage medium having stored thereon computer program instructions that, when executed by a processor, implement the aforementioned method.
[0131] This invention can be a method, apparatus, system, and / or computer program product. The computer program product may include a computer-readable storage medium having computer-readable program instructions loaded thereon for performing various aspects of the invention.
[0132] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for intelligent natural language parsing and data query instruction generation based on a large model, characterized in that, include: The system receives natural language query statements from users and inputs them into a pre-trained large-scale language model to perform semantic parsing and obtain semantic parsing results. Based on the semantic parsing results, a dynamically enhanced query semantic graph is constructed. This graph includes query entity nodes, query relation edges, and attribute constraint nodes. An external knowledge graph is introduced for semantic enhancement. This external knowledge graph includes a domain ontology and a concept hierarchy tree. The process involves: mapping the query semantic graph to concepts using knowledge from the domain ontology. This mapping is achieved by calculating inter-concept similarity, which is weighted based on the longest common subsequence distance and vector space representation. Based on the concept mapping results, the query semantic graph is expanded with hierarchical concepts according to the calculated hierarchical association strength in the concept hierarchy tree. This hierarchical association strength is based on the probability... The process involves calculating the depth and nearest common ancestor node in the hierarchical tree; structurally expanding the query semantic graph after hierarchical concept expansion, supplementing missing entity nodes and attribute nodes based on contextual associations, establishing database query relationship paths between entity nodes to convert semantic-level conceptual relationships into database query relationships, and updating the relationship edge structure of the query semantic graph with the converted database query relationships; optimizing the constraints of the structurally expanded query semantic graph, calculating constraint priorities based on constraint value ranges and class specialization, and merging constraints according to the constraint priorities; and completing semantic enhancement when the evaluation score of the constraint-optimized query semantic graph reaches a preset scoring threshold. Extract the structured feature vector of the query semantic graph, calculate the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and map the query semantic graph into a database query syntax tree based on the semantic similarity calculation results; An intermediate query representation is generated based on the database query syntax tree. An optimization strategy based on deep reinforcement learning is used to optimize the execution plan of the intermediate query representation. The query execution time and resource consumption are used as reward signals to generate an optimal execution plan. The intermediate query representation is converted into a standardized database query instruction according to the optimal execution plan. The standardized database query instruction is executed to obtain the query results and convert them into a query response in natural language form.
2. The method according to claim 1, characterized in that, The steps for receiving natural language query statements from users and inputting them into a pre-trained large-scale language model to perform semantic parsing and obtain semantic parsing results include: The natural language query statement is input into the pre-trained large-scale language model to extract the deep semantic representation of the natural language query statement; Based on the deep semantic representation, the query intent is identified and the semantic slots are filled to obtain the query intent result and the slot filling result. The query intent result and the slot filling result are fused to construct a query semantic framework including the main query object, query conditions and query target. The semantic consistency of the query semantic framework is verified to generate semantic parsing results.
3. The method according to claim 1, characterized in that, The query semantic graph is expanded with hierarchical concepts based on the hierarchical association strength calculated in the concept hierarchy tree. The step of calculating the hierarchical association strength based on the depth of the concept in the hierarchy tree and the nearest common ancestor node includes: The nodes in the concept hierarchy tree represent concepts, and the edges in the concept hierarchy tree represent hierarchical relationships. The depth weight is calculated using the sigmoid function based on the depth of the concept node, and the nearest common ancestor weight is calculated based on the ratio of the depth of the nearest common ancestor node to the maximum depth of the concept node. The hierarchical association strength is obtained by weighting the depth weight and the nearest common ancestor weight using a balancing factor. From the concept hierarchy tree, we obtain extended candidate concepts of concept nodes. When the hierarchical association strength of the extended candidate concept is greater than the association strength threshold, it is determined as an extended concept, and the node corresponding to the extended concept is added to the query semantic graph. Calculate the evaluation metrics for the extended concept, including semantic consistency and extended coverage based on hierarchical association strength; iteratively adjust the balance factor and the association strength threshold based on the evaluation metrics, and stop adjusting when the improvement value of the evaluation metrics is lower than a preset threshold.
4. The method according to claim 1, characterized in that, The steps of extracting the structured feature vector of the query semantic graph, calculating the semantic similarity between the structured feature vector and each template in the pre-built database query template library, and mapping the query semantic graph to a database query syntax tree based on the semantic similarity calculation results include: The node feature vector and edge feature vector of the query semantic graph are constructed. The node feature vector is obtained by weighted combination of low-dimensional dense vector representation of node attributes and node position encoding vector. The edge feature vector is obtained by feature extraction based on edge type and direction information. The node feature vector and the edge feature vector are combined to obtain the structured feature vector of the query semantic graph. The templates in the pre-built database query template library are represented as corresponding structured feature vectors. The multi-dimensional similarity between the structured feature vector of the query semantic graph and the structured feature vector of the template is calculated. The multi-dimensional similarity is obtained by weighted combination of semantic similarity of node features, connection relationship similarity of edge features, and matching degree of query constraints. The optimal matching template is selected based on the multi-dimensional similarity. An initial syntax tree is constructed based on the mapping relationship between the structured feature vector of the optimal matching template and the structured feature vector of the query semantic graph. The initial syntax tree is then structurally optimized to obtain the database query syntax tree.
5. The method according to claim 1, characterized in that, The steps of generating an intermediate query representation based on the database query syntax tree, optimizing the execution plan of the intermediate query representation using a deep reinforcement learning-based optimization strategy, and using query execution time and resource consumption as reward signals to generate the optimal execution plan include: The database query syntax tree is converted into an intermediate query representation, and a state vector containing query operation features and resource usage status is constructed based on the intermediate query representation. Construct a state space and action space for a deep reinforcement learning network. The state space includes the state vector, and the action space includes physical operator selection actions, execution order adjustment actions, and parallelism configuration actions. Construct a composite reward function based on a weighted combination of query execution time and resource consumption. The state vector is encoded using a multilayer perceptron to obtain a state code. An Actor network and a Critic network are trained based on the state code. The Actor network outputs an action probability distribution, and the Critic network evaluates the state value. A policy gradient algorithm is used to jointly optimize the Actor network and the Critic network according to the composite reward function, and historical optimization experience is saved to an experience pool. The execution plan generated based on the Actor network and the Critic network is monitored in real time. When the performance index exceeds the preset index threshold, the plan is adjusted. The execution plan is adjusted based on the local optimization method to maintain a smooth transition of execution. The optimization experience of the execution results is fed back to the experience pool to update the deep reinforcement learning network online. The optimal execution plan is generated based on the updated deep reinforcement learning network.
6. The method according to claim 5, characterized in that, The steps of encoding the state vector using a multilayer perceptron to obtain a state code, and training the Actor network and Critic network based on the state code include: The state vector is input into a multilayer perceptron, and the initial state code is obtained through nonlinear transformation. Local attention weights are calculated based on adjacent temporal states, and global attention weights are calculated based on historical states. The local attention weights and global attention weights are adaptively fused to obtain combined attention weights. A temporal memory is constructed based on the combined attention weights, and the state information in the temporal memory is sorted by importance and updated periodically according to the combined attention weights. The initial state code is associated and matched with the state information in the temporal memory to generate an enhanced state code that considers historical state transitions. The Actor network and the Critic network are trained based on the enhanced state encoding, and the network update results are fed back to dynamically adjust the ratio of the local attention weights and the global attention weights, thereby optimizing the update strategy of the temporal memory.
7. An electronic device, characterized in that, include: processor; Memory used to store processor-executable instructions; The processor is configured to invoke instructions stored in the memory to execute the method according to any one of claims 1 to 6.
8. A computer-readable storage medium having computer program instructions stored thereon, characterized in that, When the computer program instructions are executed by the processor, they implement the method described in any one of claims 1 to 6.
Citation Information
Patent Citations
Structured query language generation method and device based on large model, equipment and medium
CN120104637A
Data query method and system based on natural language text
CN120492610A