Processing method and device based on database query, equipment and medium
By filtering database schema, extracting key information and generating query statements, the problem of inaccurate generation of database query statements is solved, and efficient and accurate database query is achieved.
Patent Information
- Application Number
- CN202411835078.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-29
AI Technical Summary
The inaccurate generation of database query statements affects the accuracy and completeness of query results, and reduces the efficiency of the database system and user experience.
By obtaining user query problems, filtering candidate database schemas, extracting key content information, converting them into content identifiers that can be recognized by the database, determining the query statement skeleton, and populating the identifier and pattern into the skeleton, generating database query statements and performing query operations.
It realizes the automatic generation of accurate database query statements based on user query problems, lowers the threshold for users to query databases, and significantly improves query efficiency and accuracy.
Smart Images

Figure CN119903071A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a processing method, device, equipment and medium based on database query. Background Art
[0002] As a core tool for data storage and management, databases play a vital role in all walks of life. However, with the continuous expansion of database scale and the increasing complexity of data, the problem of inaccurate database query statement generation has gradually become prominent, especially in databases involving a large number of field information and complex relationships between fields.
[0003] In the prior art, the problem of inaccurate database query statement generation not only affects the accuracy and completeness of the query results, but also reduces the efficiency of the database system and the user experience. On the one hand, inaccurate query statements may cause users to be unable to obtain the required data information, and thus unable to make correct decisions and judgments; on the other hand, inaccurate query statements may also cause risks such as data errors and data loss, causing unnecessary losses to the business operations of the enterprise. Summary of the invention
[0004] In view of the above problems, a processing method, device, equipment and medium based on database query is proposed to overcome the above problems or at least partially solve the above problems, including:
[0005] A processing method based on database query, the method comprising:
[0006] Get user query questions;
[0007] According to the user query question, a candidate database mode is screened out from a plurality of database modes in a preset database, and key content information is extracted from the user query question;
[0008] Converting the key content information into a content identifier recognizable by the database, and selecting a target database schema from the candidate database schemas;
[0009] Determine the function structure information of the user query question, and determine the query statement skeleton of the user query question according to the function structure information;
[0010] The content identifier and the target database model are filled into the query statement skeleton to obtain a database query statement, and a query operation is performed on the database through the database query statement.
[0011] Optionally, the selecting a candidate database mode from a plurality of database modes in a preset database according to the user query question includes:
[0012] Determining a question vector of the user query question, and determining a database pattern vector of a plurality of database patterns in a preset database;
[0013] The similarity between the question vector and the database pattern vector is calculated, and according to the similarity, a candidate database pattern is screened out from the multiple database patterns.
[0014] Optionally, the step of selecting a target database schema from the candidate database schemas includes:
[0015] Performing text matching on the key content information and the candidate database patterns, and selecting a target database pattern from the candidate database patterns according to the result of the text matching;
[0016] And / or, semantic matching is performed on the key content information and the candidate database schemas, and a target database schema is screened out from the candidate database schemas according to a result of the semantic matching.
[0017] Optionally, before executing the query operation in the database using the database query statement, the method further includes:
[0018] Executing the database query statement in the database and obtaining the execution result;
[0019] When the execution result is an error message, the database query statement is adjusted until the execution result of the adjusted database query statement is a non-error message.
[0020] Optionally, the method is applied to a large model, and the method further comprises:
[0021] Determining target sample data matching the user query question from a preset sample data set;
[0022] The target sample data is used as a prompt word of the large model; wherein each sample data in the sample data set is a tuple of a sample query question and a sample query statement.
[0023] Optionally, determining target sample data matching the user query question from the preset sample data sets includes:
[0024] Determine a function set, table and field set for each sample data in a preset sample data set;
[0025] According to the function set, table and field set, target sample data matching the user query question is determined from the sample data set.
[0026] Optionally, the user query question is a question in natural language, and the database query statement is a statement that can be recognized by the database.
[0027] A processing device based on database query, the device comprising:
[0028] User query question acquisition module, used to obtain user query questions;
[0029] A question deconstruction module, used to screen candidate database patterns from a plurality of database patterns in a preset database according to the user query question, and extract key content information from the user query question;
[0030] A schema linking module, configured to convert the key content information into a content identifier recognizable by the database, and to select a target database schema from the candidate database schemas;
[0031] A skeleton determination module, used to determine the function structure information of the user query question, and determine the query sentence skeleton of the user query question according to the function structure information;
[0032] The database query statement generating module is used to fill the content identifier and the target database model into the query statement skeleton to obtain the database query statement, and perform a query operation in the database through the database query statement.
[0033] An electronic device comprises a processor, a memory and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the method described above when executed by the processor.
[0034] A computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described above is implemented.
[0035] The embodiments of the present invention have the following advantages:
[0036] In an embodiment of the present invention, a user query question is obtained; according to the user query question, a candidate database model is screened out from a plurality of database models in a preset database, and key content information is extracted from the user query question; the key content information is converted into a content identifier recognizable by the database, and a target database model is screened out from the candidate database models; the function structure information of the user query question is determined, and according to the function structure information, a query statement skeleton of the user query question is determined; the content identifier and the target database model are filled into the query statement skeleton to obtain a database query statement, and a query operation is executed in the database through the database query statement, thereby realizing automatic generation of a database query statement according to the user query question and executing a query operation, and while accurately generating a database query statement, lowering the threshold for users to query the database, and significantly improving query efficiency and accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solution of the present invention, the accompanying drawings required for use in the description of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.
[0038] Figure 1 It is a flowchart of a database query optimization method based on the combination of content and structure knowledge provided by some embodiments of the present invention;
[0039] Figure 2 is a flowchart of a processing method based on database query provided by some embodiments of the present invention;
[0040] Figure 3 is a schematic diagram of an implementation process of a problem deconstruction module provided by some embodiments of the present invention;
[0041] Figure 4 is a schematic diagram of an implementation process of a bidirectional mode link module provided by some embodiments of the present invention;
[0042] Figure 5 is a schematic diagram of an implementation process of an SQL generation module provided by some embodiments of the present invention;
[0043] Figure 6 It is a structural block diagram of a processing device based on database query provided by some embodiments of the present invention. DETAILED DESCRIPTION
[0044] In order to make the above-mentioned purposes, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0045] Among related technologies, the generative large language model is a powerful and popular technical means in the field of natural language processing today. Based on massive data, a deep attention network based on Transformer (a sequence model based on an attention mechanism), and reinforcement learning based on human feedback, the generative large language model can learn rich latent semantics, thereby understanding natural language and generating corresponding answers.
[0046] Among them, Natural Language Processing (NLP) is a subfield of the field of Artificial Intelligence (AI), dedicated to enabling computers to understand, process and generate human language. Large Language Model (LLM), referred to as large model or large language model, is an artificial intelligence model designed to understand and generate human language. It can perform a wide range of tasks, including text summarization, translation, sentiment analysis, etc., by training on a large amount of text data, and contains billions of parameters. Pipeline is a technical method for organizing multiple tasks in a certain order to form a processing flow. Each task receives input data in turn, generates output results, and then passes the output to the next task as its input.
[0047] Generative large language models mainly adopt a two-stage training strategy: first, pre-training on large-scale unsupervised data; then prompt fine-tuning and reinforcement learning on labeled data for specific tasks. This strategy allows the model to perform well with a small amount of labeled data and generate answers that meet human needs as much as possible.
[0048] In practical applications, big models (generative big language models) have been widely used in the field of converting natural language text into database queries. For example, the schema information in the database (including table creation statements and primary and foreign keys, etc.) is input into the big model as context information to establish the association between the big model and database knowledge. Then, a small sample learning method is used to guide the big model to generate SQL (Structured Query Language / database query statements). However, the field information in the database is usually quite numerous, and the relationship between fields and fields, as well as between fields and values, is complex, which poses a challenge to the big model in accurately generating database query statements.
[0049] Therefore, in an embodiment of the present invention, by combining multi-channel joint technology, an innovative text-to-SQL semantic bridging enhancement framework is proposed, which redefines the paradigm of SQL generation to improve the conversion efficiency from natural language questions to SQL queries.
[0050] In the embodiment of the present invention, a Text-to-SQL-In-context learning strategy is designed based on a large model, which can map the key content and structural information in the question to the database model, and optimize the context learning process of the large model through bidirectional model linking and sophisticated example selection strategy. It is applicable to various cross-domain databases and has strong scalability and adaptability. With the help of the collaborative work of the content channel and the structure channel, it can accurately extract the necessary information from the original question and generate accurate SQL queries, effectively reducing the complexity of model processing and resource consumption.
[0051] Among them, Text-to-SQL (natural language text to database structured query) is a technology in the field of natural language processing, which is used to convert natural language questions or instructions into structured query language (SQL). The goal of this technology is to enable computers to understand database query requirements described by humans in natural language and convert them into SQL query statements that can be executed directly on the database. By combining language models, grammatical analysis, semantic parsing, and knowledge of database schemas. The application areas of Text-to-SQL technology can include automatic question-answering systems, intelligent improvements to database query interfaces, and semantic search.
[0052] In-context learning is a concept in the field of machine learning and artificial intelligence, especially in the research and application of large language models (LLMs). In contextual learning, the model does not simply rely on a pre-trained static knowledge base, but is able to dynamically adjust its behavior based on the current input context. Allowing the model to adapt to new inputs instantly without retraining or fine-tuning. New tasks can be quickly learned by observing a small number of examples.
[0053] Schema, in the database field, is a logical concept used to organize objects in the database. Objects in a schema usually include tables, indexes, data types, sequences, stored procedures, primary keys, and foreign keys. Schema linking refers to the process of linking the unstructured expressions in user questions with the tables and fields in the database schema when interacting with the database. The association between user questions and fields can be effectively modeled through the schema linking process.
[0054] like Figure 1 , which is a flowchart of a database query optimization method based on the combination of content and structural knowledge provided in an embodiment of the present invention, including a question structure module, a bidirectional mode connection module, an SQL generation module, and constructs the collaborative work of content channels and structure channels.
[0055] Among them, the question deconstruction module is mainly used to preliminarily screen the database model and extract entity mentions in user questions. The entity mentions are usually some entity phrases or degree adverbs, such as names of people, places, and names of organizations. They are closely related to the content and structural level of the final SQL generated.
[0056] Bidirectional pattern linking module: This module is a transition module from user query questions to SQL generation. It is a bridge between the question structure stage and the SQL generation stage. In this stage, content information is combined with the database model to obtain candidate identifiers in the content pipeline, and further derive SQL functions from the structural information. Among them, all processes in this stage are implemented using the large model In-context learning.
[0057] SQL generation module: This module is the key step in converting natural language questions into executable SQL queries. It ensures the accuracy and effectiveness of queries through the designed query skeleton and detailed information filling. First, the SQL skeleton is generated based on the SQL function and the original question, and then the SQL skeleton is filled with candidate fields and tables to complete the final SQL query.
[0058] The embodiments of the present invention can solve the following problems existing in the related art:
[0059] 1. Information redundancy and low context learning efficiency: In practical applications, information redundancy often occurs, which causes the model to be unable to effectively focus on key content. In the embodiments of the present invention, irrelevant information can be filtered out through bidirectional mode linking and optimizing input context, significantly improving context learning efficiency.
[0060] 2. Difficulty in pattern matching: Existing models often perform poorly in matching natural language questions with database patterns. In the embodiments of the present invention, structural information is accurately extracted through the structural channel to ensure effective matching with the database pattern, thereby improving the accuracy of SQL generation.
[0061] 3. Poor cross-domain adaptability: Many models perform well in specific fields, but are not effective when applied across fields. In the embodiments of the present invention, it is possible to adapt to database query requirements in different fields and improve the applicability of the model.
[0062] The inventive point of the embodiment of the present invention is:
[0063] 1. Problem understanding solution based on joint learning of content and structure.
[0064] In a database query system, there is a complex semantic mapping relationship between the textual expression of a question and the database schema. In an embodiment of the present invention, by constructing a content channel and a structure channel, key content information and structure information are extracted from the question respectively, and these information are mapped to the database schema. Through this dual-channel mechanism, not only the key words in the question can be understood, but also the semantic structure of the question statement can be captured, thereby providing a basis for generating accurate SQL queries.
[0065] 2. Context optimization strategy based on sample selection.
[0066] Based on the similarity evaluation between questions and historical data, a two-stage screening strategy is designed from the perspectives of structure and content to select small sample data as context, thereby improving the learning ability of the large model and its adaptability to the SQL field.
[0067] 3. Pluggable SQL generation framework based on content and structure.
[0068] SQL query generation is a multi-step process involving multiple links such as problem understanding, pattern matching, query construction, etc. In the embodiment of the present invention, the designed framework is based on the general text-to-SQL process, and is flexibly adjusted according to different application scenarios and database requirements, and it is selected whether additional content and structure analysis modules need to be added, which has high scalability and adaptability.
[0069] In practical applications, the embodiments of the present invention have the following advantages:
[0070] 1. Efficient content and structural information extraction. Through innovative content channels and structural channels, key content and structural information can be accurately extracted from user questions. This efficient information extraction method can effectively identify the fields and table contents related to user questions, thereby improving the accuracy of SQL generation.
[0071] 2. Make full use of the generation capability of large models to reduce labor costs. By utilizing the generation capability of large language models, direct conversion from natural text to query statements is achieved. This reduces reliance on manually defined rules and reduces labor costs.
[0072] 3. Flexible adaptation to different database modes. Through bidirectional mode linking and sophisticated example selection strategies, the context learning process of large language models is optimized. This not only improves the conversion efficiency from natural language to SQL, but also enhances the model's adaptability to databases in different fields and different database modes.
[0073] The present invention will be further described below in conjunction with the accompanying drawings:
[0074] Reference Figure 2 , shows a flowchart of a processing method based on database query provided by some embodiments of the present invention, which may specifically include the following steps:
[0075] Step 201, obtaining a user query question.
[0076] In some embodiments of the present invention, the user query question is a question in natural language form.
[0077] Among them, the user query question is a question in the form of natural language, which means that the user inputs a question expressed in daily language (such as Chinese, English, etc.), rather than in the form of programming code, mathematical formula or other non-natural language. For example, the user query question is "What will the weather be like in Beijing tomorrow?", which is a question expressed in the form of natural language.
[0078] In practical applications, user query questions can be obtained and processed to generate database query statements.
[0079] Step 202 , based on the user query question, select candidate database schemas from a plurality of database schemas in a preset database, and extract key content information from the user query question.
[0080] The preset database refers to one or more database systems that have been established and store a large amount of data, and these databases may contain different types of data.
[0081] In a database system, a schema refers to the structure or organization of data. Different database schemas can represent different data views or access methods. Multiple database schemas refer to the existence of multiple different data structures or access paths in a pre-set database. Based on the content of the user's query, the schemas that contain the information the user needs can be selected from multiple database schemas as candidate database schemas.
[0082] After determining the candidate database schema, the key words or phrases that are crucial to the query results can be extracted from the user query as key content information. These key content information can be used as query conditions or filter conditions to retrieve the specific information required by the user in the database.
[0083] As some examples, the key content information may be data in the form of text, numbers, dates, etc.
[0084] In some embodiments of the present invention, the method of screening out candidate database patterns from a plurality of database patterns in a preset database based on the user query question includes: determining a question vector of the user query question and determining database pattern vectors of a plurality of database patterns in a preset database; calculating the similarity between the question vector and the database pattern vector, and screening out candidate database patterns from the plurality of database patterns based on the similarity.
[0085] The question vector of a user query question refers to the process of converting the user query question (natural language question) into a mathematical representation. The database schema vector refers to the process of converting the database schema information into a mathematical vector representation.
[0086] After the question vector and the database pattern vector are determined, the similarity between the two can be calculated to determine the similarity result, and based on the similarity calculation result, a candidate database pattern matching the user query question can be selected from multiple database patterns.
[0087] As some examples, the question structure module can be used to preliminarily screen the database schema and extract entity mentions in user questions. The entity mentions can be some entity phrases or degree adverbs, which are closely related to the content and structure level of the final SQL generated.
[0088] In some examples, a database filter may also be designed in the question structure module to filter out database patterns that are closely related to the question from the user query question.
[0089] For example, 20,000 SQL and question pairs can be collected from historical user interaction data.<question,SQL> , and then use the rule function to extract the tables and fields in SQL to form<question,table.column> The bigrams of are used as training data. The database filter selects the pre-trained language model (BERT) as the underlying semantic encoder to represent question (field) and table.column (field). Then the post-interaction method based on maximum similarity (MaxSim) is used to calculate the similarity between quesion and table.column, and the degree of association between the table and the node is determined by the similarity score. The calculation formula is as follows:
[0090] O q =Normalize(CNN(BERT Q (q))
[0091] O c =Normalize(CNN(BERT C (c))
[0092]
[0093] Among them, O q and O c It is a vector representation encoded by the twin-tower model; i and j represent the dimension values of a vector; score represents the similarity between the user query question and the current table.column (database model). By setting the similarity threshold, a preliminary screening of the database model can be performed.
[0094] In some examples, after preliminary screening of the database schema, key content and structural information in the user query questions can be extracted, and key information fragments such as nouns, entity words, degree adverbs, etc. can be extracted from the original user query questions.
[0095] like Figure 3The following is a schematic diagram of the implementation process of the question deconstruction module. The predefined database schema is the data tables and fields that have been initially screened and filtered by the retrieval module from the database model; the foreign key information defines which fields are used to associate different data tables; and the knowledge clues are used to describe the association between the user question and the schema. The above information can be used as context and together with the user query question to form the prompt words of the question deconstruction module as the output result (key content information) of the question deconstruction module. For example, the prompt words include the content information extracted from the user query question: gender, client, and average salary; and the structural information: the youngest, the lowest, and average.
[0096] Step 203: convert the key content information into a content identifier recognizable by the database, and select a target database schema from the candidate database schemas.
[0097] As some examples, converting key content information into a content identifier recognizable by a database refers to converting the key content information into a format that the database can understand and store. For example, converting the key content information into a specific data type (such as an integer, a floating point number, a string, a date, etc.); converting the key content information into or data encoding (such as converting text into UTF-8 encoding); or assigning a content identifier recognizable to the database to the key content information according to a specific identifier system used by the database (such as a primary key, a foreign key, etc.).
[0098] In some embodiments of the present invention, the step of filtering out the target database pattern from the candidate database patterns includes: performing text matching on the key content information and the candidate database patterns, and filtering out the target database pattern from the candidate database patterns based on the result of the text matching; and / or performing semantic matching on the key content information and the candidate database patterns, and filtering out the target database pattern from the candidate database patterns based on the result of the semantic matching.
[0099] Since the database schema defines the structure of the data in the database, including tables, columns, data types, relations, etc., the key content information and the candidate database schema can be matched (text matching and / or semantic matching), and the target database schema can be screened out from the candidate database schema based on the matching results.
[0100] Among them, text matching refers to the comparison of key content information and each candidate database pattern at the text level. For example, string or word matching, that is, checking whether the words or phrases in the key content information appear directly in the description of the candidate database pattern. Based on the results of text matching, candidate database patterns that highly match the key content information at the text level or contain key information will be screened out. The screened patterns are considered to be closer to the target requirements and are used as target database patterns.
[0101] Semantic matching refers to understanding the meaning of key content information and candidate database patterns. By using natural language processing (NLP) technologies such as semantic analysis and entity recognition, potential connections or similarities between key content information and candidate database patterns are identified, even if they use different vocabulary or expressions. Through semantic matching, candidate database patterns that are highly relevant or consistent with key content information in meaning, although they have different text expressions, are screened out and used as target database patterns.
[0102] As some examples, the key content information can be converted into a content identifier recognizable by the database through a bidirectional pattern linking module, and the target database schema can be screened out from the candidate database schema. The bidirectional pattern linking module is a transition module from user query questions to SQL generation, and is a bridge between the question structure stage and the SQL generation stage. In this stage, the content information is combined with the database schema (the data obtained by preprocessing and extracting key information above) to obtain candidate identifiers in the content pipeline, and further derive SQL functions from the structure information through the structure channel.
[0103] In some examples, the content channel is mainly used for information conversion and enhancement, combining keywords extracted from user query questions with database patterns, and using external evidence clues (such as domain knowledge and knowledge base) to convert natural language expressions into identifiers that can be recognized by the database. Then the foreign key relationship of the database (some tables have other related information, which is recorded through foreign key relationships) is used as contextual knowledge to guide the large model to expand the candidate set.
[0104] Since multiple preliminary screened database schemas (candidate database schemas) have been obtained in the question deconstruction module, the candidate database schemas can be screened again through bidirectional schema links. For example, using literal matching, the set of candidate word fragments extracted by the content channel is matched with the table names and column names in the database schema to select the most relevant table fields. Then, for table fields that are not directly mentioned in the question, potential matching items are screened out by identifying the semantic similarity between the field description information or the field value examples and the question content. Then, based on the results of the two matches and dependencies such as knowledge clues, foreign keys, and primary keys, database schemas with low task relevance in the candidate database schemas are eliminated to generate a target database schema based on the user query question with less redundant information.
[0105] The structural pipeline is used to extract the function structure information in the question. The user's language expression usually corresponds to standard function operations, such as ORDER BY (sorting) and AVG (average). The big model is used to determine whether there are some implicit function operations in the user's query question, and then the commonly used function names and definitions of the database are provided as context to the big model. The big model needs to combine the above information to map the user's question fragment to the specific function structure in SQL and determine the function's restrictions, such as ascending and descending order.
[0106] like Figure 4 The following is a schematic diagram of the implementation process of the bidirectional pattern linking module. In the content channel, first, the word average salary extracted by the problem structure module is directly replaced with the field [A11] in the database through the reference description in the knowledge clue. Through the foreign key knowledge prompt, the fields connecting the two related tables are [district] and [district_id], so this field is added to the candidate set. Combined with the knowledge in the schema, the initially extracted later birthdate is replaced with the corresponding field [birth_data] in the database. Finally, the extracted candidate tables and fields are bidirectionally linked to the predefined database schema again to obtain the database schema after bidirectional filtering. In the structure channel, the word fragments output in the previous stage are matched with the functions in the database, and the candidate functions [ORDER BY], [ASC], [DESC] and [AVG] are screened out. The output results of the model in this stage are: candidate tables / fields, candidate functions and database schema after bidirectional filtering.
[0107] Step 204: determine the function structure information of the user query question, and determine the query statement skeleton of the user query question based on the function structure information.
[0108] Among them, the function structure information refers to the structured representation of the various components of the query and their relationships after parsing the user query. The query statement skeleton refers to the basic query statement framework for executing the user query problem constructed based on the function structure information. For example, the framework can be a template of a structured query language (such as SQL), which contains the basic elements required to execute the query.
[0109] As some examples, user query questions can be parsed to identify the various components in the query and their relationships to form a structured representation, which is used as function structure information; after parsing the function structure information, a basic query statement framework required to execute the query (i.e., the query statement skeleton of the user query question) can be constructed based on this information.
[0110] Step 205: Fill the content identifier and the target database model into the query statement skeleton to obtain a database query statement, and perform a query operation in the database through the database query statement.
[0111] After determining the query statement skeleton of the user's query question, the query statement skeleton can be filled with information about the content identifier (such as specific field names, table names) and the target database schema (such as the actual table structure and field names in the database). The filled query statement skeleton can become a complete and executable database query statement.
[0112] As some examples, after obtaining a database query statement, the obtained database query statement can be sent to a database management system (DBMS), and the DBMS parses and executes the query statement. During the execution process, the DBMS calculates the query result based on the content in the query statement and the actual data in the database, and returns it to the initiator of the query.
[0113] In some embodiments of the present invention, the database query statement is a statement that can be recognized by the database.
[0114] In some embodiments of the present invention, before executing the query operation in the database through the database query statement, it also includes: executing the database query statement in the database and obtaining an execution result; when the execution result is an error content, adjusting the database query statement until the execution result of the adjusted database query statement is non-error content.
[0115] As some examples, after executing a database query statement, the DBMS will calculate the query result based on the content of the database query statement and the actual data in the database, and return this result to the initiator of the query. This result can be a data record set, a single data value, the number of rows affected (such as insert, update, delete operations), or an error message (if there is an error in the query statement).
[0116] If the execution result returned by the DBMS is an error message, it means that the database query statement encountered a problem during execution. These problems may be caused by syntax errors, logical errors, insufficient permissions, data non-existence, etc. When the execution result is an error message, the database query statement needs to be corrected. For example, correct syntax errors, adjust logical conditions, change field names or table names, add necessary permissions, etc.
[0117] After adjusting the database query statement, you need to execute the adjusted database query statement again in the database and check the execution result. If the execution result is no longer an error message, but a normal query result (such as a data record set, a single data value, etc.), it means that the adjustment is effective and the database query statement can be executed correctly. If the execution result is still an error message, you need to continue to adjust the database query statement until you find the correct database query statement.
[0118] As some examples, the SQL generation module can convert natural language questions (user query questions) into executable SQL queries. The accuracy and effectiveness of the query are ensured through the carefully designed query skeleton and meticulous information filling. The SQL skeleton is generated based on the SQL function and the original question, and then the SQL skeleton is filled with candidate fields and tables to complete the final SQL query. Specifically, it includes:
[0119] 1. Skeleton generation: Provide the candidate functions recommended by the structure channel and the user query questions as context to the big model, so that it can build an SQL query skeleton. The skeleton defines the basic structure of the query, including how each function combines fields. The specific table name and field name will be defaulted using the [MASK] character.
[0120] 2. Skeleton filling: Based on the candidate tables and field information provided by the content channel, use the big model to complete the mapping and filling of placeholders in the skeleton. If necessary, you can add the WHERE clause in the SQL skeleton to filter records to ensure that the returned results are consistent with the user's question. Figure 1 To.
[0121] 3. Error correction module based on LLM: Design an error correction agent based on a large model. After each skeleton filling execution, it will get the SQL of the current round, and then call the SQL library function execution module to obtain the execution result of the SQL. If the execution result of the SQL is wrong, or a null value is queried, etc., it will roll back to the previous module to re-extract the structure information and skeleton information until there is no error in the SQL execution.
[0122] like Figure 5 This is a schematic diagram of the implementation process of the SQL generation module. Through the candidate functions output by the structural channel in the bidirectional mode link module, the SQL skeleton is generated using the large model. The table names and field names involved are filled with [table_name] and [column_name] respectively. The resulting SQL skeleton is as follows:
[0123] "SELECT T1.[column_name]
[0124] FROM [table_name] AS T1
[0125] INNER JOIN [table_name] AS T2
[0126] ON T1.[column_name]=T2.[column_name]
[0127] ORDER BY T2.[column_name]ASC,T1.[column_name]DESC
[0128] LIMIT 1”
[0129] The meaning of the SQL statement: Find the records matching the "Region" table from the "Customer" table, sort by the A11 field in the "Region" table in ascending order and the "Date of Birth" field in the "Customer" table in descending order, and then return the "Gender" field value of the first record in the sorted result. Corresponding user question: What is the gender of the youngest customer who opened an account in the branch in the region with the lowest average salary?
[0130] Then the candidate tables / fields and schema provided by the content channel are input into the big model as contextual knowledge together with the SQL skeleton to obtain the final SQL query:
[0131] "SELECT T1.gender (Select the "gender" field from the "customer" table).
[0132] FROM client AS T1 (the data comes from the "client" table, and the table is aliased as T1).
[0133] INNER JOIN district AS T2 (perform an inner join with the "district" table and give the "district" table an alias of T2).
[0134] ON T1.district_id=T2.district_id (the join condition is that the "district ID" field in the "customer" table is equal to the "district ID" in the "district" table).
[0135] ORDER BY T2.A11 ASC,T1.birth_date DESC (sort by the A11 field in the "Region" table in ascending order (A11 represents the average salary), and sort by the "Birth_date" field in the "Customer" table in descending order).
[0136] LIMIT 1 (limit the query result to return only one row of data)". Figure 5 process.
[0137] In some embodiments of the present invention, the method is applied to a large model, and the method also includes: determining target sample data that matches the user query question from a preset sample data set; using the target sample data as a prompt word for the large model; wherein each sample data in the sample data set is a tuple of a sample query question and a sample query statement.
[0138] The preset sample data set includes multiple sample data. These sample data are used to match the user query question, find the sample data matching the user query question as the target sample data, and use the target sample data as the prompt word of the large model.
[0139] The prompt words can instruct the large model how to generate database query statements corresponding to user query questions. Each sample data in the sample data set is a tuple consisting of a sample query question and a sample query statement. Each sample contains a question that a user may ask (sample query question) and a query statement written for the question (sample query statement). This structure enables the model to learn the mapping relationship between query questions and query statements, so that it can generate corresponding query statements based on new query questions.
[0140] As some examples, 20,000 manually verified sample data can be collected from a large amount of database interaction history (<question,SQL> Two-tuple), forming a sample data set Q. Then, each question q in Q i They are used as context input to the big model, so that it executes the question structure module and the two-way pattern link module to extract the candidate table fields and functions in the user's question. Assume that F iis the final set of functions, I i is the final set of tables and fields, for each q i Define the complexity C of a task decoding i , the calculation process is as follows:
[0141] c i =|I i |+|F i |
[0142] Based on F i and C i Determine q in the candidate set i Whether there is a similar decoding idea with the current user question q. K-shot examples with similar SQL structure and question expression are selected as the basis for small sample learning, thereby guiding the large model to learn.
[0143] The specific implementation process pseudo code is as follows:
[0144] Algorithm name: Small sample screening strategy based on content and structure similarity.
[0145] Input: example candidate set Q, target user question q, target number of examples k.
[0146] Output: The first K selected candidate sample sets. .
[0147] 1:S1<-Empty Set (construct an empty set S1 to store the small sample set for preliminary screening).
[0148] 2: for each question qiin Q do (for each user question q in the sample set Q i )
[0149] 3:ci,Fi<-InformationExtraction(qi) (Call InformationExtraction function to extract information from question q i Extract content information and structure information).
[0150] 4:if SimilarityStructure(q,ci,Fi)then(determine the similarity between the target user question q and the user question q of the current sample i structural similarity).
[0151] 5: S1<-Add qi to S1 (if similar, add problem q i Added to set S1)
[0152] 6: end if
[0153] 7: end for
[0154] 8:S<-SimilarityExpression(q,k,S1) (determine the content similarity between the target user question q and the samples in S1, and select the first k samples as the final selected sample set S).
[0155] 9:return S
[0156] In some embodiments of the present invention, determining target sample data that matches the user query question from the preset sample data set includes: determining a function set, a table, and a field set for each sample data in the preset sample data set; and determining the target sample data that matches the user query question from the sample data set based on the function set, the table, and the field set.
[0157] The sample data set consists of multiple tables, each of which contains multiple fields (i.e., columns). The table and field set refers to the set of these tables and the fields they contain. These tables and fields constitute the structural framework of the data. The function set can be a set consisting of date conversion functions, string processing functions, mathematical calculation functions, etc.
[0158] By analyzing the user's query questions, we determine the functions that need to be used (such as filtering, sorting, aggregation, etc.) and the tables and fields that need to be queried, and then extract sample data that matches the query questions from the sample data set as the target sample data.
[0159] In an embodiment of the present invention, a user query question is obtained; according to the user query question, a candidate database model is screened out from a plurality of database models in a preset database, and key content information is extracted from the user query question; the key content information is converted into a content identifier recognizable by the database, and a target database model is screened out from the candidate database models; the function structure information of the user query question is determined, and according to the function structure information, a query statement skeleton of the user query question is determined; the content identifier and the target database model are filled into the query statement skeleton to obtain a database query statement, and a query operation is executed in the database through the database query statement, thereby realizing automatic generation of a database query statement according to the user query question and executing a query operation, and while accurately generating a database query statement, lowering the threshold for users to query the database, and significantly improving query efficiency and accuracy.
[0160] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0161] Reference Figure 6 , shows a schematic diagram of the structure of a processing device based on database query provided by some embodiments of the present invention, which may specifically include the following modules:
[0162] The user query question acquisition module 601 is used to acquire the user query question.
[0163] The question decomposition module 602 is used to screen candidate database patterns from a plurality of database patterns in a preset database according to the user query question, and extract key content information from the user query question.
[0164] The schema linking module 603 is used to convert the key content information into a content identifier recognizable by the database, and select a target database schema from the candidate database schemas.
[0165] The skeleton determination module 604 is used to determine the function structure information of the user query question, and determine the query statement skeleton of the user query question according to the function structure information.
[0166] The database query statement generating module 605 is used to fill the content identifier and the target database model into the query statement skeleton to obtain a database query statement, and perform a query operation in the database through the database query statement.
[0167] In some embodiments of the present invention, the method of selecting a candidate database schema from a plurality of database schemas in a preset database according to the user query question is used to:
[0168] Determining a question vector of the user query question, and determining a database pattern vector of a plurality of database patterns in a preset database;
[0169] The similarity between the question vector and the database pattern vector is calculated, and according to the similarity, a candidate database pattern is screened out from the multiple database patterns.
[0170] In some embodiments of the present invention, the step of selecting a target database schema from the candidate database schemas is used to:
[0171] Performing text matching on the key content information and the candidate database patterns, and selecting a target database pattern from the candidate database patterns according to the result of the text matching;
[0172] And / or, semantic matching is performed on the key content information and the candidate database schemas, and a target database schema is screened out from the candidate database schemas according to a result of the semantic matching.
[0173] In some embodiments of the present invention, the device further comprises:
[0174] An execution result acquisition module, used to execute the database query statement in the database and obtain the execution result;
[0175] The adjustment module is used to adjust the database query statement when the execution result is an error content, until the execution result of the adjusted database query statement is a non-error content.
[0176] In some embodiments of the present invention, the device is applied to a large model, and the device further comprises:
[0177] The target sample data determination module is used to determine the target sample data matching the user query question from the preset sample data set; use the target sample data as the prompt word of the large model; wherein each sample data in the sample data set is a tuple of a sample query question and a sample query statement.
[0178] In some embodiments of the present invention, the target sample data determination module includes:
[0179] A set information determination submodule is used to determine the function set, table and field set of each sample data in the preset sample data set;
[0180] The sample data matching submodule is used to determine target sample data matching the user query question from the sample data set according to the function set, table and field set.
[0181] In some embodiments of the present invention, the user query question is a question in natural language form, and the database query statement is a statement that can be recognized by the database.
[0182] Some embodiments of the present invention further provide an electronic device, comprising a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the above method is implemented when the computer program is executed by the processor.
[0183] Some embodiments of the present invention further provide a computer-readable storage medium, on which a computer program is stored, and the computer program implements the above method when executed by a processor.
[0184] Some embodiments of the present invention further provide a computer program product, including a computer program, which implements the above method when executed by a processor.
[0185] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0186] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0187] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0188] Those skilled in the art will appreciate that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0189] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0190] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0191] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0192] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0193] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "including one..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the above elements.
[0194] The above is a detailed introduction to a processing method, device, equipment and medium based on database query. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A processing method based on database query, characterized in that: The method comprises: Get user query questions; According to the user query question, a candidate database mode is screened out from a plurality of database modes in a preset database, and key content information is extracted from the user query question; Converting the key content information into a content identifier recognizable by the database, and selecting a target database schema from the candidate database schemas; Determine the function structure information of the user query question, and determine the query statement skeleton of the user query question according to the function structure information; The content identifier and the target database model are filled into the query statement skeleton to obtain a database query statement, and a query operation is performed on the database through the database query statement.
2. The method according to claim 1, characterized in that The step of selecting a candidate database mode from a plurality of database modes in a preset database according to the user query question includes: Determining a question vector of the user query question, and determining a database pattern vector of a plurality of database patterns in a preset database; The similarity between the question vector and the database pattern vector is calculated, and according to the similarity, a candidate database pattern is screened out from the multiple database patterns.
3. The method according to claim 1, characterized in that: The step of selecting a target database schema from the candidate database schemas includes: Performing text matching on the key content information and the candidate database patterns, and selecting a target database pattern from the candidate database patterns according to the result of the text matching; And / or, semantic matching is performed on the key content information and the candidate database schemas, and a target database schema is screened out from the candidate database schemas according to a result of the semantic matching.
4. The method according to any one of claims 1 to 3, characterized in that: Before executing the query operation on the database through the database query statement, the method further includes: Executing the database query statement in the database and obtaining the execution result; When the execution result is an error message, the database query statement is adjusted until the execution result of the adjusted database query statement is a non-error message.
5. The method according to any one of claims 1 to 3, characterized in that: The method is applied to a large model, and the method further comprises: Determining target sample data matching the user query question from a preset sample data set; The target sample data is used as a prompt word of the large model; wherein each sample data in the sample data set is a tuple of a sample query question and a sample query statement.
6. The method according to claim 5, characterized in that Determining target sample data matching the user query question from the preset sample data sets includes: Determine a function set, table and field set for each sample data in a preset sample data set; According to the function set, table and field set, target sample data matching the user query question is determined from the sample data set.
7. The method according to claim 1, characterized in that The user query question is a question in natural language, and the database query statement is a statement that can be recognized by the database.
8. A processing device based on database query, characterized in that: The device comprises: User query question acquisition module, used to obtain user query questions; A question deconstruction module, used to screen candidate database patterns from a plurality of database patterns in a preset database according to the user query question, and extract key content information from the user query question; A schema linking module, configured to convert the key content information into a content identifier recognizable by the database, and to select a target database schema from the candidate database schemas; A skeleton determination module, used to determine the function structure information of the user query question, and determine the query sentence skeleton of the user query question according to the function structure information; The database query statement generating module is used to fill the content identifier and the target database model into the query statement skeleton to obtain the database query statement, and perform a query operation in the database through the database query statement.
9. An electronic device, characterized in that: The method comprises a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the computer program implements the method according to any one of claims 1 to 7 when executed by the processor.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Text conversion query statement generation method and system, medium and equipment
CN120371853A
View optimization query method and system oriented to multimode database
CN120723789A
A view optimization query method and system for a multi-mode database
CN120723789B
Information query method, terminal equipment and computer readable storage medium
CN121009106A
SQL statement generation method, multi-agent system and storage medium
CN121188080A