Database query method and device based on natural language and storage medium

Through a natural language-based database query method, the database semantic library and large language model are used to generate and optimize database query statements, the problem of high complexity of traditional database query methods is solved, and the efficiency of users querying databases through natural language is achieved.

CN119961283APending Publication Date: 2025-05-09BEIJING GLOBAL SAFETY TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510006978.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-02
Publication Date
2025-05-09

AI Technical Summary

Technical Problem

Traditional database query methods rely on proficiency in structured query language (SQL), making it difficult for non-professionals to obtain the required information from the database.

Method used

A natural language-based database query method is proposed. By obtaining natural language query text, the database semantic library and large language model are used to generate database query statements that are adapted to the target database, and the query statements are optimized to improve execution efficiency.

Benefits of technology

This allows users to obtain query results in the database through natural language without mastering complex database query languages, which improves query efficiency and reduces operational complexity.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119961283A_ABST
    Figure CN119961283A_ABST
Patent Text Reader

Abstract

The invention provides a database query method and device based on a natural language and a storage medium, and the method comprises the steps: obtaining a natural language query text for a target database, obtaining target database semantics matched with the natural language query text from a database semantic database constructed based on the target database, and obtaining a target database semantic database, the target database semantics and the natural language query text are input into a large language model, so that a first database query statement matched with the target database is generated through the large language model, and the first database query statement is optimized; the execution efficiency of the second database query statement is higher than that of the first database query statement, the second database query statement is queried in the target database, and a query result corresponding to the natural language query text is obtained. Therefore, the user can obtain the required information in the database through natural language input without mastering a complex database query language.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a database query method, device and storage medium based on natural language. Background Art

[0002] With the rapid increase in data volume and the increasing complexity of database structure, traditional database query methods rely on users who are proficient in structured query language (SQL) or other query languages. For non-professionals who lack SQL language and database expertise, although the database contains a large amount of rich data, many people find it difficult to obtain the required information from it. Therefore, how to enable users to obtain the required information in the database in a convenient way is a technical problem that needs to be solved urgently. Summary of the invention

[0003] The present application proposes a database query method, device and storage medium based on natural language.

[0004] In one aspect, an embodiment of the present application proposes a natural language-based database query method, comprising: obtaining a natural language query text for a target database; obtaining a target database semantics matching the natural language query text from a database semantic library, wherein the database semantic library is pre-constructed based on the target database; using a large language model to generate a first database query statement adapted to the target database based on the natural language query text and the target database semantics; optimizing the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database; and running the second database query statement in the target database to obtain a query result corresponding to the natural language query text.

[0005] In one embodiment of the present application, obtaining target database semantics that match the natural language query text from a database semantic library includes: determining the semantic representation of the natural language query text; determining the degree of match between the semantic representation of the natural language query text and each database semantic in the database semantic library; sorting each database semantic in the database semantic library according to the degree of match to obtain a sorting result; and obtaining the top N database semantics from the sorting result as the target database semantics, where N is an integer greater than or equal to 1.

[0006] In one embodiment of the present application, the optimizing the first database query statement to obtain the second database query statement includes: using the large language model to generate the second database query statement according to database performance data of the target database and the first database query statement.

[0007] In one embodiment of the present application, the optimizing processing of the first database query statement to obtain the second database query statement includes: when it is determined that the first database query statement does not limit the data filtering range, outputting a first optimization suggestion, wherein the first optimization suggestion is used to indicate that the data filtering range is limited for the first database query statement; determining a target data filtering range limited for the first database query statement based on a response result returned for the first optimization suggestion; and processing the first database query statement based on the target data filtering range to obtain the second database query statement, wherein the data filtering range in the second database query statement is the target data filtering range.

[0008] In one embodiment of the present application, the optimizing the first database query statement to obtain the second database query statement includes: when it is determined that the connection order of the multiple database tables in the target database in the first database query statement is the first connection order, outputting a second optimization suggestion, wherein the second optimization suggestion is used to indicate whether to adjust the connection order of the multiple database tables to the second connection order, wherein the efficiency of executing the first database query statement in the second connection order is higher than that in the second connection order; when it is determined that the connection order of the multiple database tables is adjusted to the second connection order according to a response result of the second optimization suggestion, processing the first database query statement according to the second connection order to obtain the second database query statement, wherein the connection order of the multiple database tables in the second database query statement is the second connection order.

