A database query method based on graph structure
By using a graph-based database query method, leveraging an enterprise knowledge base and a pre-trained semantic understanding model, standard SQL statements are generated, solving the problems of insufficient semantic understanding and poor interpretability in NL2SQL technology, and achieving efficient and accurate database queries.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HANGZHOU LINGHE DIGITAL INTELLIGENCE TECHNOLOGY CO LTD
- Filing Date
- 2026-04-27
- Publication Date
- 2026-07-10
AI Technical Summary
Existing NL2SQL technology is insufficient in semantic understanding, making it impossible for ordinary users to directly use the database, and its poor interpretability leads to the problem of data silos.
A graph-based database query method is adopted. An enterprise knowledge base is built by collecting data tables and query statements. An undirected weighted graph structure is constructed, and the query statement is enhanced by RAG. A pre-trained semantic understanding model is used to filter the initial nodes, generate the target node set, and generate standard SQL statements using LLM.
It improves semantic understanding and interpretability, accurately locates relevant data tables, enhances the accuracy and efficiency of SQL statement generation, solves the omission problem in multi-table join scenarios, and ensures the completeness and executability of query results.
Smart Images

Figure CN122364285A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of information retrieval, and more specifically to a database query method based on graph structure. Background Technology
[0002] To ensure the secure storage, rapid retrieval, and processing of critical information such as financial and human resource data, enterprises and institutions typically store this information in relational databases using fixed formats or schemas. They then access, manage, and manipulate (query, update, etc.) these relational databases using SQL. However, SQL is a standardized programming language that ordinary users (non-technical personnel) can only master after specific training. Therefore, in practice, enterprise databases often become data silos, with ordinary users in various departments having to contact technical personnel to access the database, causing numerous inconveniences in their work.
[0003] To reduce the difficulty for ordinary users to use databases, researchers developed NL2SQL (Natural Language to SQL) technology. NL2SQL can automatically convert human natural language questions into SQL query statements, allowing ordinary users to use the database simply by entering their questions.
[0004] However, existing NL2SQL suffers from insufficient semantic understanding and poor interpretability. Summary of the Invention
[0005] To ensure the secure storage, rapid retrieval, and processing of critical information such as financial and human resource data, enterprises and institutions typically store this information in relational databases using fixed formats or schemas. They then access, manage, and manipulate (query, update, etc.) these relational databases using SQL. However, SQL is a standardized programming language that ordinary users (non-technical personnel) can only master after specific training. Therefore, in practice, enterprise databases often become data silos, with ordinary users in various departments having to contact technical personnel to access the database, causing numerous inconveniences in their work.
[0006] To reduce the difficulty for ordinary users to use databases, researchers developed NL2SQL (Natural Language to SQL) technology. NL2SQL can automatically convert human natural language questions into SQL query statements, allowing ordinary users to use the database simply by entering their questions.
[0007] However, existing NL2SQL suffers from insufficient semantic understanding and poor interpretability. The technical problem to be solved by the present invention is to overcome the shortcomings of the prior art and provide a database query method based on graph structure with better semantic understanding and interpretability.
[0008] To address the aforementioned technical problems, this invention provides a database query method based on a graph structure, comprising: S1. Collect all data tables and multiple query statements-data table pairs from the database and build an enterprise knowledge base; S2. Model a database graph structure based on all data tables in the database; S3. Use RAG to enhance the input natural language query to obtain the enhanced query statement; S4. Based on the enhanced query statement, the initial nodes are selected from the graph structure to complete the initial node matching of the graph; S5. Generate a target node set based on each initial node; S6. Generate the SQL statement corresponding to the enhanced query statement based on the target node set to achieve SQL statement conversion; S7. Execute SQL statements to query data in the database and obtain the query results.
[0009] As a further improvement of the present invention: the query statement-data table pair in S1 refers to the query statement and its corresponding data table to be queried, which is organized by those skilled in the art; The enterprise knowledge base includes database-related business dictionaries, field meaning explanations, examples of common query scenarios, and business rule constraints.
[0010] As a further improvement of the present invention: step S2 includes: S201. Construct an undirected weighted graph structure network using all data tables as the core modeling objects; treat each data table as a node in the graph structure and the relationships between the data tables as edges in the graph structure. S202. Assign corresponding edge weights to each edge based on the tightness of each association.
[0011] As a further improvement of the present invention: step S4 includes: S401. Use multiple pairs of query statements and data tables to train the semantic understanding model to obtain a pre-trained semantic understanding model; S402. Input the enhanced query statement into the pre-trained semantic understanding model. The pre-trained semantic understanding model calculates the semantic matching degree between the enhanced query statement and all nodes in the graph. The semantic matching degree ranges from 0 to 1. Select the nodes with the highest matching degree as the initial nodes.
[0012] As a further improvement of the present invention: step S5 includes: S501. Starting from each initial node, traverse all adjacent nodes in the graph that are directly connected to the starting point; S502. Retain the adjacent nodes whose edge weights to each starting point are greater than the edge weight threshold as target nodes; S503. Summarize each initial node and the target node corresponding to each initial node to obtain the target node set. The target node set includes all relevant data tables required for the query.
[0013] As a further improvement of the present invention: step S6 includes: S601. Extract the structured information of each node in the target node set to obtain complete context information; S602. Input the enhanced query statement and complete context information into the LLM at the same time. The LLM defines the data structure and association logic based on the complete context information and converts the enhanced query statement into a standard, executable SQL statement.
[0014] The beneficial effects of the present invention are as follows: The database query method based on graph structure provided by the present invention has good semantic understanding and good interpretability.
[0015] First, S2 explicitly models the table structure and relationships of the database in the form of a graph. It accurately describes the degree of connection between data tables through a quantitative weighting mechanism, providing a structured basis for subsequent node matching and related table filtering. This effectively avoids the information redundancy problem caused by full table input to LLM and improves the overall processing efficiency of the system. Secondly, S3 combines the enterprise knowledge base to perform structured enhancement on users' natural language queries, transforming vague or incomplete query intentions into standardized statements containing expected output fields and query conditions. This effectively compensates for the problem of incomplete or ambiguous user query expressions, significantly improves the input quality of subsequent processing steps, and thus improves the accuracy of the final generated SQL statement. Furthermore, S4 employs a pre-trained semantic understanding model to accurately calculate the semantic matching degree between the query statement and each data table node, quickly locating the most relevant initial data table, significantly reducing the candidate table search space, and effectively improving the accuracy and computational efficiency of node matching. Next, S5 uses a graph-based adjacency node traversal and edge weight threshold filtering mechanism to automatically discover data tables that are highly related to the initial node. This enables automatic completion of data tables involved in multi-table join queries, solving the problem of missing key data tables in complex multi-table join scenarios in existing NL2SQL methods and ensuring the integrity of the target node set. Finally, S6 provides LLM with accurate and minimal structured context information, focusing LLM's attention on the data tables directly related to the query, eliminating the interference of irrelevant data tables on the generation process, significantly improving the accuracy and executability of SQL statement generation in complex multi-table query scenarios, and solving the core problems of insufficient semantic understanding and poor interpretability in existing technologies. Attached Figure Description
[0016] Figure 1 This is a schematic diagram of the principle of the present invention; Figure 2 This is an example diagram structure. Detailed Implementation
[0017] The specific embodiments of the present invention will be described in further detail below with reference to the accompanying drawings.
[0018] Definitions of relevant terms used in this invention: (1) Relational Database: A widely used database type that uses tables to store and organize data. Each table consists of rows and columns. (2) SQL: Structured Query Language, a standardized programming language used to manage and manipulate relational databases. It performs functions such as data definition, data query, data manipulation, and data control through a series of statements and commands.
[0019] (3) Graph structure: also called graph, is a graph consisting of several given points (i.e., vertices, also called nodes) and lines (i.e. edges) connecting two points. This type of graph is usually used to describe a certain relationship between certain things. Points represent things, and lines connecting two points represent the relationship between the corresponding two things. (4) Undirected graph: An undirected graph is one of the most basic graph structures in graph theory. The edges in an undirected graph have no directionality and only represent bidirectional connections between nodes. That is, in an undirected graph, if there is an edge between vertex A and vertex B, then you can reach B from A or from B. This symmetrical connection makes undirected graphs an ideal model for representing many bidirectional relationships in the real world.
[0020] (5) Weighted graph: refers to a graph in which edges have weights, representing connection costs, distances, etc.; (6) RAG: Retrieval-Augmented Generation, is an artificial intelligence framework that integrates retrieval technology and generative model. Its core mechanism is to obtain related information from external knowledge bases or real-time data sources through semantic retrieval and input the retrieval results as contextual enhancement prompts into the large language model. (7) INT: integer, an integer type, also known as integer, is a commonly used integer data type; (8) VARCHAR: A data type used to store variable-length strings. VARCHAR(50) means that the field can store a maximum of 50 characters, and VARCHAR(100) means that the field can store a maximum of 100 characters. (9) DATE: A date and time data type; (10) DECIMAL(M,D): A data type where M represents the total number of digits, including the integer part and the fractional part, and D represents the number of fractional digits; like Figure 1 As shown, this invention provides a database query method based on a graph structure: S1. Collect all data tables and multiple query statements-data table pairs from the database and build an enterprise knowledge base; The core metadata of a data table includes the table name, the business module it belongs to, column names, column data types, column comments, and key information; The table names and column names of all data tables are shown in Table 1: Table 1
[0021] A query statement-data table pair refers to a query statement and its corresponding data table to be queried. This can be organized by those skilled in the art and may include querying employee salary, querying employee name, etc.
[0022] The enterprise knowledge base includes database-related business dictionaries, field meaning explanations, examples of common query scenarios, business rule constraints, and other information. The business dictionary can include annual salary, department, job, etc.
[0023] S2. Model a database graph structure based on all data tables in the database; S201. Construct an undirected weighted graph structure network using all data tables as the core modeling objects; treat each data table as a node in the graph structure and the relationships between the data tables as edges in the graph structure. Each data table corresponds to one node, and the two are uniquely linked; the node attributes contain the core metadata of the data table. The sources of relationships include defined foreign key relationships between data tables and implicit relationships between data tables where no foreign key is defined but the field names and data types are completely identical.
[0024] S202. Assign corresponding edge weights to each edge based on the tightness of each relationship, and use a quantitative scoring mechanism to calculate the edge weights in order to clarify the rules for assigning edge weights; S2021. If there is a foreign key relationship between two data tables, then set the basic weight of the edge between the two nodes corresponding to the two data tables to 3 points. If the foreign key is a primary key or unique index in both data tables, then the base weight of the edge is increased by 2 points on top of the base weight of 3 points. S2022. If there is a relationship between two data tables with the same field, the edge weight of the edge between the two nodes corresponding to the two data tables will be increased by 1 point, up to a maximum of 3 points. The same field is associated with a field that has the exact same name and data type; S2023. The final edge weight is the sum of the scores of S2021 to S2022, with a value range of 1 to 5 points. If the score is over 5 points, it is counted as 5 points, and if it is less than 1 point, it is counted as 1 point. The higher the weight value, the closer the relationship between the data tables. like Figure 2 As shown, if there are four data tables, the graph structure includes four nodes. Since emp has specific relationships with salary, dept, and job respectively, and there are no direct relationships between the other tables, no edges are created. Therefore, the graph structure has three edges. Since there is a foreign key relationship emp_id between emp and salary, and emp_id forms a unique index in the salary table, the edge weight of this edge is 5. Since there is a foreign key relationship dept_id between emp and dept, the edge weight of this edge is 3. Since there is a foreign key relationship job_id between emp and job, the edge weight of this edge is 3.
[0025] S3. Use RAG to enhance the input natural language query to obtain the enhanced query statement; S301. After the user inputs a natural language query, information related to the query content is retrieved from the knowledge base to form auxiliary understanding data; S302. Use the knowledge base to restructure the original natural language query according to the preset format to generate an enhanced query statement; The default format refers to the statement's arrangement as: [Query Input] + [Expected Output Fields] + [Query Conditions]; The query input is the natural language query text entered by the user; the expected output fields are completed by matching with the knowledge base; and the query conditions are completed by combining the business rules in the knowledge base. If the natural language query is: to find the name, department name, and job title of employees with an annual salary of over 200,000 yuan in 2024, the enhanced query after RAG enhancement would be: [Employees with an annual salary of over 200,000 yuan in 2024] + [emp_name, dept_name, job_title] + [salary.record_year=2024 AND salary.annual_salary>200000].
[0026] S4. Based on the enhanced query statement, the initial nodes are selected from the graph structure to complete the initial node matching of the graph; The initial node refers to the data table most relevant to the enhanced query statement; S401. Use multiple pairs of query statements-data tables to train the semantic understanding model to obtain a pre-trained semantic understanding model. The pre-trained semantic understanding model has the ability to accurately identify the correspondence between query requirements and data tables. The pre-trained semantic understanding model is a bidirectional text encoding model based on the Transformer architecture. Specifically, the bidirectional text encoding model based on the Transformer architecture is BERT (Bidirectional Encoder Representations from Transformers) or RoBERTa (Robustly Optimized BERT Approach).
[0027] The training steps for a pre-trained semantic understanding model include: (1) Collect query statement-data table pairs, and construct positive sample pairs (matching query statement-data table pairs) and negative sample pairs (non-matching query statement-data table pairs) as training datasets; (2) Initialize the semantic understanding model with the publicly available parameter weights to obtain the initialized semantic understanding model; (3) Fine-tune the initial semantic understanding model with the contrastive learning loss function to maximize the semantic matching score between the correct query statement and the corresponding data table, while minimizing the matching score of the incorrect pairing; The contrastive learning loss function is either InfoNCE Loss (Information Noise Contrastive Estimation) or Triplet Loss (Triplet Loss). (4) Evaluate the performance of the semantic understanding model on positive sample pairs using TOP-N recall as an indicator until the performance of the semantic understanding model meets the application requirements, and obtain the pre-trained semantic understanding model.
[0028] S402. Input the enhanced query statement into the pre-trained semantic understanding model. The pre-trained semantic understanding model calculates the semantic matching degree between the enhanced query statement and all nodes in the graph. The semantic matching degree ranges from 0 to 1. Select the nodes with the highest matching degree as the initial nodes. Specifically, the top N nodes can be filtered, where N is a configurable parameter that can be dynamically adjusted based on the number of database tables. The semantic matching degree between the enhanced query statement and each node is calculated and obtained in Table 2. Let N=2, and filter the top 2 nodes with the highest matching degree to obtain the initial node set: {salary,emp}.
[0029] Table 2 node The semantic match score between the enhanced query and the node. emp 0.87 dept 0.72 salary 0.93 job 0.68 S5. Generate a target node set based on each initial node; S501. Starting from each initial node, traverse all adjacent nodes in the graph that are directly connected to the starting point; S502. Retain the adjacent nodes whose edge weights to each starting point are greater than the edge weight threshold as target nodes, and filter the adjacent nodes by weight threshold; The edge weight threshold is a configurable parameter that can be set according to actual needs. S503. Summarize each initial node and the target node corresponding to each initial node to obtain the target node set, which includes all relevant data tables required for the query; Specifically, starting with salary and emp, we traverse the adjacent nodes: salary's adjacent node is emp (weight=5); emp's adjacent nodes are salary (weight=5), dept (weight=3), and job (weight=3); assuming an edge weight threshold of 2, we retain adjacent nodes with weight > 2. The final target node set is: {salary,emp,dept,job}, which contains all the relevant data tables required for this query. S6. Generate the SQL statement corresponding to the enhanced query statement based on the target node set to achieve SQL statement conversion; S601. Extract the structured information of each node in the target node set to obtain complete context information and organize the context information; Complete context information refers to the structured information extracted from the data tables corresponding to each node in the target node set, including table name, column name, column comments, field data type, and inter-table relationships (field mapping relationships corresponding to edges), etc.
[0030] S602. Input the enhanced query statement and complete context information into the LLM (Large Language Model) at the same time. The LLM defines the data structure and association logic based on the complete context information and converts the enhanced query statement into a standard, executable SQL statement.
[0031] S7. Execute SQL statements to query data in the database and obtain the query results.
Claims
1. A database query method based on graph structure, characterized in that, include: S1. Collect all data tables and multiple query statements-data table pairs from the database and build an enterprise knowledge base; S2. Model a database graph structure based on all data tables in the database; S3. Use RAG to enhance the input natural language query to obtain the enhanced query statement; S4. Based on the enhanced query statement, the initial nodes are selected from the graph structure to complete the initial node matching of the graph; S5. Generate a target node set based on each initial node; S6. Generate the SQL statement corresponding to the enhanced query statement based on the target node set to achieve SQL statement conversion; S7. Execute SQL statements to query data in the database and obtain the query results.
2. The database query method based on graph structure according to claim 1, characterized in that, The query statement-data table pair in S1 refers to the query statement and its corresponding data table to be queried, which is organized by those skilled in the art. The enterprise knowledge base includes database-related business dictionaries, field meaning explanations, examples of common query scenarios, and business rule constraints.
3. The database query method based on graph structure according to claim 1, characterized in that, The steps in S2 include: S201. Construct an undirected weighted graph structure network using all data tables as the core modeling objects; treat each data table as a node in the graph structure and the relationships between the data tables as edges in the graph structure. S202. Assign corresponding edge weights to each edge based on the tightness of each association.
4. The database query method based on graph structure according to claim 1, characterized in that, The steps in S4 include: S401. Use multiple pairs of query statements and data tables to train the semantic understanding model to obtain a pre-trained semantic understanding model; S402. Input the enhanced query statement into the pre-trained semantic understanding model. The pre-trained semantic understanding model calculates the semantic matching degree between the enhanced query statement and all nodes in the graph. The semantic matching degree ranges from 0 to 1. Select the nodes with the highest matching degree as the initial nodes.
5. The database query method based on graph structure according to claim 1, characterized in that, The steps in S5 include: S501. Starting from each initial node, traverse all adjacent nodes in the graph that are directly connected to the starting point; S502. Retain the adjacent nodes whose edge weights to each starting point are greater than the edge weight threshold as target nodes; S503. Summarize each initial node and the target node corresponding to each initial node to obtain the target node set. The target node set includes all relevant data tables required for the query.
6. The database query method based on graph structure according to claim 1, characterized in that, Step S6 includes: S601. Extract the structured information of each node in the target node set to obtain complete context information; S602. Input the enhanced query statement and complete context information into the LLM at the same time. The LLM defines the data structure and association logic based on the complete context information and converts the enhanced query statement into a standard, executable SQL statement.