Multi-source heterogeneous database query method, system, device and medium

By constructing a multimodal database metadata knowledge graph and using graph neural networks to calculate table relevance, combined with a large language model and adapter pattern, the problems of incorrect table selection and inaccurate join conditions in multi-source heterogeneous database queries are solved, thereby improving the accuracy and reliability of complex cross-table queries.

CN120950542BActive Publication Date: 2026-02-13INSPUR SOFTWARE CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202511476435.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-10-16
Publication Date
2026-02-13
Estimated Expiration
2045-10-16

AI Technical Summary

Technical Problem

Existing technologies often result in SQL query errors in multi-source heterogeneous database queries due to incorrect table selection or inaccurate join conditions. This lack of in-depth understanding of the relationships between database tables leads to insufficient accuracy and reliability in complex multi-table queries.

Method used

We construct a multimodal database metadata knowledge graph, use the Transformer model and graph neural network to calculate table relevance, combine the Lingxi Youyan big language model for iterative SQL generation, and use the adapter pattern to achieve unified query access for multi-source heterogeneous data, thereby improving query accuracy and reliability.

Benefits of technology

By deeply understanding the relationships between tables and the attribution of fields, the system intelligently infers the most relevant tables and generates accurate SQL queries, solving the problem of SQL generation failure caused by incorrect table selection or inaccurate join conditions, and significantly improving the accuracy and reliability of complex cross-table queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120950542B_ABST
    Figure CN120950542B_ABST
Patent Text Reader

Abstract

The application discloses a multi-source heterogeneous database query method, system, device and medium, belongs to the technical field of natural language to SQL, and aims to solve the technical problem of how to overcome the defects of SQL query errors caused by the selection of wrong tables or inaccurate connection conditions in traditional natural language query, improve the accuracy and reliability of cross-table complex query, and the technical scheme is as follows: constructing a multi-modal database metadata knowledge graph: reading the metadata information of all data tables in the target database, and constructing a structured knowledge graph according to the database metadata information; table correlation collaborative calculation and sorting based on semantics and graph neural network: encoding the user problem into a semantic vector by using a Transformer model; iterative SQL generation and optimization based on confidence backtracking verification; unified query access of multi-source heterogeneous data based on the adapter mode.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the technical field of natural language to SQL (NL2SQL), in particular to a multi-source heterogeneous database query method, system, device and medium. BACKGROUND

[0002] The prior art mainly includes two types of methods based on traditional deep learning and large language models (LLM): the traditional method is highly dependent on "natural language-SQL" paired data, and its performance is limited by the size and quality of the training data; the method based on LLM can generate SQL, but still has the problems of "pattern hallucination" (i.e., generating references to non-existent tables or fields) in the prompt engineering mode, high training cost in the fine-tuning mode, and difficulty in fundamentally solving the accuracy of multi-table association judgment. These methods generally lack deep understanding of the relationships between database tables when dealing with complex multi-table queries, and often fail to generate SQL due to incorrect table selection or missing or complex table relationship processing.

[0003] Therefore, how to overcome the defects of incorrect SQL query caused by incorrect table selection or inaccurate connection conditions in traditional natural language query and improve the accuracy and reliability of cross-table complex query is a technical problem to be solved. SUMMARY

[0004] The technical task of the application is to provide a multi-source heterogeneous database query method, system, device and medium to solve the problem of how to overcome the defects of incorrect SQL query caused by incorrect table selection or inaccurate connection conditions in traditional natural language query and improve the accuracy and reliability of cross-table complex query.

[0005] The technical task of the application is achieved in the following manner: a multi-source heterogeneous database query method, which is specifically as follows:

[0006] Constructing a multi-modal database metadata knowledge graph: reading the metadata information of all data tables in the target database, and constructing a structured knowledge graph according to the database metadata information;

[0007] Table correlation collaborative calculation and sorting based on semantics and graph neural networks: using a Transformer model to encode the user question into a semantic vector, calculating the cosine similarity between the semantic vector and the semantic vector of each table to obtain an initial score, and then combining the structured knowledge graph to perform message passing and node embedding learning through a graph neural network, thereby obtaining a correlation score enhanced by the graph structure and generating an ordered list;

[0008] Iterative SQL generation and optimization based on confidence backtracking verification: traverse the candidate tables in order list order, add the candidate tables to the table set one by one, and generate SQL query statements using the Lingyu Youyan large language model, calculate the comprehensive confidence score through the Lingyu Youyan large language model, and judge whether to pass the verification, whether to trigger the backtracking mechanism or whether to reach the traversal termination condition according to the confidence score;