[0009] In one embodiment of the present application, the optimization processing of the first database query statement to obtain the second database query statement includes: when it is determined that the first database query statement includes a sorting operation, outputting a third optimization suggestion, wherein the third optimization suggestion is used to indicate setting a restriction condition for the result returned by the sorting operation; determining a target restriction condition set for the sorting operation based on a response result returned for the third optimization suggestion; and processing the first database query statement based on the target restriction condition to obtain the second database query statement, wherein the restriction condition set for the result returned by the sorting operation in the second database query statement is the target restriction condition.

[0010] In one embodiment of the present application, the method of pre-constructing the database semantic library according to the target database is: obtaining a knowledge graph pre-constructed for the target database, wherein each node in the knowledge graph is a database element in the target database, and the associated edges between the nodes are the association relationships between the corresponding database elements; determining the semantic representation of the nodes and the associated edges in the knowledge graph through a graph neural network; and constructing the database semantic library according to the semantic representation of the nodes and the associated edges.

[0011] The database query method based on natural language in the embodiment of the present application, after obtaining the natural language query text for the target database, obtains the target database semantics that matches the natural language query text from the database semantic library constructed based on the target database, and inputs the target database semantics and the natural language query text into the large language model, so as to generate a first database query statement adapted to the target database through the large language model, and optimizes the first database query statement to obtain a second database query statement with higher execution efficiency than the first database query statement, and queries the second database query statement in the target database to obtain the query result corresponding to the natural language query text. As a result, the user does not need to master the complex database query language, and can obtain the corresponding query result in the database by inputting the natural language, which facilitates the user to query information in the database while improving the efficiency of obtaining the query result.

[0012] On the other hand, an embodiment of the present application proposes a natural language-based database query device, including: a first acquisition module, used to acquire a natural language query text for a target database; a second acquisition module, used to acquire the target database semantics matching the natural language query text from a database semantic library, wherein the database semantic library is pre-constructed based on the target database; a generation module, used to adopt a large language model to generate a first database query statement adapted to the target database according to the natural language query text and the target database semantics; an optimization processing module, used to optimize the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database; a query module, used to run the second database query statement in the target database to obtain a query result corresponding to the natural language query text.

[0013] The database query device based on natural language in the embodiment of the present application, after obtaining the natural language query text for the target database, obtains the target database semantics that matches the natural language query text from the database semantic library constructed based on the target database, and inputs the target database semantics and the natural language query text into the large language model, so as to generate a first database query statement adapted to the target database through the large language model, and optimizes the first database query statement to obtain a second database query statement with higher execution efficiency than the first database query statement, and queries the second database query statement in the target database to obtain the query result corresponding to the natural language query text. As a result, the user does not need to master the complex database query language, and can obtain the corresponding query result in the database by inputting the natural language, which facilitates the user to query information in the database while improving the efficiency of obtaining the query result.

[0014] On the other hand, an embodiment of the present application proposes an electronic device, including: a memory and a processor; the memory stores computer instructions, and when the computer instructions are executed by the processor, the natural language-based database query method of the embodiment of the present application is implemented.

[0015] On the other hand, an embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the natural language-based database query method of the embodiment of the present application.

[0016] Another aspect of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the natural language-based database query method of the embodiment of the present application.

[0017] Other effects of the above optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present application.

[0019] Figure 1 is a flowchart of a natural language-based database query method according to an embodiment of the present application;

[0020] Figure 2 is a schematic diagram of a process of pre-building a database semantic library according to a target database according to an embodiment of the present application;

[0021] Figure 3 is a flowchart of a natural language-based database query method according to another embodiment of the present application;

[0022] Figure 4 is a schematic diagram of the structure of a database query device based on natural language according to an embodiment of the present application;

[0023] Figure 5 It is a block diagram of an electronic device used to implement the natural language-based database query method of an embodiment of the present application. DETAILED DESCRIPTION

[0024] Embodiments of the present invention are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.

[0025] The following describes the natural language-based database query method, device, and electronic device according to an embodiment of the present application with reference to the accompanying drawings.

