Data query method and device, storage medium and program product
Through the automatic generation and multiple inspection of pre-trained language models, the problems of complexity and high error rate of traditional database query are solved, and efficient and accurate data query is realized, which is suitable for real-time analysis in big data environments.
Patent Information
- Application Number
- CN202510515335.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-22
- Publication Date
- 2025-08-08
AI Technical Summary
The process of writing and optimization of traditional database queries is complex, time-consuming and error-prone, and it is difficult to meet the needs of efficient and accurate information extraction in the big data environment.
Use the pre-trained language model to generate query statements, and perform multiple checks and automatic adjustments through the database inspector and the pre-trained language model to ensure that the query statement meets the requirements and submits it to the database for execution.
It improves the accuracy and execution efficiency of query statements, reduces the risk of errors, and adapts to the real-time analysis needs in the big data environment.
Smart Images

Figure CN120448414A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the field of data query technology, and in particular, to a data query method, electronic device, computer-readable storage medium, and computer program product. Background Art
[0002] In the era of big data, businesses and organizations are facing an increasingly urgent need to efficiently and accurately extract valuable information from massive amounts of data. Traditional database query writing and optimization processes rely heavily on the expertise of database administrators and data analysts, requiring not only deep database knowledge but also a deep understanding of specific database structures. However, with the expansion of data volumes and the diversification of technology stacks, manually writing and debugging database queries has become increasingly complex, time-consuming, error-prone, and inefficient. Summary of the Invention
[0003] In view of this, one or more embodiments of this specification provide a data query method, an electronic device, a computer-readable storage medium, and a computer program product.
[0004] To achieve the above objectives, one or more embodiments of this specification provide the following technical solutions:
[0005] According to a first aspect of one or more embodiments of this specification, a data query method is proposed, including:
[0006] Generate a query statement based on the query requirements input by the user and the data table information related to the query requirements in the database through the pre-trained language model;
[0007] Performing a check task on the query statement and generating a check result; the check task includes: checking the query statement using a database checker integrated with preset check rules, and / or checking the query statement using the pre-trained language model;
[0008] If the preset condition is not met, the preset condition at least includes that the inspection result does not meet the inspection requirement, modifying the query statement by using the pre-trained language model with reference to the inspection result, and repeating the inspection task for the modified query statement;
[0009] If the inspection result meets the inspection requirement, the query statement is submitted to the database for execution, and the query result returned by the database is output.
[0010] According to a second aspect of the embodiments of this specification, an electronic device is provided, including:
[0011] processor;
[0012] a memory for storing processor-executable instructions;
[0013] Wherein, when the processor executes the executable instructions, it is used to implement the method described in the first aspect.
[0014] According to a third aspect of the embodiments of this specification, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the steps of the method described in the first aspect are implemented.
[0015] According to a fourth aspect of the embodiments of this specification, a computer program product is provided, comprising a computer program, which implements the steps of the method described in the first aspect when executed by a processor.
[0016] The technical solutions provided by the embodiments of this specification may have the following beneficial effects:
[0017] In the embodiments of this specification, first, based on the query requirements input by the user and the data table information related to the query requirements in the database, the pre-trained language model automatically generates a query statement that meets the query requirements. Then, the query statement is checked by at least one of the database checker and the pre-trained language model. If the inspection result of the query statement does not meet the inspection requirements, the query statement can be intelligently adjusted based on the inspection result and repeated verification can be performed. Compared with the traditional method of manually writing and adjusting query statements, this method can greatly improve the accuracy and execution efficiency of query statements, and can ensure that the query statements submitted to the database are optimized and verified, so that the query results are more reliable and the overall query execution efficiency and stability are improved.
[0018] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 This is a schematic diagram of the architecture of a data query service system provided by an exemplary embodiment.
[0020] Figure 2 This is a flowchart of a data query method provided by an exemplary embodiment.
[0021] Figure 3 This is a data query flow chart provided by an exemplary embodiment.
[0022] Figure 4 It is a schematic diagram of judging the inspection result and the number of times the inspection task is executed, provided by an exemplary embodiment.
[0023] Figure 5 It is a structural diagram of an electronic device provided by an exemplary embodiment. DETAILED DESCRIPTION
[0024] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The implementations described in the following exemplary embodiments are not intended to represent all implementations consistent with one or more embodiments of this specification. Rather, they are merely examples of apparatuses and methods consistent with certain aspects of one or more embodiments of this specification, as detailed in the appended claims.
[0025] It should be noted that in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this specification. In some other embodiments, the method may include more or fewer steps than those described in this specification. In addition, a single step described in this specification may be broken down into multiple steps for description in other embodiments, and multiple steps described in this specification may be combined into a single step for description in other embodiments.
[0026] 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 manual 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 relevant countries and regions, and corresponding operation entrances are provided for users to choose to authorize or refuse.
[0027] Here are some explanations of the terms mentioned in this manual:
[0028] 1. Pretrained language models, such as the Large Language Model (LLM) and BERT models, are AI models based on deep learning technology, particularly those trained on large corpora. They are designed to understand and generate text similar to human language and possess strong natural language understanding and generation capabilities. The goal of pretrained language models is to leverage natural language processing capabilities to enable a variety of applications, such as text generation, translation, summarization, question-answering, and dialogue systems, thereby improving the efficiency and automation of human-computer interaction.
[0029] 2. Prompts are tips, instructions, or directions. In the field of artificial intelligence (AI), prompts are used to guide and direct pre-trained language models to produce specific outputs. Prompts can be text snippets or questions that inspire the pre-trained language model to think and generate relevant content.
[0030] Based on the problems in the related art, the embodiments of this specification provide a data query method, an electronic device, a computer-readable storage medium, and a computer program product.
[0031] In one possible application scenario, based on the user's privacy protection requirements, the database can be deployed in the user's electronic device, which means that all data storage and query operations will be performed on the user's electronic device. The core purpose of this approach is to ensure that the user's data does not leave their device, thereby avoiding the risk of data leakage. Under this architecture, the data query method provided in the embodiments of this specification is designed to be executed directly on the user's local electronic device, which includes but is not limited to physical servers, server clusters, cloud servers, smart phones / mobile phones, tablet computers, personal digital assistants (PDAs), laptop computers, and desktop computers.
[0032] In another possible application scenario, such as Figure 1 As shown, Figure 1 FIG1 is a schematic diagram of an architecture of a data query service system provided by an exemplary embodiment. The system may include a server 11, a network 12, and several user terminals, such as a PC (Personal Computer) 13, a mobile phone 14, and the like.
[0033] Server 11 can be a physical server containing a standalone host, or it can be a virtual server hosted by a host cluster. During operation, server 11 can run server-side programs of a data query application to implement a corresponding data query service platform. Exemplarily, server 11 is a database server that performs operations such as data storage, data query, and data update. Server 11 assumes the function of client request processing, is responsible for receiving and processing user query requests, and returns query results to the client.
[0034] PC 13 and mobile phone 14 are only some types of user terminals that can be used by users. In fact, users can obviously also use user terminals such as the following types: tablet devices, laptops, PDAs (Personal Digital Assistants), wearable devices (such as smart glasses, smart watches, etc.), etc., and one or more embodiments of this specification do not limit this. During operation, the user terminal can run the client-side program of the data query application, which can be implemented as a client of the data query service. Among them, the client application of the above-mentioned data query service can be started and run on the user terminal. The client-side program can be a native application installed on the user terminal, or the client-side program can be a small program, a quick application or other similar forms. Of course, when using web page technologies such as HTML5 or similar, the relevant functions can be implemented through the page displayed by the browser. The browser here can be an independent browser application or a browser module embedded in certain applications.
[0035] Regarding the network 12 for interaction between user terminals such as PC 13 and mobile phone 14 and server 11, communication can be achieved using a wired or wireless network based on the communication methods supported by the corresponding user terminals, and this specification does not limit this. For example, if PC 13 supports both wired and wireless communication, then communication can be achieved using either a wired or wireless network as needed, while mobile phone 14 generally only supports wireless communication and thus can achieve communication using a wireless network.
[0036] The data query method provided in the embodiment of this specification can be executed by the server 11, or by the user terminal, or part of the method can be executed by the user terminal and the other part can be executed by the server 11. This embodiment does not impose any restrictions on this.
[0037] In some embodiments, see Figure 2 The embodiments of this specification provide a data query method, which is applied to an electronic device (such as the above-mentioned user terminal, server, or other electronic device connected to a database, etc.), and the method includes:
[0038] In S201 , a query statement is generated according to a query requirement input by a user and data table information related to the query requirement in a database through a pre-trained language model.
[0039] For example, an electronic device may receive a user-entered query request for a database, which may be expressed in natural language, such as text. For example, the query request may include, but is not limited to, filtering conditions, data integration methods, and output formats. By clearly receiving the query request, the query intent can be accurately captured, avoiding misunderstandings or duplication of work during subsequent processing, while also providing clear guidance for automated processing.
[0040] Next, the electronic device can determine the data table information related to the query request in the database based on the query request and the metadata used to describe the data organization structure of the database. The metadata used to describe the data organization structure of the database, also known as the database schema, defines the logical framework and constraints of the data and specifies how the data is stored, associated, and accessed. The metadata used to describe the data organization structure of the database includes but is not limited to:
[0041] (1) Tables, which uniquely identify a data set by the table name. Each table consists of multiple columns. The fields include the field name, data type, and various constraints, such as the primary key used to uniquely identify records, the foreign key used to link fields in other tables, the unique constraint to ensure that field values are not repeated, the not null constraint to prohibit the field from being empty, and the default value to set the default value of the field.
[0042] (2) Indexes, used to speed up queries.
[0043] (3) Views are virtual objects generated based on query results.
[0044] (4) Stored procedures and functions are predefined database operation logic.
[0045] (5) Triggers: actions performed upon specific events (such as inserts and updates), such as automatically recording modification times.
[0046] For example, the electronic device can use natural language processing technology to perform word segmentation, part-of-speech tagging and entity recognition on the query requirements input by the user, and extract keywords such as keywords, screening conditions, data integration methods and output formats. For example, for "querying customer information with sales greater than 100,000 in last year's orders", the electronic device can identify keywords such as "order", "sales greater than 100,000", and "customer information". Then, the electronic device can determine the relevant data table information from the metadata used to describe the data organization structure of the database based on the extracted keywords, such as by using any of the following methods: (1) comparing the similarity between the keywords and the table names and field names, screening out the table names or field names with similarity greater than a preset threshold, and obtaining the data table information. (2) converting the extracted keywords into a first embedding vector, and converting the relevant information of each table in the database (such as table name, field and various constraints) into a second embedding vector, and finding the most relevant table by calculating the semantic similarity between the first embedding vector and each second embedding vector. In addition, in addition to individual keyword matching, the relationship between tables can be further considered, such as judging whether multiple related tables constitute a complete query scenario based on the foreign key constraint information described in the metadata. According to the above matching and analysis process, the electronic device can obtain data table information that meets the query requirements. The data table information includes but is not limited to the table name, related fields, and association information with other tables.
[0047] In one possible implementation, after obtaining the query requirements and data table information related to the query requirements in the database, the electronic device can input the query requirements and data table information into a pre-trained language model to generate a query statement using the pre-trained language model. This step eliminates the need for database administrators or data analysts to manually write complex database query statements, reducing dependence on professional skills.
[0048] In another possible implementation, to further improve query generation accuracy, multiple generation prompt templates can be prepared in advance. These templates are designed to guide the pre-trained language model through a series of logical reasoning steps to generate query statements. Different templates correspond to different query requirements, allowing the most appropriate generation prompts to be selected based on the actual scenario, thus adapting to diverse query needs.
[0049] Exemplarily, each generation prompt template corresponds to a type of query requirement and contains multiple reasoning steps related to the query requirement, arranged in a chain-of-thought manner, to guide the pre-trained language model to deduce step by step according to the logical chain. For example, the multiple reasoning steps include but are not limited to: "Step 1: Identify the main entities in the query requirement. Step 2: Establish a correspondence between these entities and the corresponding data tables in the data table information. Step 3: Analyze the relationship between the tables. Step 4: Construct a SELECT clause based on the columns to be queried. Step 5: Construct a FROM clause based on the source table or view of the columns to be queried. Step 6: Construct a WHERE clause based on the filtering conditions. Step 7: Construct a GROUP BY clause based on the output method of the query results." This clear step-by-step prompt allows the pre-trained language model to have a clear goal at each step, rather than completing the generation of the entire query statement at one time. The multiple reasoning steps built into the template help the pre-trained language model clarify the tasks of each step, reduce misunderstandings and skipped steps, and thus generate more accurate query statements.
[0050] Exemplarily, each generation prompt template corresponds to a type of query requirement and contains query examples related to this type of query requirement. The query examples are used to describe the step-by-step conversion process of converting textual example requirements into query statements. In this way, the pre-trained language model can draw on the conversion ideas in the query examples and copy similar reasoning steps to the current query statement generation task, which can provide a clear generation direction so that the query statement finally generated can more accurately reflect the user's query requirements and database structure.
[0051] In addition to query examples, each generation prompt template may further include at least one of the following prompt information: (1) The role played by the pre-trained language model: setting a specific role so that the pre-trained language model can generate a response based on that role. (2) Background information: providing sufficient background information so that the pre-trained language model can understand the context of the question. (3) The expected goal of the task: defining key, measurable results so that the pre-trained language model knows how to measure the completion of the goal. However, this is not limited to these.
[0052] The electronic device can screen out a target generation prompt template that matches the query requirement from a plurality of preset generation prompt templates, thereby automatically screening the target generation prompt template from the plurality of target generation prompt templates, reducing manual intervention and improving response speed. For example, any of the following screening methods can be adopted: in the first screening method, based on the semantic vector matching method, the query requirement is converted into a first embedding vector, and each generation prompt template is converted into a third embedding vector respectively. By calculating the semantic similarity between the first embedding vector and each third embedding vector, the target generation prompt template with the largest semantic similarity is found. In the second screening method, one or more labels are pre-set for each generation prompt template to describe the query type to which it is applicable (such as "joint table query", "aggregate statistics", "data post-processing", etc.), and then the query requirement is matched with the labels of each generation prompt template to select the most appropriate target generation prompt template.
[0053] Next, see Figure 3 The electronic device embeds the query requirements and data table information into the target generation prompt template to generate a first prompt word, and then inputs the first prompt word into the pre-trained language model. The pre-trained language model can complete multiple reasoning steps in the first prompt word in sequence based on the query requirements and data table information, and finally generate a query statement. This chain thinking method helps to improve the query accuracy. The pre-trained language model gradually deduces according to the preset multiple reasoning steps to reduce semantic understanding deviation.
[0054] In S202, a check task is performed on the query statement to generate a check result; the check task includes: checking the query statement using a database checker integrated with preset check rules, and / or checking the query statement using a pre-trained language model.
[0055] In this step, the electronic device performs a comprehensive check on the generated query statement. The check task mainly includes at least one of the following: (1) Using a pre-built database checker with built-in preset check rules to check the query statement to ensure that the syntax, logic, and constraints meet the expectations. (2) Using a pre-trained language model to check the query statement to catch potential omissions or errors. Through automated checking, failures caused by incorrect queries are avoided and the robustness of the overall system is enhanced.
[0056] In one possible implementation, a comprehensive and efficient database search engine can be designed to ensure the accuracy and feasibility of query statements. Figure 3 , the electronic device can use a database checker integrated with preset checking rules to perform at least one of the following checks on the query statement:
[0057] In the first check (i.e., table and field existence check), metadata describing the database's data organization structure is used to generate a set of table and column names related to all tables in the database. The query statement then checks whether the table name described in the query statement exists in the table name set, and whether the column name described in the query statement exists in the column name set. This check ensures that the table and field names referenced in the query statement are consistent with the names defined in the actual database schema, effectively avoiding query errors caused by typos, misspellings, or version inconsistencies, and ensuring that the generated query statement can be correctly executed in the database.
[0058] In the second check (also known as syntax check), check whether the syntax of the query statement meets the syntax requirements of the database. For example, through the database checker, the query statement can be disassembled into various syntax units (such as keywords, identifiers, expressions, etc.), and each part can be verified to meet the syntax requirements of the database. During the check, syntax errors such as: ① missing necessary keywords (such as SELECT, FROM, etc.); ② mismatched brackets; ③ illegal characters or unsupported syntax; ④ statement structure errors; etc. can be detected. In the case of syntax errors found in the query statement, the database checker can mark the error location for subsequent correction. This check ensures that the generated query statement complies with the basic syntax rules of the database language, thereby preventing query failure or execution exceptions due to syntax errors. Through automated syntax checking, syntax errors can be captured in advance, runtime errors can be avoided, and the quality and stability of generated query statements can be improved.
[0059] In the third check (i.e., the correctness check of the association conditions), the foreign key relationship graph of the database is obtained through the metadata used to describe the data organization structure of the database. The nodes in the graph represent data tables, and the edges represent foreign key associations. The graph contains the specific fields, association types, and constraint information of the foreign keys. Next, the foreign key relationship graph is used to check whether the connection type and connection relationship described in the query statement are correct. Specifically, all JOIN operations are extracted from the query statement, and the tables, connection conditions, and JOIN types (INNER, LEFT, RIGHT, FULL, etc.) involved in the JOIN clause are analyzed. For example, for each JOIN operation: check whether the fields in the connection conditions exist in the foreign key relationship graph, and confirm whether these fields constitute a legal primary-foreign key association; verify whether the JOIN type is consistent with the foreign key relationship and the query purpose, such as whether the appropriate connection method is used when there is a one-to-many relationship; check whether necessary connection conditions are omitted, or whether the incorrect use of connection conditions leads to data duplication or omission; etc. This check verifies whether the JOIN operations involved in the query statement are based on the actual foreign key relationships in the database, ensuring that the connection conditions are reasonable and avoiding data omissions or duplications. It not only improves the accuracy of data queries, but also optimizes query performance and prevents data logic errors caused by improper associations.
[0060] In the fourth check (data column data type verification), the data types of all data columns in the database, such as integer (INT), character (VARCHAR), and date (DATE), are obtained through metadata used to describe the database's data organization structure. Based on the data types of all data columns in the database, the query statement's operational logic is checked for rationality. For example, the query statement's data types on both sides of each comparison operator (such as =, >, <, LIKE, etc.) can be checked for compatibility. For example, the comparison between a string type field and a numeric type constant should result in an error; the comparison between a date type field and a string date format should ensure the correct format; operations or comparisons between numeric fields and non-numeric types require conversion or error reporting, etc. By checking whether the operations and comparisons involved in the query statement meet the data type requirements of each field in the database, errors or unreasonable query results caused by data type mismatches can be avoided, ensuring that the generated query statement is logically correct and executed correctly during data operations and filtering.
[0061] In another possible implementation, a pre-trained language model, such as a large language pre-trained language model, can be used to check the query. Figure 3 The electronic device may input at least the query statement and the query requirement into the pre-trained language model, so that the pre-trained language model performs at least one of the following checks on the query statement:
[0062] In the first check (also known as the intent check), it is checked whether the query intent of the query statement is the same as the query intent described in the query requirement. Exemplarily, the natural language understanding capabilities of the pre-trained language model are used to parse the query statement and the query requirement separately. For example, the pre-trained language model performs semantic understanding on the query statement to determine its target entity, conditions, and output content; at the same time, the pre-trained language model parses the intent in the query requirement description, such as the time range, data range, statistical requirements, etc. of interest. Next, the pre-trained language model compares the two parts: checks whether there are deviations in the main entities or processing logic; verifies whether key points such as filtering conditions and connection methods match user needs; and finally outputs a consistency score or detailed feedback, pointing out possible mismatches (such as missing conditions, redundant logic, etc.). Through this check, it can be ensured that the generated query statement is not only grammatically correct, but also conforms to the user's true intent in actual processing logic, avoiding data query errors caused by intent deviation.
[0063] In the second check (key parameter check), the query statement is checked to see if it contains the specified parameters in the query requirements. For example, the pre-trained language model can parse the query requirements and use the pre-trained language model to extract all explicitly mentioned parameter information, including but not limited to: (1) filter conditions, such as a specific date range, numerical threshold, text matching conditions, etc. (2) output fields, the names of fields that the user requires to be included in the query results. (3) grouping or sorting parameters, such as grouping by a certain field, sorting by sales, etc. The pre-trained language model parses the query statement and extracts the WHERE conditions, SELECT fields, GROUP BY and ORDER BY clauses, etc. The parameters extracted from the query requirements are compared with the contents of the query statement item by item: confirm whether the filter conditions specified in each requirement are reflected in the WHERE clause; check whether all output fields are included in the SELECT list; verify whether they are grouped and sorted according to the specified parameters; etc. If missing or mismatched parameters are found, the pre-trained language model can output detailed prompts to indicate the specific omissions or parts that need to be adjusted. This check ensures that the query statement fully covers all parameters clearly specified in the user's needs, reduces the situation where the results are incomplete or do not meet the requirements due to missing parameters, and improves the accuracy and reliability of the query.
[0064] The third check (output format check) checks whether the output format defined in the query statement is consistent with the output format specified in the query requirement. For example, a pre-trained language model can be used to extract the user's desired output format description from the query requirement. This may include: output field names and order; data display format, such as date format, numerical precision, and unit conversion; and data aggregation format, such as multi-table join output and statistical summary results. Furthermore, the pre-trained language model parses the SELECT clause and other data processing components (such as data formatting functions and alias settings) in the query statement to determine the generated result set structure and output format. The pre-trained language model then compares the output format in the query requirement with the output result structure reflected in the query statement: checking whether the SELECT columns are consistent with the user's expectations; verifying whether the expected data formatting is performed, such as date format conversion and numerical rounding; and verifying whether the result set is grouped, sorted, or aggregated as required by the user. If a discrepancy is detected in the output format, the pre-trained language model generates a feedback report, clearly identifying the discrepancy with the user's specified requirements and suggesting adjustments, such as adding or modifying fields or adjusting formatting functions. This check ensures that the output format is consistent with user needs and can meet users' specific requirements for result presentation. This ensures that the final query results are not only correct but can also be directly used in visualization, reporting, or subsequent data processing links, thereby improving overall data processing efficiency and user experience.
[0065] In another possible implementation, to ensure that the execution of query statements does not affect the production environment, a test database can be deployed in a dedicated test environment (or sandbox environment), which is completely isolated from the production database in terms of hardware, network and access rights, thereby avoiding misoperation or resource conflicts. A test database is deployed in the test environment, and the data organization structure of the test database is the same as the data organization structure of the database targeted by the query requirements. This ensures that when query statements are executed in the test environment, the database structure is consistent with the actual application environment. In order to simulate the production situation more realistically, a portion of desensitized production data can be loaded into the test database or a representative data sample can be constructed to ensure that the query results are of reference significance.
[0066] Electronic devices can execute query statements in a pre-built test environment. Electronic devices can submit the query statements to the test database in the test environment for execution, thereby obtaining the execution information of the query statements. The execution information includes at least one of the following: (1) the execution result of the query statement, the returned result set, number of rows, field data, etc., which directly reflects whether the query logic is correct; (2) runtime status, including but not limited to: a. Runtime errors, such as division by zero, data type mismatch, resource limit exceeded and other runtime error information. b. Performance indicators, query response time, occupied resources, etc., which help evaluate the execution efficiency of query statements. c. Warnings and logs, the database execution engine may output warnings or prompts (such as empty result sets, implicit data conversion, etc.). For example, please refer to Figure 3 The electronic device can at least input the execution information of the query statement into the pre-trained language model so that the pre-trained language model can check the execution status of the query statement, such as checking whether the query statement can be executed correctly, and checking the execution performance of the query statement, etc.
[0067] When performing an inspection task for the first time, the electronic device uses a pre-trained language model to refer to the execution information of the query statement, evaluates the execution effect of the query statement, and provides execution optimization suggestions based on the execution effect of the query statement. For example, it confirms whether the query statement involves unnecessary full-table scans and recommends using indexes instead; identifies whether deprecated query modes are used and recommends querying only necessary fields; analyzes WHERE condition optimization to avoid index failure caused by complex calculations; and so on.
[0068] In the case where this is not the first time the inspection task is executed, that is, the inspection task has been executed multiple times, the execution information corresponding to the historical inspection tasks can be used for more in-depth analysis and optimization, providing more accurate execution optimization suggestions. Specifically, the electronic device can use the pre-trained language model to refer to the execution information corresponding to the historical inspection tasks and the execution information of the query statement in the current inspection task, evaluate the execution effect of the query statement, and provide execution optimization suggestions based on the execution effect of the query statement. For example, the average execution time of the query is calculated by combining the execution time in multiple execution information to evaluate whether there is a significant delay; compare the execution information corresponding to the historical inspection tasks and the execution information of the query statement in the current inspection task to check whether the index and query path have changed; detect whether the resource usage of the query increases with the growth of data; etc. Based on the above evaluation process, the pre-trained language model can provide relevant execution optimization suggestions, such as but not limited to: a. If the query execution time increases, the pre-trained language model can analyze whether the index needs to be added or adjusted; b. Or detect whether there are any unused indexes and recommend deleting unnecessary indexes to reduce database maintenance costs; c. If the query involves a large data set, it is recommended to enable parallel query; d. If the query is run frequently but the results do not change much, it is recommended to use query result cache; etc.
[0069] This solution optimizes by combining a pre-trained language model with query execution information. This not only improves query execution efficiency, but also continuously improves query performance as historical execution data accumulates.
[0070] Of course, in addition to inputting the execution information of the query statement into the pre-trained language model, the inspection results of the other two implementation methods mentioned above (the inspection results output by the database checker and the inspection results output by the pre-trained language model) can also be input into the pre-trained language model. Different inspection methods can provide information from different angles, thereby improving the rationality and credibility of the execution optimization suggestions.
[0071] In actual data query scenarios, any one of the above three implementation methods can be selected based on actual needs, or at least two can be combined to check the query statement. This embodiment does not impose any restrictions on this.
[0072] In S203, if the preset conditions are not met, the preset conditions at least include that the inspection result does not meet the inspection requirements, the query statement is modified by using the pre-trained language model with reference to the inspection result, and the inspection task is repeated for the modified query statement.
[0073] In this step, the query statement is continuously improved through automatic feedback and adjustment mechanisms, thereby improving the quality of the final query statement.
[0074] The specific content of the inspection requirements may be set according to the actual application scenario, and this embodiment does not impose any restrictions on this.
[0075] For example, to further improve query modification accuracy, multiple modification suggestion templates can be pre-prepared. These templates are designed to guide the pre-trained language model to make targeted modifications to the query. Different templates correspond to different error types, allowing the most appropriate modification suggestion to be selected based on the actual scenario, thus adapting to diverse modification needs.
[0076] Error types include, but are not limited to: (1) grammatical errors (e.g., keyword spelling, bracket matching issues); (2) logical errors (e.g., unreasonable conditions, incorrect connection relationships); (3) index optimization issues (e.g., index not used, slow query speed); (4) data type errors (e.g., integer and string mismatch); (5) query result errors (e.g., excessive data, missing data); etc. Each error type has a different modification method, for example: grammatical error → directly modify keyword spelling; index problem → recommend creating an index or rewriting the query structure; data type error → adjust the data conversion method. With modification templates for different error types, the pre-trained language model can select the most appropriate template to improve the adaptability of the modification.
[0077] Exemplarily, each modification prompt template corresponds to a type of error and includes modification examples related to the type of error, so that the pre-trained language model can learn how to modify the query statement more accurately.
[0078] The modification prompt template includes at least one of the following: a positive modification example describing the correct modification method, and a negative modification example describing the incorrect modification method. Positive modification examples provide the correct modification method, allowing the pre-trained language model to learn from excellent cases and ensure that the modified query statement is both correctly executed and complies with optimization principles. Negative modification examples demonstrate incorrect modification methods, point out common error patterns, and prevent the pre-trained language model from misusing unreasonable correction methods. Providing both positive and negative modification examples can promote comparative learning, improve the accuracy of query statement modifications, and avoid known error traps.
[0079] In addition to query examples, each modification prompt template may further include at least one of the following prompt information: (1) The role played by the pre-trained language model: setting a specific role so that the pre-trained language model can generate a response based on the role. (2) Background information: providing sufficient background information so that the pre-trained language model can understand the context of the question. (3) The expected goal of the task: defining key, measurable results so that the pre-trained language model knows how to measure the completion of the goal. However, this is not limited to these.
[0080] The electronic device can automatically select a target modification prompt template from multiple preset modification prompt templates to match the query requirements, reducing manual intervention and improving response speed. For example, if a JOIN operation error is detected, a modification prompt template that contains the correct JOIN usage and the relevant table structure description is selected.
[0081] Exemplarily, any of the following screening methods can be used: In the first screening method, based on the semantic vector matching method, the inspection result is converted into a fourth embedding vector, and each modification prompt template is converted into a fifth embedding vector. By calculating the semantic similarity between the fourth embedding vector and each fifth embedding vector, the target modification prompt template with the greatest semantic similarity is found. In the second screening method, one or more labels are pre-set for each modification prompt template to describe the error type to which it applies. The error type described in the inspection result is then matched with the label of each modification prompt template to select the most appropriate target modification prompt template.
[0082] Exemplarily, among multiple modification prompt templates, there are at least two modification prompt templates for the same type of error, and different modification prompt templates for the same type of error contain modification examples of different complexity. For example, there are three modification prompt templates for the same type of error, which are at the elementary, intermediate, and advanced levels. The modification examples contained in the elementary modification prompt template involve basic SQL structures; the modification examples contained in the intermediate modification prompt template introduce more complex query elements such as grouping and subqueries; and the modification examples contained in the advanced modification prompt template cover advanced features such as window functions and complex JOIN operations.
[0083] In the process of screening the target modification prompt template that matches the error type described in the inspection result, the electronic device can screen out at least two candidate modification prompt templates that match the error type described in the inspection result from the preset multiple modification prompt templates; then the query requirement, query statement, inspection result and at least two candidate modification prompt templates are input into the pre-trained language model, so that the pre-trained language model determines the query complexity of the query requirement based on the query requirement, query statement and inspection result, and determines the target modification prompt template from at least two candidate modification prompt templates based on the query complexity. In this embodiment, the appropriate modification prompt template is selected according to the actual complexity of the query, so that the modification suggestion is more accurate. For example, for simple errors, a modification prompt template containing simple examples is selected to avoid using overly complex modification methods; for complex errors, a modification prompt template containing complex examples is selected to provide sufficient modification ideas. Automatically selecting the target modification prompt template according to the query complexity can prevent the modification from being too simple (unable to solve complex errors) and avoid excessive modification (introducing unnecessary complexity), ensuring that the modified query statement is both correct and efficient.
[0084] Next, see Figure 3 , in branch 1, for the inspection results that do not meet the inspection requirements, the electronic device embeds the inspection results and the query statement into the target modification prompt template to obtain a second prompt word, and then inputs the second prompt word into the pre-trained language model to instruct the pre-trained language model to modify the query statement with reference to the inspection results. Among them, in the second prompt word, the inspection result provides specific error information (such as syntax errors, index optimization suggestions, data type mismatch, etc.), and the target modification prompt template provides correction ideas, so that the pre-trained language model can generate a highly targeted modification plan. After the pre-trained language model outputs the modified query statement, the inspection task can be repeated for the modified query statement, that is, the query process described above.
[0085] In S204, if the inspection result meets the inspection requirements, the query statement is submitted to the database for execution, and the query result returned by the database is output.
[0086] In this step, see Figure 3 In branch 2, if the inspection result corresponding to the inspection task meets the inspection requirements, the query statement indicated by the inspection result can be submitted to the database for execution, thereby outputting high-quality query results. Automated query execution ensures that users can quickly obtain the required data, adapting to the requirements of real-time data analysis in the big data era.
[0087] In this embodiment, non-professional users can also obtain complex data queries through simple natural language input, reducing dependence on professional database writing and tuning skills. The process of automatically generating, checking and modifying query statements significantly shortens the time for query writing and debugging, and is particularly suitable for real-time analysis in massive data environments. The multiple check mechanism ensures the correctness of the query statement and avoids data security and integrity issues caused by erroneous queries. In short, the data query method uses a pre-trained language model to achieve automatic conversion from natural language query requirements to precise query statements, which not only improves query efficiency but also reduces the risk of errors, providing strong technical support for information extraction and decision support in the big data era. In one possible implementation, in order to ensure data query efficiency, the number of retries can be preset, and the above-mentioned preset conditions can also include checking that the number of executions of the task has not reached the preset number of retries. Please refer to Figure 4 After obtaining the inspection result (S401), the electronic device first determines whether the inspection result meets the inspection requirements (S402). If the inspection result does not meet the inspection requirements, it further determines whether the number of executions of the inspection task reaches the preset number of retries (S403).
[0088] If the inspection result does not meet the inspection requirements and the number of executions of the inspection task does not reach the preset number of retries, the electronic device uses the pre-trained language model to refer to the inspection result to modify the query statement and repeats the inspection task for the modified query statement (S404). By setting a preset number of retries, this embodiment can enhance fault tolerance and improve the accuracy of the query statement, while avoiding endless retries that consume the computing and storage resources of the electronic device.
[0089] When the inspection result does not meet the preset inspection conditions and the number of executions of the inspection task reaches the preset number of retries, the electronic device can submit the query statement to the database for execution and output the query result and inspection result returned by the database (S405). This embodiment can ensure that the user can ultimately obtain the query result without delaying or failing to execute the query due to excessive pursuit of perfection. Even if there are some problems with the query statement, the database will still execute the query and return the result, along with the inspection result, to inform the user of potential problems. This feedback mechanism helps the user understand the problems encountered during the query process and decide whether manual intervention is needed to further optimize or adjust the query requirements.
[0090] The various technical features in the above embodiments can be arbitrarily combined as long as there is no conflict or contradiction between the combinations of features. However, due to space limitations, they are not described one by one. Therefore, the arbitrary combination of the various technical features in the above embodiments also falls within the scope of disclosure of this specification.
[0091] In some embodiments, an embodiment of this specification further provides an electronic device, comprising: a processor; a memory for storing processor-executable instructions; wherein the processor implements any of the above methods by running the executable instructions.
[0092] Figure 5 This is a schematic structural diagram of a device provided by an exemplary embodiment. Figure 5 At the hardware level, the device includes a processor 502, an internal bus 504, a network interface 506, a memory 508, and a non-volatile memory 510. Of course, it may also include hardware required for other functions. One or more embodiments of this specification can be implemented based on software, such as the processor 502 reading the corresponding computer program from the non-volatile memory 510 into the memory 508 and then running it. Of course, in addition to software implementation, one or more embodiments of this specification do not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0093] In some embodiments, the data query device can be applied to Figure 5 The device shown in the figure is used to implement the technical solution of this specification. The data query device may include:
[0094] The query statement generation module is used to generate a query statement according to the query requirements input by the user and the data table information related to the query requirements in the database through a pre-trained language model.
[0095] The query statement checking module is used to perform checking tasks on query statements and generate checking results; the checking tasks include: checking the query statements using a database checker integrated with preset checking rules, and / or checking the query statements using a pre-trained language model.
[0096] The query statement modification module is used to modify the query statement using the pre-trained language model with reference to the inspection result if the preset conditions are not met, and repeat the inspection task for the modified query statement.
[0097] The query statement execution module is used to submit the query statement to the database for execution if the inspection result meets the inspection requirements, and output the query result returned by the database.
[0098] Exemplarily, the query statement generation module is specifically used to screen out a target generation prompt template that matches the query requirement from a plurality of preset generation prompt templates; wherein each generation prompt template corresponds to a type of query requirement and includes a plurality of reasoning steps and / or query examples related to the type of query requirement arranged in a thought chain manner, and the query example is used to describe the step-by-step conversion process of converting the textual example requirement into a query statement; the query requirement and data table information are embedded in the target generation prompt template to generate a first prompt word; and the first prompt word is input into a pre-trained language model to generate a query statement.
[0099] Exemplarily, the data table information is determined based on the query requirements and metadata used to describe the data organization structure of the database.
[0100] Exemplarily, the query statement checking module is specifically used to perform at least one of the following checks on the query statement using a database checker integrated with preset checking rules: in a first check, a table name set and a column name set related to all data tables in the database are generated through metadata used to describe the data organization structure of the database, and it is checked whether the table name described in the query statement exists in the table name set, and whether the column name described in the query statement exists in the column name set; in a second check, it is checked whether the syntax of the query statement meets the syntax requirements of the database; in a third check, a foreign key relationship graph of the database is obtained through metadata used to describe the data organization structure of the database, and the foreign key relationship graph is used to check whether the connection type and connection relationship described in the query statement are correct; in a fourth check, the data types of all data columns in the database are obtained through metadata used to describe the data organization structure of the database, and based on the data types of all data columns in the database, it is checked whether the operation logic involved in the query statement is reasonable.
[0101] Exemplarily, the query statement checking module is specifically used to input the query statement and the query requirement into the pre-trained language model, so that the pre-trained language model performs at least one of the following checks on the query statement: in the first check, check whether the query intent of the query statement is the same as the query intent described in the query requirement; in the second check, check whether the query statement contains the specified parameters in the query requirement; in the third check, check whether the output form defined by the query statement is the same as the output form specified by the query requirement.
[0102] Exemplarily, a query statement checking module is specifically used to execute query statements in a pre-built test environment to obtain execution information of the query statements, where the execution information includes the execution results and / or runtime status of the query statements; wherein a test database is deployed in the test environment, and the data organization structure of the test database is the same as the data organization structure of the database targeted by the query requirements; in the case of the first execution of the inspection task, the pre-trained language model is used to refer to the execution information of the query statement to evaluate the execution effect of the query statement, and provide execution optimization suggestions for the execution effect of the query statement; in the case of not the first execution of the inspection task, the pre-trained language model is used to refer to the execution information corresponding to the historical inspection tasks and the execution information of the query statement in the current inspection task to evaluate the execution effect of the query statement, and provide execution optimization suggestions for the execution effect of the query statement.
[0103] Exemplarily, the preset condition also includes: checking that the number of executions of the task has not reached a preset number of retries.
[0104] The query statement execution module is also used to submit the query statement to the database for execution if the inspection result does not meet the preset inspection conditions and the number of executions of the inspection task reaches the preset retry number, and output the query result and inspection result returned by the database.
[0105] Exemplarily, the query statement modification module is specifically used to filter out a target modification prompt template that matches the error type described in the inspection result from multiple preset modification prompt templates; wherein each modification prompt template corresponds to a type of error and contains modification examples related to the type of error; the inspection result and the query statement are embedded in the target modification prompt template to obtain a second prompt word; the second prompt word is input into the pre-trained language model to instruct the pre-trained language model to modify the query statement with reference to the inspection result.
[0106] Exemplarily, among the multiple modification prompt templates, there are at least two modification prompt templates for the same error type, and different modification prompt templates for the same error type contain modification examples of different complexity.
[0107] The query statement modification module is specifically used to screen out at least two candidate modification prompt templates that match the error type described in the inspection result from multiple preset modification prompt templates; input the query requirements, query statements, inspection results and at least two candidate modification prompt templates into a pre-trained language model, so that the pre-trained language model determines the query complexity of this query requirement based on the query requirements, query statements and inspection results, and determines the target modification prompt template from at least two candidate modification prompt templates based on the query complexity.
[0108] Exemplarily, the modification prompt template includes: a positive modification example for describing a correct modification method, and / or a negative modification example for describing an incorrect modification method.
[0109] The implementation process of the functions and effects of each module in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.
[0110] Based on the same concept as the above method, this specification also provides an electronic device, including: a processor; a memory for storing processor-executable instructions; wherein the processor implements the steps of the method described in any of the above embodiments by running the executable instructions.
[0111] Based on the same concept as the above method, this specification also provides a computer-readable storage medium on which computer instructions are stored. When the instructions are executed by a processor, the steps of the method described in any of the above embodiments are implemented.
[0112] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.
[0113] Based on the same concept as the above method, this specification also provides a computer program product, including a computer program / instruction, which implements the steps of the method described in any of the above embodiments when executed by a processor.
[0114] The above description is merely a preferred embodiment of one or more embodiments of this specification and is not intended to limit one or more embodiments of this specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of this specification shall be included in the scope of protection of one or more embodiments of this specification.
Claims
1. A data query method, comprising: Generate a query statement based on the query requirements input by the user and the data table information related to the query requirements in the database through the pre-trained language model; Execute the inspection task for the query statement and generate the inspection result; The checking task includes: checking the query statement using a database checker integrated with preset checking rules, and / or checking the query statement using the pre-trained language model; If the preset condition is not met, the preset condition at least includes that the inspection result does not meet the inspection requirement, modifying the query statement by using the pre-trained language model with reference to the inspection result, and repeating the inspection task for the modified query statement; If the inspection result meets the inspection requirement, the query statement is submitted to the database for execution, and the query result returned by the database is output.
2. The method according to claim 1, wherein generating a query statement according to a query requirement input by a user and data table information related to the query requirement in a database using a pre-trained language model comprises: Filtering a target generation prompt template that matches the query requirement from a plurality of preset generation prompt templates; wherein each generation prompt template corresponds to a type of query requirement and includes a plurality of reasoning steps and / or query examples related to the type of query requirement, arranged in a thought chain manner, wherein the query examples are used to describe a step-by-step conversion process of converting the textual example requirement into a query statement; Embedding the query requirement and the data table information into the target generation prompt template to generate a first prompt word; The first prompt word is input into the pre-trained language model to generate the query statement. 3 . The method according to claim 1 , wherein the data table information is determined based on the query requirement and metadata used to describe the data organization structure of the database.
4. The method according to claim 1, wherein the checking of the query statement by using a database checker integrated with preset checking rules comprises: Perform at least one of the following checks on the query statement using a database checker integrated with preset check rules: In the first check, a table name set and a column name set related to all data tables in the database are generated using metadata used to describe the data organization structure of the database, and a check is made to see whether the table name described in the query statement exists in the table name set, and whether the column name described in the query statement exists in the column name set; In the second check, whether the syntax of the query statement complies with the syntax requirements of the database; In the third check, a foreign key relationship map of the database is obtained through metadata used to describe the data organization structure of the database, and the foreign key relationship map is used to check whether the connection type and connection relationship described in the query statement are correct; In the fourth check, the data types of all data columns in the database are obtained through metadata used to describe the data organization structure of the database, and whether the operation logic involved in the query statement is reasonable is checked based on the data types of all data columns in the database.
5. The method according to claim 1, wherein the checking the query statement using the pre-trained language model comprises: Input the query statement and the query requirement into the pre-trained language model, so that the pre-trained language model performs at least one of the following checks on the query statement: In the first check, it is checked whether the query intent of the query statement is the same as the query intent of the query requirement description; In the second check, it is checked whether the query statement contains the specified parameters in the query requirement; In the third check, it is checked whether the output format defined by the query statement is the same as the output format specified by the query requirement.
6. The method according to claim 1, wherein the checking the query statement using the pre-trained language model comprises: Executing the query statement in a pre-built test environment to obtain execution information of the query statement, wherein the execution information includes the execution result and / or runtime status of the query statement; wherein a test database is deployed in the test environment, and the data organization structure of the test database is the same as the data organization structure of the database targeted by the query requirement; When the inspection task is performed for the first time, the pre-trained language model is used to refer to the execution information of the query statement, to evaluate the execution effect of the query statement, and to provide execution optimization suggestions based on the execution effect of the query statement; When this is not the first time the inspection task is executed, the pre-trained language model is used to refer to the execution information corresponding to the historical inspection tasks and the execution information of the query statement in this inspection task to evaluate the execution effect of the query statement and provide execution optimization suggestions based on the execution effect of the query statement.
7. The method according to claim 1, wherein the preset condition further comprises: The number of executions of the inspection task does not reach the preset number of retries; The method further comprises: If the inspection result does not meet the preset inspection conditions and the number of executions of the inspection task reaches the preset number of retries, the query statement is submitted to the database for execution, and the query result returned by the database and the inspection result are output.
8. The method according to claim 1, wherein modifying the query statement by using the pre-trained language model with reference to the inspection result comprises: Filtering out a target modification prompt template that matches the error type described in the inspection result from a plurality of preset modification prompt templates; wherein each modification prompt template corresponds to a class of error types and includes modification examples related to the class of error types; Embedding the inspection result and the query statement into the target modification prompt template to obtain a second prompt word; The second prompt word is input into the pre-trained language model to instruct the pre-trained language model to modify the query statement with reference to the inspection result.
9. The method according to claim 8, wherein among the plurality of modification prompt templates, there are at least two modification prompt templates for the same error type, and different modification prompt templates for the same error type include modification examples of different complexity; The step of selecting a target modification prompt template that matches the error type described in the inspection result from a plurality of preset modification prompt templates includes: Filtering at least two candidate modification prompt templates that match the error type described in the inspection result from a plurality of preset modification prompt templates; The query requirement, the query statement, the inspection result and the at least two candidate modification prompt templates are input into the pre-trained language model, so that the pre-trained language model determines the query complexity of this query requirement based on the query requirement, the query statement and the inspection result, and determines the target modification prompt template from the at least two candidate modification prompt templates based on the query complexity.
10. The method according to claim 8 or 9, wherein the modifying prompt template comprises: Positive modification examples used to describe correct modification methods, and / or negative modification examples used to describe incorrect modification methods.
11. An electronic device comprising: processor; A memory for storing processor-executable instructions; wherein the processor implements the steps of the method according to any one of claims 1 to 10 by running the executable instructions.
12. A computer-readable storage medium having computer instructions stored thereon, wherein when the instructions are executed by a processor, the steps of the method according to any one of claims 1 to 10 are implemented.
13. A computer program product comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 10.