[0009] Unified query access of multi-source heterogeneous data based on adapter pattern: provide unified access and query support for different types of data sources through the adapter engine, and map heterogeneous data sources into standardized table structures and query interfaces.

[0010] As preferred, the metadata information of all data tables in the target database includes table name, table annotation, field name, field data type, field annotation, primary key constraint, foreign key constraint, and dictionary table.

[0011] As preferred, the structured knowledge graph includes three types of nodes (Nodes) : table nodes, field nodes, and dictionary item nodes, and three types of edges (Edges) : table-table relationship edges, table-field relationship edges, and field-dictionary item relationship edges.

[0012] Among them, the attributes of the table node include the table name and the table annotation.

[0013] The attributes of the field node include the field name, the type, and the annotation.

[0014] The attributes of the dictionary item node include the dictionary key and the dictionary value.

[0015] The direction of the table-table relationship edge is defined by the foreign key constraint, and contains a weight attribute, which represents the closeness of the correlation between the two tables.

[0016] The direction of the table-field relationship edge is from the table to the field, indicating "contains".

[0017] The direction of the field-dictionary item relationship edge is from the field to the dictionary item, indicating "limited to".

[0018] As preferred, the table correlation collaborative calculation and sorting based on semantics and graph neural network are as follows:

[0019] Receive user natural language questions, and encode them into semantic vectors through the Transformer model Vq ;

[0020] For each node in the structured knowledge graph, concatenate the table, key field, and associated dictionary item node attributes into a JSON text and encode it into a table vector Vt , and calculate the table vector Vt and the semantic vector VqThe cosine similarity is used to obtain the initial relevance score between each table and the corresponding question;

[0021] The initial relevance score and the structured knowledge graph are input into the graph neural network. Neighborhood information is aggregated through message passing, and node embeddings containing global structure are learned.

[0022] Finally, the fully connected layer outputs the correlation scores of each table enhanced by the graph structure, and sorts them in descending order, taking the top scores. n Items generate ordered list L .

[0023] As a preferred approach, the iterative SQL generation and optimization based on confidence backtracking verification is as follows:

[0024] (1) Initialization: Construct a table collection Get an ordered list L And set the maximum number of iterations. K and confidence threshold ;

[0025] (2) Select table: according to the ordered list L Select the next candidate table T in sequence. i ;

[0026] (3) Generate SQL: Generate SQL from candidate table T i Add to table collection and the table set The table structure and user question Q are input into the Lingxi Youyan big language model to obtain the candidate SQL statement SQL_current;

[0027] (4) Verification scoring: The user question Q and the set of tables are used to verify the scoring. The candidate SQL statement SQL_current is input into the Lingxi Youyan Big Language Model for verification, and the overall confidence score is calculated by the Lingxi Youyan Big Language Model. :

[0028] ;

[0029] in, Indicates semantic consistency score; Indicates the score for grammatical and structural correctness; This indicates pattern matching and executability scoring; α , β , γ These are the weighting coefficients;

[0030] (5) Result judgment, the situation is as follows:

[0031] ① If the overall confidence level If the verification is successful, the current candidate SQL statement SQL_current is returned as the final query statement, and the SQL generation process ends.

[0032] ②If the overall confidence level If the verification fails, proceed to step (6).

[0033] (6) Terminate the judgment, as follows:

[0034] ①If or ordered list L If the traversal has been completed, proceed to step (8);

[0035] ②If or ordered list L If the traversal is not complete, proceed to step (7);

[0036] (7) Backtracking judgment: ① If the overall confidence level If the value drops to the set threshold, it indicates that candidate table T... i Introducing a significant negative bias into the result generation from the table set Remove candidate table T i Proceed to step (2); ② If the overall confidence level is... If the value does not drop to the set threshold, proceed directly to step (2).

[0037] (8) Return failure information: Return a message to the user that the SQL generation failed, and the process ends.

[0038] More specifically, the unified query access for multi-source heterogeneous data based on the adapter pattern involves: introducing a multi-source heterogeneous adaptation engine, using the adapter pattern to provide unified access and query support for heterogeneous data sources such as relational databases, HTTP API interfaces, and Excel files. The adapter enables connection management, protocol conversion, and data mapping, and abstracts the differences in syntax and operation between different sources into a standardized table structure and query interface. This allows the generated SQL to be executed seamlessly in multi-source heterogeneous data environments, improving versatility and scalability.