[0026] Figure 1 It is a flowchart of a natural language-based database query method according to an embodiment of the present application. It should be noted that the execution subject of the natural language-based database query method provided in this embodiment is a natural language-based database query device, which can be implemented by software and / or hardware, and the database query device can be an electronic device, or can be configured in an electronic device to implement a database query function.

[0027] Among them, the electronic device can be any device with computing capabilities, such as a personal computer, a mobile terminal, a server, etc. The mobile terminal can be, for example, a vehicle-mounted device, a mobile phone, a tablet computer, a personal digital assistant, a wearable device, and other hardware devices with various operating systems, touch screens and / or display screens.

[0028] like Figure 1 As shown, the natural language-based database query method may include:

[0029] Step 101: Obtain a natural language query text for a target database.

[0030] The natural language query text may be input by a user when searching for information in a target database.

[0031] The natural language query text refers to the query text input in natural language.

[0032] It should be noted that the target database in this embodiment can be any type of database, and this embodiment does not specifically limit the target database.

[0033] Step 102: Acquire the target database semantics matching the natural language query text from the database semantic library, wherein the database semantic library is pre-built according to the target database.

[0034] In one embodiment of the present application, a possible implementation method for obtaining target database semantics matching natural language query text from a database semantic library is: determining the semantic representation of the natural language query text; determining the matching degree between the semantic representation of the natural language query text and each database semantic in the database semantic library; sorting each database semantic in the database semantic library according to the matching degree to obtain a sorting result; obtaining the top N database semantics from the sorting result as the target database semantics, where N is an integer greater than or equal to 1. Thus, the target database semantics matching the natural language query text is accurately obtained from the database semantic library.

[0035] Wherein, N is preset according to actual needs, and this embodiment does not specifically limit the value of N.

[0036] In this embodiment, a possible implementation manner of determining the semantic representation of the natural language query text is: performing semantic representation on the natural language query text to obtain the semantic representation of the natural language query text.

[0037] As an example, the natural language query text may be input into a trained semantic representation model to obtain a semantic representation of the natural language query text through the semantic representation model.

[0038] In this embodiment, in order to facilitate efficient matching of natural language query text with the database semantic library, a dense vector representation method may be used to semantically represent the natural voice query text.

[0039] Step 103: using a large language model, based on the natural language query text and the semantics of the target database, generate a first database query statement adapted to the target database.

[0040] In this embodiment, a first prompt word can be generated according to the natural language query text and the semantics of the target database, and the first prompt word can be input into the large language model to obtain a first database query statement adapted to the target database through the large language model, wherein the first prompt word is used to instruct the large language model to generate a database query statement adapted to the target database according to the natural language query text and the semantics of the target database.

[0041] For example, the target database supports the SQL language, and correspondingly, the first database query statement generated by the large language model may be a SQL query statement.

[0042] In some exemplary embodiments, in order to further improve the accuracy of the first database query statement generated by the large language model, correspondingly, a query intent analysis can be performed on the natural language query text to obtain a structured query intent of the natural language query text. Correspondingly, a large language model is used to generate a first database query statement adapted to the target database based on the structured query intent and the target database semantics.

[0043] For example, the natural language query text input by the user is: the user asks about the recent sales trend. The target database semantics corresponding to the natural language query text can be obtained from the database semantic library. Then, a large language model is used to generate an SQL query statement based on the natural language query text and the target database semantics.

[0044] Step 104: Optimize the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database.

[0045] It is understandable that in different application scenarios, the first database query statement is optimized to obtain the second database query statement in different implementation methods, which are exemplarily described as follows:

[0046] As an exemplary implementation method, when it is determined that the first database query statement does not limit the data filtering range, a first optimization suggestion is output, wherein the first optimization suggestion is used to indicate that the data filtering range is limited for the first database query statement; based on the response result returned for the first optimization suggestion, a target data filtering range limited for the first database query statement is determined; based on the target data filtering range, the first database query statement is processed to obtain a second database query statement, wherein the data filtering range in the second database query statement is the target data filtering range.

[0047] As another exemplary implementation, when it is determined that the connection order of multiple database tables in the target database in the first database query statement is the first connection order, a second optimization suggestion is output, wherein the second optimization suggestion is used to indicate whether to adjust the connection order of the multiple database tables to the second connection order, wherein the efficiency of executing the first database query statement in the second connection order is higher than that in the second connection order; when it is determined that the connection order of the multiple database tables is adjusted to the second connection order according to a response result of the second optimization suggestion, the first database query statement is processed according to the second connection order to obtain a second database query statement, wherein the connection order of the multiple database tables in the second database query statement is the second connection order.

[0048] As another exemplary implementation, when it is determined that the first database query statement includes a sorting operation, a third optimization suggestion is output, wherein the third optimization suggestion is used to indicate setting a restriction condition for the result returned by the sorting operation; based on the response result returned for the third optimization suggestion, a target restriction condition set for the sorting operation is determined; based on the target restriction condition, the first database query statement is processed to obtain a second database query statement, wherein the restriction condition set for the result returned by the sorting operation in the second database query statement is the target restriction condition.

[0049] As another exemplary implementation, the first database query statement may be optimized by a query optimizer to obtain a second database query statement.

[0050] In this embodiment, the query optimizer can analyze the execution plan of the first database query statement to identify potential bottleneck operations, and determine the performance bottleneck of the first database query statement based on the identified bottleneck operation, and optimize the first database query statement based on the performance bottleneck and the bottleneck operation to obtain a second database query statement, or generate optimization suggestions for the first database query statement based on the performance bottleneck and the bottleneck operation, and output the optimization suggestions, and optimize the first database query statement based on the response result of the optimization suggestion to obtain a second database query statement.

[0051] In this embodiment, the optimization suggestions may be output in a readable and operable manner, for example, the optimization suggestions may be output through interactive feedback, or by outputting prompt information, or by outputting an optimization report, etc. This embodiment does not specifically limit the manner of outputting the optimization suggestions.

[0052] As another exemplary implementation, a large language model is used to generate a second database query statement based on database performance data of the target database and the first database query statement.

[0053] In this embodiment, the database performance data of the target database and the first database query statement may be input into the large language model to generate the second database query statement through the large language model.

[0054] The database performance data of the target database refers to data related to the performance of the target database, and the database performance data may include but is not limited to the load information, parallel execution status, etc. of the target database.

[0055] In one embodiment of the present application, in order to further improve the accuracy of the generated second database query statement, correspondingly, in addition to inputting the database performance data of the target database into the large language model, at least one of the following information can also be input into the large language model: query optimization requirements, metadata of the target database, cache information of the target database, etc.

[0056] In one embodiment of the present application, a second prompt word may be generated based on the database performance data of the target database and the first database query statement, and the second prompt word may be input into the large language model to obtain the second database query statement. The second prompt word is used to instruct the large language model to optimize the first database query statement based on the database performance data of the target database.

[0057] Step 105: Run the second database query statement in the target database to obtain a query result corresponding to the natural language query text.

[0058] In this embodiment, after the query result is obtained, the query result may be output.

[0059] In one embodiment of the present application, the query results may be output in a visual manner so that the user can obtain the query results intuitively.

[0060] The database query method based on natural language in the embodiment of the present application, after obtaining the natural language query text for the target database, obtains the target database semantics that matches the natural language query text from the database semantic library constructed based on the target database, and inputs the target database semantics and the natural language query text into the large language model, so as to generate a first database query statement adapted to the target database through the large language model, and optimizes the first database query statement to obtain a second database query statement with higher execution efficiency than the first database query statement, and queries the second database query statement in the target database to obtain the query result corresponding to the natural language query text. As a result, the user does not need to master the complex database query language, and can obtain the corresponding query result in the database by inputting the natural language, which facilitates the user to query information in the database while improving the efficiency of obtaining the query result.

[0061] In order to clearly understand this application, Figure 2 The process of pre-building a database semantic library according to a target database is described exemplarily.

[0062] Figure 2 It is a flowchart of pre-building a database semantic library according to a target database according to an embodiment of the present application.

[0063] like Figure 2 As shown, this may include:

[0064] Step 201, obtain a knowledge graph pre-built for the target database, wherein each node in the knowledge graph is a database element in the target database, and the associated edges between the nodes are the association relationships between the corresponding database elements.