[0039] A multi-source heterogeneous database query system, the system comprising:

[0040] A knowledge graph construction module is configured to read metadata information of all data tables in a target database and construct a structured knowledge graph according to the database metadata information; the metadata information of all data tables in the target database includes table names, table annotations, field names, field data types, field annotations, primary key (Primary Key) constraints, foreign key (Foreign Key) constraints and dictionary tables; the structured knowledge graph includes three types of nodes (Node), namely table nodes, field nodes and dictionary item nodes, and three types of edges (Edge), namely table-table relationship edges, table-field relationship edges and field-dictionary item relationship edges; wherein the attributes of the table nodes include table names and table annotations; the attributes of the field nodes include field names, types and annotations; the attributes of the dictionary item nodes include dictionary keys and dictionary values; the direction of the table-table relationship edge is defined by the foreign key constraint, and the table-table relationship edge includes a weight attribute, which is used to represent the closeness of the correlation between the two tables; the direction of the table-field relationship edge is from the table to the field, indicating "contains"; the direction of the field-dictionary item relationship edge is from the field to the dictionary item, indicating "limited to";

[0041] A table correlation collaborative calculation and sorting module is configured to encode a user question into a semantic vector using a Transformer model, calculate the cosine similarity between the semantic vector and the semantic vector of each table to obtain an initial score, and then combine the structured knowledge graph to perform message passing and node embedding learning through a graph neural network, thereby obtaining a correlation score enhanced by the graph structure and generating an ordered list;

[0042] A SQL generation and optimization module is configured to traverse the candidate tables in the order of the ordered list, add the candidate tables to a table set one by one, and generate SQL query statements using the Lingxi Youyan large language model, calculate a comprehensive confidence score through the Lingxi Youyan large language model, and determine whether to pass the verification, whether to trigger the backtracking mechanism or whether to reach the traversal termination condition according to the confidence score;

[0043] A multi-source heterogeneous data unified query access module is configured to provide unified access and query support for different types of data sources through an adapter engine, and map the heterogeneous data sources into a standardized table structure and query interface.

[0044] As a preferred, the comprehensive confidence is obtained by weighted calculation of the semantic consistency score, the syntax and structure correctness score, and the pattern matching and executability score;

[0045] When the comprehensive confidence does not pass the threshold verification, the next candidate table in the ordered list is continued to be traversed;

[0046] When the comprehensive confidence drops to a set threshold, the backtracking mechanism is triggered and the current poor candidate table is skipped to continue the traversal; wherein the traversal termination condition is that the size of the table set reaches a preset maximum value or all candidate tables in the ordered list have been traversed;

[0047] The heterogeneous data sources include a relational database, an HTTP API interface and an Excel file.

[0048] An electronic device, comprising: a memory and at least one processor;

[0049] The memory has stored thereon a computer program;

[0050] The at least one processor executes the computer program stored in the memory, so that the at least one processor executes the multi-source heterogeneous database query method as described above.

[0051] A computer readable storage medium, the computer readable storage medium has stored therein a computer program, the computer program can be executed by a processor to implement the multi-source heterogeneous database query method as described above.

[0052] The multi-source heterogeneous database query method, system, device and medium of the present application have the following advantages:

[0053] (1) The knowledge graph constructed based on metadata effectively expresses the inter-table association and field attribution relationship;

[0054] (2) The method combining semantic calculation and graph neural network shows good effect in capturing query intention and database structure characteristics, and improves the table selection accuracy in the multi-table scenario;

[0055] (3) After adopting the confidence evaluation and backtracking mechanism, the present application can reduce the generation of incorrect SQL and improve the reliability of complex queries;

[0056] (4) The unified data access layer realized by the adapter mode can adapt to different data source environments, and enhances the practicality and scope of application; compared with the prior art, the present application has practical improvements in query accuracy and multi-source data support;

[0057] (5) The present application automatically converts the user's natural language question into an executable SQL query statement, and solves the problem of SQL query error caused by incorrect table selection or inaccurate table structure in traditional natural language query through intelligent table selection and query optimization;