[0065] In this embodiment, the knowledge graph of the target database can be constructed based on the database elements in the target database and the associations between the database elements. In other words, the knowledge graph of the target database can be constructed based on the database structure information of the target database.

[0066] In one embodiment of the present application, a structural analysis may be performed on the target database to obtain a structural analysis result of the target database, and a knowledge graph of the target database may be constructed based on the structural analysis result.

[0067] The database elements may include but are not limited to tables, fields, views, indexes, triggers, records, table meanings, field meanings, keys, constraints, etc.

[0068] In some embodiments, the nodes in the knowledge graph may include semantic information of database elements. Correspondingly, the associated edges of the knowledge graph may also include semantic information of the association relationship between database elements.

[0069] It should be noted that the knowledge graph clarifies the semantic information of the database elements in the target database and the semantic information of the association relationship between the database elements.

[0070] Step 202: Determine the semantic representation of nodes and associated edges in the knowledge graph through a graph neural network.

[0071] In some exemplary embodiments, the knowledge graph may be input into a graph neural network, and correspondingly, the graph neural network performs representation learning on the knowledge graph of the target database to determine the semantic representations of the nodes and associated edges in the knowledge graph.

[0072] Among them, it can be understood that the graph neural network can understand the complex relationships between tables and fields in the target database, and can also identify the correlation between tables and fields in the target database. In addition, the graph neural network can also capture more complex context and relationship information.

[0073] Step 203: construct a database semantic library based on the semantic representation of the nodes and associated edges.

[0074] In this embodiment, a database semantic library is established based on the semantic representation of nodes and associated edges output by the graph neural network, so that the database semantics in the database semantic library can fully reflect the structure of the target database and its logical relationships, making it convenient for the subsequent large language model to accurately generate database query statements adapted to the target database based on the corresponding database semantics in the database semantic library.

[0075] In order to clearly understand this application, Figure 3 The method of this embodiment is described exemplarily.

[0076] Figure 3 It is a flowchart of a natural language-based database query method according to another embodiment of the present application.

[0077] like Figure 3 As shown, the method may include:

[0078] Step 301: Obtain natural language query text for a target database.

[0079] It should be noted that, for the specific description of step 301, reference may be made to the relevant descriptions in other embodiments, which will not be repeated here.

[0080] Step 302: Determine the semantic representation of the natural language query text.

[0081] For a specific description of determining the semantic representation of the natural language query text, reference may be made to the relevant descriptions in other embodiments, which will not be repeated here.

[0082] Step 303: According to the semantic representation of the natural language query text and the matching degree between the database semantics in the database semantic library, the target database semantics matching the natural language query text is obtained from the database semantic library.

[0083] Among them, the specific process of obtaining the target database semantics matching the natural language query text from the database semantic library based on the semantic representation of the natural language query text and the matching degree between the database semantics in the database semantic library can be found in the relevant descriptions in other embodiments and will not be repeated here.

[0084] Step 304: using a large language model, based on the natural language query text and the semantics of the target database, generate a first database query statement adapted to the target database.

[0085] For the specific description of step 304, please refer to the relevant description in other embodiments, which will not be repeated here.

[0086] Step 305 , determining whether the first database query statement has a performance bottleneck through the query optimizer, if yes, executing step 306 , otherwise executing step 309 .

[0087] Step 306: Generate optimization suggestions for the first database query statement based on the performance bottleneck, and output the optimization suggestions.

[0088] Step 307: Process the first database query statement according to the response result of the optimization suggestion to obtain a second database query statement.

[0089] The execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database.

[0090] Step 308: execute the second database query statement in the target database to obtain the first query result, and output the first query result.

[0091] In one embodiment of the present application, in order to efficiently return the corresponding query results, correspondingly, in the process of executing the second database query statement in the target database, the second database query statement can also be processed in combination with the database caching mechanism and index optimization technology to improve the efficiency of obtaining the first query results.

[0092] Step 309: execute the first database query statement in the target database to obtain a second query result, and output the second query result.

[0093] In some embodiments, after generating a first database query statement, if the query optimizer determines that the first database query statement has a performance bottleneck, a second database query statement can also be generated through a large language model based on database performance data of the target database and the first database query statement.

[0094] In one embodiment of the present application, in order to efficiently return the corresponding query results, correspondingly, in the process of executing the first database query statement in the target database, the first database query statement can also be processed in combination with the database caching mechanism and index optimization technology to improve the efficiency of obtaining the second query result.

[0095] In this embodiment, the user does not need to master complex database query languages, and can efficiently obtain query results in the target database through natural language input, which greatly reduces the complexity of user operations and allows users to efficiently interact with the target database and quickly obtain the required data. In addition, the present application, by combining the generative large model and retrieval enhancement generation RAG technology, can understand the user's query intention and generate database query statements that conform to the database structure, which is particularly suitable for processing complex query tasks, such as multi-table connections and data screening. In addition, the present application can evaluate the execution efficiency of the query while generating it, and provide optimization suggestions to ensure that the query is efficiently executed in the database, thereby improving the efficiency of obtaining query results.

[0096] Corresponding to the natural language-based database query methods provided in the above-mentioned embodiments, an embodiment of the present application also provides a natural language-based database query device. Since the natural language-based database query device provided in the embodiment of the present application corresponds to the natural language-based database query methods provided in the above-mentioned embodiments, the implementation method of the natural language-based database query method is also applicable to the natural language-based database query device provided in this embodiment, and will not be described in detail in this embodiment.

[0097] Figure 4 It is a structural diagram of a natural language-based database query device according to an embodiment of the present application.

[0098] like Figure 4 As shown, the natural language-based database query device 400 includes: a first acquisition module 401, a second acquisition module 402, a generation module 403, an optimization processing module 404 and a query module 405, wherein:

[0099] The first acquisition module 401 is used to acquire a natural language query text for a target database.

[0100] The second acquisition module 402 is used to acquire the target database semantics matching the natural language query text from the database semantic library, wherein the database semantic library is pre-constructed according to the target database.

[0101] The generation module 403 is used to generate a first database query statement adapted to the target database based on the natural language query text and the target database semantics by using a large language model.

[0102] The optimization processing module 404 is used to optimize the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database.

[0103] The query module 405 is used to execute the second database query statement in the target database to obtain the query result corresponding to the natural language query text.

[0104] In one embodiment of the present application, the first acquisition module 401 is specifically used to: determine the semantic representation of the natural language query text; determine the degree of match between the semantic representation of the natural language query text and each database semantic in the database semantic library; sort each database semantic in the database semantic library according to the degree of match to obtain a sorting result; obtain the top N database semantics from the sorting result as the target database semantics, where N is an integer greater than or equal to 1.

[0105] In one embodiment of the present application, the optimization processing module 404 is specifically used to: use a large language model to generate a second database query statement according to database performance data of the target database and the first database query statement.

[0106] In one embodiment of the present application, the optimization processing module 404 is specifically used to: output a first optimization suggestion when it is determined that the first database query statement does not limit the data filtering range, wherein the first optimization suggestion is used to indicate that the data filtering range is limited for the first database query statement; determine the target data filtering range limited for the first database query statement based on the response result returned for the first optimization suggestion; process the first database query statement based on the target data filtering range to obtain a second database query statement, wherein the data filtering range in the second database query statement is the target data filtering range.

[0107] In one embodiment of the present application, the optimization processing module 404 is specifically used to: when it is determined that the connection order of multiple database tables in the target database in the first database query statement is the first connection order, output a second optimization suggestion, wherein the second optimization suggestion is used to indicate whether to adjust the connection order of the multiple database tables to the second connection order, wherein the efficiency of executing the first database query statement in the second connection order is higher than that in the second connection order; when it is determined that the connection order of the multiple database tables is adjusted to the second connection order according to the response result of the second optimization suggestion, the first database query statement is processed according to the second connection order to obtain a second database query statement, wherein the connection order of the multiple database tables in the second database query statement is the second connection order.