[0058] (Six) The application intelligently infers the most relevant table and generates an accurate SQL query by deeply utilizing table name, table relationship and field semantic information, rather than simply relying on literal matching of field names. Through semantic coding and graph structure learning, the application deeply understands the deep association between user query intention and database schema, thereby effectively solving the SQL generation failure problem caused by incorrect table selection, missing connection conditions and difficult processing of complex table relationships in the prior art, and significantly improving the accuracy and reliability of cross-multi-table complex queries.

[0059] (Seven) The application reduces the consumption of computing resources and guarantees query efficiency and accuracy through template matching of high-frequency requirements and linkage model processing of long-tail requirements, and is suitable for intelligent data analysis, database retrieval and other scenarios. BRIEF DESCRIPTION OF DRAWINGS

[0060] The application will be further described below with reference to the accompanying drawings.

[0061] ATTACHED Figure 1 is a flow chart of a multi-source heterogeneous database query method;

[0062] ATTACHED Figure 2 is a schematic diagram of constructing a structured knowledge graph. DETAILED DESCRIPTION

[0063] The multi-source heterogeneous database query method, system, device and medium of the application will be described in detail below with reference to the accompanying drawings and specific embodiments.

[0064] Embodiment 1: As shown in the attached Figure 1 , the present embodiment provides a multi-source heterogeneous database query method, which specifically comprises the following steps:

[0065] S1, constructing a multi-modal database metadata knowledge graph: reading the metadata information of all data tables in the target database, and constructing a structured knowledge graph according to the database metadata information;

[0066] S2, table relevance collaborative calculation and sorting based on semantics and graph neural network: encoding the user problem into a semantic vector using a Transformer model, calculating the cosine similarity of the semantic vector and each table semantic vector to obtain an initial score, and then combining the structured knowledge graph to perform message passing and node embedding learning through a graph neural network, thereby obtaining a relevance score enhanced by the graph structure and generating an ordered list;

[0067] S3, Iterative SQL generation and optimization based on confidence backtracking verification: traverse the candidate tables in order list order, add the candidate tables to the table set one by one, and generate SQL query statements by using the Lingyu Youyan large language model, calculate the comprehensive confidence score by using the Lingyu Youyan large language model, and determine whether to pass the verification, whether to trigger the backtracking mechanism or whether to reach the traversal termination condition according to the confidence score;

[0068] S4, Unified query access of multi-source heterogeneous data based on adapter pattern: providing unified access and query support for different types of data sources through the adapter engine, mapping heterogeneous data sources into standardized table structures and query interfaces.

[0069] The metadata information of all data tables in the target database in step S1 of the embodiment includes table name, table annotation, field name, field data type, field annotation, primary key (Primary Key) constraint, foreign key (Foreign Key) constraint and dictionary table.

[0070] The structured knowledge graph in step S1 of the embodiment includes three types of nodes (Node) including table nodes, field nodes and dictionary item nodes, and three types of edges (Edge) including table-table relationship edges, table-field relationship edges and field-dictionary item relationship edges.

[0071] The attributes of the table node include table name and table annotation.

[0072] The attributes of the field node include field name, type and annotation.

[0073] The attributes of the dictionary item node include dictionary key and dictionary value.

[0074] The direction of the table-table relationship edge is defined by the foreign key constraint, and includes a weight attribute, which is used to represent the closeness of the correlation between the two tables.

[0075] The direction of the table-field relationship edge is from the table to the field, indicating "contains".

[0076] The direction of the field-dictionary item relationship edge is from the field to the dictionary item, indicating "limited to".

[0077] The table correlation collaborative calculation and sorting based on semantics and graph neural network in step S2 of the embodiment is as follows:

[0078] S201, receive the user natural language question, and encode it into a semantic vector by using the Transformer model Vq ;

[0079] S202, for each node in the structured knowledge graph, concatenate the table, key field and associated dictionary item node attributes into a JSON text and encode it into a table vector Vt , and calculate the table vectorVt with the cosine similarity of semantic vectors Vq , obtain the initial relevance score of each table and the corresponding question;

[0080] S203, input the initial relevance score and the structured knowledge graph into the graph neural network, aggregate neighborhood information through message passing, and learn to obtain node embedding containing global structure;

[0081] S204, finally output the relevance score of each table enhanced by the graph structure through the full connection layer, and arrange in descending order in turn, take the first n item to generate an ordered list L .

[0082] The iterative SQL generation and optimization based on confidence backtracking verification in step S3 of the embodiment are as follows:

[0083] (1) Initialization: construct a table set , obtain an ordered list L , and set the maximum iteration number K and the confidence threshold ;

[0084] (2) Select table: select the next candidate table T i according to the ordered list L ;

[0085] (3) Generate SQL: add the candidate table T i to the table set , and input the table structure in the table set and the user question Q into the Big Language Model of the Big Language Model to obtain the candidate SQL statement SQL_current;

[0086] (4) Verification score: input the user question Q, the table set and the candidate SQL statement SQL_current into the Big Language Model to verify, and calculate the comprehensive confidence by the Big Language Model:

[0087] ;

[0088] Wherein, represents the semantic consistency score; represents the syntax and structure correctness score; represents the pattern matching and executability score; α , β , γ are weight coefficients;

[0089] (5) Result judgment, the following cases:

[0090] ① If the overall confidence level If the verification is successful, the current candidate SQL statement SQL_current is returned as the final query statement, and the SQL generation process ends.

[0091] ②If the overall confidence level If the verification fails, proceed to step (6).

[0092] (6) Terminate the judgment, as follows:

[0093] ①If or ordered list L If the traversal has been completed, proceed to step (8);

[0094] ②If or ordered list L If the traversal is not complete, proceed to step (7);

[0095] (7) Backtracking judgment: ① If the overall confidence level If the value drops to the set threshold, it indicates that candidate table T... i Introducing a significant negative bias into the result generation from the table set Remove candidate table T i Proceed to step (2); ② If the overall confidence level is... If the value does not drop to the set threshold, proceed directly to step (2).

[0096] (8) Return failure information: Return a message to the user that the SQL generation failed, and the process ends.

[0097] In this embodiment, step S4, the unified query access for multi-source heterogeneous data based on the adapter pattern, specifically involves: introducing a multi-source heterogeneous adaptation engine and using the adapter pattern to provide unified access and query support for heterogeneous data sources such as relational databases, HTTP API interfaces, and Excel files. The adapter enables connection management, protocol conversion, and data mapping, and abstracts the differences in syntax and operation between different sources into a standardized table structure and query interface, so that the generated SQL can be seamlessly executed in a multi-source heterogeneous data environment, improving versatility and scalability.

[0098] Example 2: Taking the query task "Query the average grade of students in the Computer Science Department" in an education management scenario as an example, the workflow is as follows: First, access the database metadata to identify the table structures such as the student table (including student ID, name, and college ID), the course selection table (including student ID, course ID, and grade), the course table (including course ID and course name), and the college table (including college ID and college name). Then, construct a knowledge graph containing table nodes and field nodes based on foreign key relationships, as shown in the attached figure. Figure 2The user question is encoded into a semantic vector using a Transformer model, while the table names and key field information of each table are also encoded into vectors. The initial table association scores are obtained by calculating the cosine similarity, with the course table obtaining the highest initial score due to the inclusion of the "score" field. The system then inputs the initial scores and the knowledge graph into a graph neural network, aggregates neighborhood information through a message passing mechanism, and finally outputs the relevance scores enhanced by the graph structure, generating an ordered table list: course table > student table > college table > course table. Based on this ranking, the iterative query generation process is started: first, only the top-ranked course table is selected to generate SQL through the Lingxi Youyan large language model, which can achieve average score calculation but cannot reflect the college constraint. The verification module determines that the confidence is insufficient and fails; then the system connects the course table with the student table to generate a new SQL query through the Lingxi Youyan large language model, which establishes a student association but still lacks a college name filtering condition, and the verification fails again; finally, the system introduces the college table to form a three-table connection, successfully constructing a complete SQL statement containing the "college name = 'Computer College'" constraint condition and the AVG(score) aggregation operation. After the verification module confirms the semantic correctness and executability of the statement, it is executed, and finally the average score of the students in the Computer College is accurately returned.

[0099] Embodiment 3: The present embodiment provides a multi-source heterogeneous database query system, which comprises:

[0100] A knowledge graph construction module is configured to read the metadata information of all data tables in the target database, and construct a structured knowledge graph according to the database metadata information; the metadata information of all data tables in the target database includes table name, table annotation, field name, field data type, field annotation, primary key constraint, foreign key constraint, and dictionary table; the structured knowledge graph includes three types of nodes (Node), i.e., table node, field node, and dictionary item node, and three types of edges (Edge), i.e., table-table relationship edge, table-field relationship edge, and field-dictionary item relationship edge; wherein the attributes of the table node include table name and table annotation; the attributes of the field node include field name, type, and annotation; the attributes of the dictionary item node include dictionary key and dictionary value; the direction of the table-table relationship edge is defined by the foreign key constraint, and includes a weight attribute, which is used to represent the closeness of the correlation between two tables; the direction of the table-field relationship edge is from the table to the field, indicating "contains"; the direction of the field-dictionary item relationship edge is from the field to the dictionary item, indicating "limited to";

[0101] A table correlation synergy calculation and ranking module is configured to encode a user question into a semantic vector using a Transformer model, calculate a cosine similarity between the semantic vector and semantic vectors of each table to obtain an initial score, and combine a structured knowledge graph to perform message passing and node embedding learning through a graph neural network, thereby obtaining a correlation score enhanced by a graph structure and generating an ordered list.

[0102] An SQL generation and optimization module is configured to traverse candidate tables in the order of the ordered list, add the candidate tables to a table set one by one, and generate SQL query statements using the ConverseAI large language model, calculate a comprehensive confidence score through the ConverseAI large language model, and determine whether to pass the verification, trigger a backtracking mechanism, or reach a traversal termination condition according to the confidence score.

[0103] A multi-source heterogeneous data unified query access module is configured to provide unified access and query support for different types of data sources through an adapter engine, and map heterogeneous data sources into standardized table structures and query interfaces.

[0104] The comprehensive confidence in this embodiment is obtained by weighted calculation of semantic consistency scores, syntax and structure correctness scores, and pattern matching and executability scores.

[0105] When the comprehensive confidence does not pass the threshold verification, the next candidate table in the ordered list is traversed.

[0106] When the comprehensive confidence drops to a set threshold, the backtracking mechanism is triggered and the current bad candidate table is skipped to continue traversal. The traversal termination condition is that the size of the table set reaches a preset maximum value or all candidate tables in the ordered list have been traversed.

[0107] The heterogeneous data sources in this embodiment include relational databases, HTTP API interfaces, and Excel files.

[0108] Embodiment 4: The embodiment also provides an electronic device, comprising: a memory and at least one processor.

[0109] The memory stores computer execution instructions.

[0110] The at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor performs the multi-source heterogeneous database query method in any embodiment of the application.

[0111] The processor can be a central processing unit (CPU), and can also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), or other programmable logic devices, discrete gates or transistor logic components, discrete hardware components, etc. The processor can be a microprocessor or can be any conventional processor.

[0112] The memory can be used to store computer programs and / or modules, and the processor can realize various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory, and calling data stored in the memory. The memory can mainly include a program storage area and a data storage area, wherein the program storage area can store operating systems, application programs required by at least one function, etc.; and the data storage area can store data created according to the use of the terminal, etc. In addition, the memory can also include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (SMC), a secure digital (SD) card, a flash memory card, at least one disk storage device, a flash memory device, or other volatile solid-state memory devices.

[0113] Embodiment 5: The embodiment also provides a computer-readable storage medium, in which a plurality of instructions are stored, the instructions are loaded by a processor to make the processor execute the multi-source heterogeneous database query method in any embodiment of the application. Specifically, a system or device equipped with a storage medium can be provided, and the storage medium stores software program codes for realizing the functions of any one of the above embodiments, and the computer (or CPU or MPU) of the system or device reads and executes the program codes stored in the storage medium.

[0114] In this case, the program codes read from the storage medium can realize the functions of any one of the above embodiments, and thus the program codes and the storage medium storing the program codes constitute a part of the application.

[0115] The storage medium for providing the program codes includes floppy disks, hard disks, magneto-optical disks, optical disks (such as CD-ROM, CD-R, CD-RW, DVD-ROM, DVD-RYM, DVD-RW, DVD+RW), magnetic tapes, non-volatile memory cards, and ROMs. Alternatively, the program codes can be downloaded from a server computer via a communication network.

[0116] In addition, it should be clear that not only the program codes read by the computer can be executed, but also part or all of the actual operations can be completed by the operating system or the like operating on the computer based on the instructions of the program codes, so as to realize the functions of any one of the above embodiments.

[0117] Further, it is understood that the program code read by the storage medium can be written into the memory provided in the expansion board inserted into the computer or the memory provided in the expansion unit connected to the computer, and then the CPU or the like mounted on the expansion board or the expansion unit is caused to perform part or all of the actual operation based on the instruction of the program code, thereby realizing the function of any of the above-described embodiments.