[0108] In one embodiment of the present application, the optimization processing module 404 is specifically used to: when it is determined that the first database query statement includes a sorting operation, output a third optimization suggestion, wherein the third optimization suggestion is used to indicate setting a restriction condition for the result returned by the sorting operation; based on the response result returned for the third optimization suggestion, determine the target restriction condition set for the sorting operation; based on the target restriction condition, process the first database query statement to obtain a second database query statement, wherein the restriction condition set for the result returned by the sorting operation in the second database query statement is the target restriction condition.

[0109] In one embodiment of the present application, the method of pre-constructing a database semantic library based on a target database is as follows: obtaining a knowledge graph pre-constructed for the target database, wherein each node in the knowledge graph is a database element in the target database, and the associated edges between each node are the association relationships between the corresponding database elements; determining the semantic representation of the nodes and associated edges in the knowledge graph through a graph neural network; and constructing a database semantic library based on the semantic representation of the nodes and associated edges.

[0110] The database query device based on natural language in the embodiment of the present application, after obtaining the natural language query text for the target database, obtains the target database semantics that matches the natural language query text from the database semantic library constructed based on the target database, and inputs the target database semantics and the natural language query text into the large language model, so as to generate a first database query statement adapted to the target database through the large language model, and optimizes the first database query statement to obtain a second database query statement with higher execution efficiency than the first database query statement, and queries the second database query statement in the target database to obtain the query result corresponding to the natural language query text. As a result, the user does not need to master the complex database query language, and can obtain the corresponding query result in the database by inputting the natural language, which facilitates the user to query information in the database while improving the efficiency of obtaining the query result.

[0111] According to an embodiment of the present application, the present application also provides an electronic device and a readable storage medium.

[0112] Figure 5 It is a block diagram of an electronic device used to implement the natural language-based database query method of an embodiment of the present application.

[0113] like Figure 5 As shown, the electronic device comprises:

[0114] Memory 1001 , processor 1002 , and computer instructions stored in the memory 1001 and executable on the processor 1002 .

[0115] When the processor 1002 executes the instructions, the natural language-based database query method provided in the above embodiment is implemented.

[0116] Furthermore, the electronic device further comprises:

[0117] The communication interface 1003 is used for communication between the memory 1001 and the processor 1002 .

[0118] The memory 1001 is used to store computer instructions that can be executed on the processor 1002 .

[0119] The memory 1001 may include a high-speed RAM memory, and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory.

[0120] The processor 1002 is configured to implement the natural language-based database query method of the above embodiment when executing a program.

[0121] If the memory 1001, the processor 1002 and the communication interface 1003 are implemented independently, the communication interface 1003, the memory 1001 and the processor 1002 can be connected to each other through a bus and communicate with each other. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0122] Optionally, in a specific implementation, if the memory 1001, the processor 1002 and the communication interface 1003 are integrated on a chip, the memory 1001, the processor 1002 and the communication interface 1003 can communicate with each other through an internal interface.

[0123] The processor 1002 may be a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present application.

[0124] The electronic device of the embodiment of the present application, after obtaining the natural language query text for the target database, obtains the target database semantics that matches the natural language query text from the database semantic library constructed based on the target database, and inputs the target database semantics and the natural language query text into the large language model, so as to generate a first database query statement adapted to the target database through the large language model, and optimizes the first database query statement to obtain a second database query statement with higher execution efficiency than the first database query statement, and queries the second database query statement in the target database to obtain the query result corresponding to the natural language query text. As a result, the user does not need to master the complex database query language, and can obtain the corresponding query result in the database by inputting the natural language, which facilitates the user to query information in the database while improving the efficiency of obtaining the query result.

[0125] This embodiment may also provide a computer program product, including a computer program, which implements the above-mentioned natural language-based database query method disclosed in this embodiment when executed by a processor.

[0126] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.

[0127] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of this application, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.

[0128] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, fragment or portion of code comprising one or more executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.

[0129] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute the instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), a fiber optic device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or otherwise processing in a suitable manner if necessary, and then stored in a computer memory.

[0130] It should be understood that the various parts of the present application can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. If implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0131] A person skilled in the art may understand that all or part of the steps in the above-mentioned embodiment method may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.

[0132] In addition, each functional unit in each embodiment of the present application may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.

[0133] The storage medium mentioned above may be a read-only memory, a magnetic disk or an optical disk, etc. Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limiting the present application. A person of ordinary skill in the art may change, modify, replace and modify the above embodiments within the scope of the present application.