[0118] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, and are not limited thereto; although the present application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that the technical solutions recorded in the foregoing embodiments can still be modified, or part or all of the technical features can be replaced by equivalents; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for multi-source heterogeneous database query, characterized in that, The method specifically comprises the following steps: Constructing a multi-modal database metadata knowledge graph: read the metadata information of all data tables in the target database, and construct a structured knowledge graph according to the database metadata information; Table correlation collaborative calculation and ranking based on semantics and graph neural networks: encode the user question into a semantic vector using a Transformer model, and calculate the cosine similarity between the semantic vector and the semantic vector of each table to obtain an initial score. Then, combine the structured knowledge graph to perform message passing and node embedding learning through a graph neural network, and then obtain a correlation score enhanced by the graph structure and generate an ordered list; Iterative SQL generation and optimization based on confidence backtracking verification: traverse the candidate tables in the order of the ordered list, add the candidate tables to the table set one by one, and generate SQL query statements using the Lingxi Hanyan large language model. Calculate the comprehensive confidence score through the Lingxi Hanyan large language model, and determine whether to pass the verification, whether to trigger the backtracking mechanism, or whether to reach the traversal termination condition according to the confidence score; Unified query access of multi-source heterogeneous data based on adapter pattern: provide unified access and query support for different types of data sources through the adapter engine, and map heterogeneous data sources into standardized table structures and query interfaces; Iterative SQL generation and optimization based on confidence backtracking verification specifically comprises the following steps: (1) initialization: construct a table set , get an ordered list L , and set the maximum iteration number K and the confidence threshold ; (2) Select table: according to the ordered list L , sequentially select the next candidate table T i ; (3) generating SQL: inputting the candidate table T i into the table set and inputting the table structure in the table set and the user question Q into the Replika large language model to obtain a candidate SQL statement SQL_current; (4) Verification score: input the user question Q, the table set T and the candidate SQL statement SQL current into the Lingxihuyangdaling language model for verification, and calculate the comprehensive confidence score C by the Lingxihuyangdaling language model : C = f(Q, T, SQL current) : ; wherein, represents a semantic consistency score; represents a syntactic and structural correctness score; represents a pattern matching and executability score; α , β , γ is a weight coefficient; (5) Result judgment, the situation is as follows: ① If the overall confidence level If the verification is successful, the current candidate SQL statement SQL_current is returned as the final query statement, and the SQL generation process ends. If the combined confidence level is then the verification fails and the next step is step (6); (6) Termination judgment, the situation is as follows: If or ordered list L has been traversed, jump to step (8); If or an ordered list L If not, then the next step is step (7). (7) Backtracking judgment: ① If the comprehensive confidence degree falls to a set threshold, it means that the candidate table T i has a significant negative bias, and the result generation introduces a significant negative bias, so the candidate table T is removed from the table set i , and the process jumps to step (2); ② If the comprehensive confidence degree does not fall to a set threshold, it directly jumps to step (2). (8) Return failure information: return the SQL generation failure prompt to the user, and the process ends.

2. The multi-source heterogeneous database query method of claim 1, wherein, The metadata information of all data tables in the target database includes table name, table annotation, field name, field data type, field annotation, primary key constraint, foreign key constraint, and dictionary table.

3. The multi-source heterogeneous database query method of claim 1, wherein, The structured knowledge graph includes three types of nodes: table nodes, field nodes, and dictionary item nodes, and three types of edges: table-table relationship edges, table-field relationship edges, and field-dictionary item relationship edges; Among them, the attributes of the table node include the table name and the table annotation; The attributes of the field node include the field name, the type, and the annotation; The attributes of the dictionary item node include the dictionary key and the dictionary value; The direction of the table-table relationship edge is defined by the foreign key constraint, and includes a weight attribute, which represents the closeness of the correlation between the two tables; The direction of the table-field relationship edge is from the table to the field, indicating "contains"; The direction of the field-dictionary item relationship edge is from the field to the dictionary item, indicating "limited to".