Claims

1. A database query method based on natural language, characterized in that: The method comprises: Obtaining natural language query text for a target database; Acquire target database semantics matching the natural language query text from a database semantic library, wherein the database semantic library is pre-constructed according to the target database; Using a large language model, based on the natural language query text and the semantics of the target database, a first database query statement adapted to the target database is generated; Optimizing the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database; A second database query statement is executed in the target database to obtain a query result corresponding to the natural language query text.

2. The method according to claim 1, characterized in that The step of acquiring the target database semantics matching the natural language query text from the database semantic library includes: Determining a semantic representation of the natural language query text; Determining the matching degree between the semantic representation of the natural language query text and the semantics of each database in the database semantic library; According to the matching degree, sorting the database semantics in the database semantic library to obtain a sorting result; The top N database semantics are obtained from the sorting result as the target database semantics, where N is an integer greater than or equal to 1.

3. The method according to claim 1, characterized in that The step of optimizing the first database query statement to obtain a second database query statement includes: The large language model is used to generate the second database query statement according to the database performance data of the target database and the first database query statement.

4. The method according to claim 1, characterized in that The step of optimizing the first database query statement to obtain a second database query statement includes: When it is determined that the first database query statement does not limit the data screening range, output a first optimization suggestion, wherein the first optimization suggestion is used to indicate that the data screening range is limited for the first database query statement; Determining a target data screening range defined by the first database query statement according to a response result returned in response to the first optimization suggestion; The first database query statement is processed according to the target data screening range to obtain the second database query statement, wherein the data screening range in the second database query statement is the target data screening range.

5. The method according to claim 1, characterized in that The step of optimizing the first database query statement to obtain a second database query statement includes: When it is determined that the connection order of the multiple database tables in the target database in the first database query statement is the first connection order, outputting a second optimization suggestion, wherein the second optimization suggestion is used to indicate whether to adjust the connection order of the multiple database tables to a second connection order, wherein the efficiency of executing the first database query statement in the second connection order is higher than that in the second connection order; When it is determined, based on a response result of the second optimization suggestion, that the connection order of the multiple database tables is adjusted to the second connection order, the first database query statement is processed according to the second connection order to obtain the second database query statement, wherein the connection order of the multiple database tables in the second database query statement is the second connection order.

6. The method according to claim 1, characterized in that The step of optimizing the first database query statement to obtain a second database query statement includes: In the case where it is determined that the first database query statement includes a sorting operation, outputting a third optimization suggestion, wherein the third optimization suggestion is used to indicate setting a limiting condition for a result returned by the sorting operation; Determining a target limiting condition set for the sorting operation according to a response result returned for the third optimization suggestion; The first database query statement is processed according to the target limiting condition to obtain the second database query statement, wherein the limiting condition set for the result returned by the sorting operation in the second database query statement is the target limiting condition.

7. The method according to any one of claims 1 to 6, characterized in that The method of pre-building the database semantic library according to the target database is: Obtain a knowledge graph pre-built for the target database, wherein each node in the knowledge graph is a database element in the target database, and the associated edges between the nodes are the association relationships between the corresponding database elements; Determine the semantic representation of the nodes and the associated edges in the knowledge graph through a graph neural network; The database semantic library is constructed according to the semantic representations of the nodes and the associated edges.

8. A database query device based on natural language, characterized in that: The device comprises: A first acquisition module is used to acquire a natural language query text for a target database; A second acquisition module is used to acquire the target database semantics matching the natural language query text from a database semantic library, wherein the database semantic library is pre-constructed according to the target database; A generation module, configured to generate a first database query statement adapted to the target database based on the natural language query text and the target database semantics by using a large language model; an optimization processing module, configured to optimize the first database query statement to obtain a second database query statement, wherein the execution efficiency of the second database query statement in the target database is higher than the execution efficiency of the first database query statement in the target database; The query module is used to run a second database query statement in the target database to obtain a query result corresponding to the natural language query text.

9. An electronic device, comprising: Memory, processor; The memory stores computer instructions, and when the computer instructions are executed by the processor, the method according to any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 7 when executed by a processor.