4. The multi-source heterogeneous database query method of claim 1, wherein, The table correlation collaborative calculation and ranking based on semantics and graph neural networks specifically comprises the following steps: receiving a user natural language question, encoding into a semantic vector via a Transformer model Vq ; For each node in the structured knowledge graph, the table, key field and associated dictionary item node attributes of each node are spliced into a JSON text and encoded into a table vector Vt , and the table vector is calculated Vt , and the cosine similarity of the semantic vector Vq is calculated, to obtain the initial relevance score of each table and the corresponding question; Input the initial correlation score and the structured knowledge graph into the graph neural network, aggregate the neighborhood information through message passing, and learn to obtain node embeddings containing global structure; Finally, the correlation scores of each table are output through the full connection layer, and are arranged in descending order in turn, and the top n Item generation ordered list L .

5. The multi-source heterogeneous database query method according to any one of claims 1 to 4, characterized in that, The multi-source heterogeneous data unified query access based on the adapter mode is specifically: introducing a multi-source heterogeneous adaptation engine, using an adapter mode to provide unified access and query support for heterogeneous data sources of relational databases, HTTP API interfaces and Excel files, realizing connection management, protocol conversion and data mapping through the adapter, and abstracting the syntax and operation differences of different sources into standardized table structures and query interfaces, so that the generated SQL can be seamlessly executed in a multi-source heterogeneous data environment, and the generality and expansibility are improved.

6. A multi-source heterogeneous database query system, characterized in that, The system comprises: a knowledge graph construction module configured to read metadata information of all data tables in a target database, and construct a structured knowledge graph according to the database metadata information; the metadata information of all data tables in the target database comprises table names, table annotations, field names, field data types, field annotations, primary key constraints, foreign key constraints and dictionary tables; the structured knowledge graph comprises three types of nodes, i.e., table nodes, field nodes and dictionary item nodes, and three types of edges, i.e., table-table relationship edges, table-field relationship edges and field-dictionary item relationship edges; wherein the attributes of the table nodes comprise table names and table annotations; the attributes of the field nodes comprise field names, types and annotations; the attributes of the dictionary item nodes comprise dictionary keys and dictionary values; the direction of the table-table relationship edges is defined by the foreign key constraint, and the table-table relationship edges comprise a weight attribute, which is used to represent the closeness of the correlation between two tables; the direction of the table-field relationship edges is from the table to the field, indicating "contains"; the direction of the field-dictionary item relationship edges is from the field to the dictionary item, indicating "limited to"; a table correlation collaborative calculation and sorting module configured to encode a user question into a semantic vector by using a Transformer model, calculate a cosine similarity between the semantic vector and semantic vectors of all tables to obtain an initial score, and then combine the structured knowledge graph to perform message passing and node embedding learning by using a graph neural network, so as to obtain a correlation score enhanced by a graph structure and generate an ordered list; a SQL generation and optimization module configured to traverse candidate tables in the order of the ordered list, add the candidate tables into a table set one by one, and generate a SQL query statement by using a Lingxi Youyan large language model, calculate a comprehensive confidence score by using the Lingxi Youyan large language model, and determine whether to pass the verification, whether to trigger a backtracking mechanism or whether to reach a traversal termination condition according to the confidence score; a multi-source heterogeneous data unified query access module configured to provide unified access and query support for different types of data sources by using an adapter engine, and map the heterogeneous data sources into standardized table structures and query interfaces; wherein the comprehensive confidence score is obtained by weighted calculation of a semantic consistency score, a syntax and structure correctness score and a pattern matching and executability score; when the comprehensive confidence score does not pass the threshold verification, the next candidate table in the ordered list is continuously traversed; when the comprehensive confidence score drops to a set threshold, a backtracking mechanism is triggered, and the current poor candidate table is skipped and then continuously traversed; wherein the traversal termination condition is that the size of the table set reaches a preset maximum value or all candidate tables in the ordered list have been traversed; the heterogeneous data sources comprise relational databases, HTTP API interfaces and Excel files.

7. An electronic device, comprising: comprise: a memory and at least one processor; wherein the memory has stored thereon computer programs; the at least one processor executes the computer programs stored in the memory, so that the at least one processor performs the multi-source heterogeneous database query method according to any one of claims 1 to 5.

8. A computer-readable storage medium, characterized in that, The computer readable storage medium has stored therein a computer program, which can be executed by a processor to implement the multi-source heterogeneous database query method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Data intelligent question and answer method and system fusing domain knowledge

    CN118779438A

  • Database query method and device, electronic equipment and nonvolatile storage medium

    CN119226315A

  • Automatic SQL tuning method and system combining large language model and database optimizer, computer equipment and storage medium

    CN120670464A