Data query method and related equipment
Through the method of generating and evaluating candidate SQL query schemes based on large language models, the problem that traditional NL2SQL conversion system is difficult to generate efficient SQL statements when processing complex queries is solved, and efficient and accurate data query in complex query scenarios is achieved.
Patent Information
- Application Number
- CN202510179965.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-18
- Publication Date
- 2025-05-27
AI Technical Summary
Traditional natural language to SQL (NL2SQL) conversion systems are difficult to generate efficient and accurate SQL statements when processing complex queries, especially when faced with subqueries, nested queries or multi-table connections.
By generating multiple candidate SQL query schemes based on a large language model, comprehensively evaluated from multiple dimensions, selecting efficient and accurate SQL query schemes, and finally executing the selected SQL query scheme to complete data query.
It realizes the automatic generation of efficient and accurate SQL statements when facing complex queries, ensuring the accuracy and efficiency of data queries.
Smart Images

Figure CN120045642A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technologies, and in particular, to a data query method and related devices. Background Art
[0002] In a modern data-driven business environment, users often need to extract information from complex databases. However, users usually use natural language to express their query requirements, and these natural language query requirements need to be converted into Structured Query Language (SQL) for execution in the database. Traditional natural language to SQL (NL2SQL) conversion systems perform well when dealing with simple queries, but when faced with complex queries, especially those involving subqueries, nested queries, or multi-table joins, it is often difficult to generate efficient and accurate SQL statements. Summary of the Invention
[0003] In view of this, embodiments of the present disclosure provide a data query method, which can first generate multiple candidate SQL query solutions based on a large language model for a data query request input by a user in natural language form, then comprehensively evaluate the generated candidate SQL query solutions from multiple dimensions, and select an efficient and accurate SQL query solution from them. Finally, the data query is completed by executing the selected SQL query solution. The above data query method can not only support data query requests input in natural language form, but also automatically generate efficient and accurate SQL statements based on the input data query request when faced with complex queries, so as to efficiently obtain accurate data query results.
[0004] The data query method described in the embodiments of the present disclosure may include: receiving a data query request input by a user; generating, based on a large language model, multiple candidate Structured Query Language (SQL) query solutions corresponding to the data query request; respectively performing structure analysis on the multiple candidate SQL query solutions to obtain structure analysis results corresponding to the multiple candidate SQL query solutions; respectively performing performance analysis on the multiple candidate SQL query solutions to obtain performance analysis results corresponding to the multiple candidate SQL query solutions; determining a target SQL query solution from the multiple candidate SQL query solutions based on the structure analysis results and the performance analysis results; and executing the target SQL query solution to obtain and return a data query result corresponding to the data query request.
[0005] In an embodiment of the present disclosure, generating multiple candidate SQL query schemes corresponding to the data query request based on a large language model includes: generating a database query operation prompt based on the data query request; wherein the database query operation prompt includes: a database query operation task description and a thought chain related to the database query operation; and inputting the database query operation prompt into a pre-trained large language model, instructing the pre-trained large language model to output the multiple candidate SQL query schemes related to the database query operation task description according to the guidance of the thought chain related to the database query operation.
[0006] The data query method according to the embodiment of the present disclosure further includes: constructing a database query operation corpus; wherein the database query operation corpus includes: a plurality of data pairs composed of database query requests described in natural language and SQL query schemes corresponding to the above database query requests; and performing supervised fine-tuning on the pre-trained large language model based on the database query operation corpus to obtain a large language model after supervised fine-tuning.
[0007] In an embodiment of the present disclosure, generating multiple candidate SQL query schemes corresponding to the data query request based on a large language model includes: generating a database query operation prompt based on the data query request; wherein the database query operation prompt includes a database query operation task description; and inputting the database query operation prompt into the large language model after supervised fine-tuning, instructing the large language model after supervised fine-tuning to output the multiple candidate SQL query schemes related to the database query operation task description.
[0008] In an embodiment of the present disclosure, respectively performing a structural analysis on the multiple candidate SQL query schemes includes: respectively performing a structural complexity analysis of the SQL abstract syntax tree and / or a rationality analysis of SQL index usage on the multiple candidate SQL query schemes; and respectively determining the structural analysis results corresponding to the multiple candidate SQL query schemes based on the structural complexity and / or index rationality corresponding to the multiple candidate SQL query schemes.
[0009] In an embodiment of the present disclosure, performing the structural complexity analysis of the SQL abstract syntax tree includes: using an SQL engineering evaluator to respectively perform a structural complexity analysis of the SQL abstract syntax tree on the multiple candidate SQL query schemes, and respectively obtaining the structural complexity corresponding to the multiple candidate SQL query schemes.
[0010] In an embodiment of the present disclosure, the analysis of the rationality of SQL index usage includes: using an SQL project evaluator to separately analyze the rationality of SQL index usage for the multiple candidate SQL query plans, and separately obtaining the index rationality degrees corresponding to the multiple candidate SQL query plans.
[0011] In an embodiment of the present disclosure, the performance analysis of the multiple candidate SQL query plans respectively includes: performing one or any combination of accuracy analysis, execution performance analysis, and resource consumption analysis on the multiple candidate SQL query plans respectively; and respectively determining the performance analysis results corresponding to the multiple candidate SQL query plans based on one or any combination of the accuracy, execution performance, and resource consumption degrees corresponding to the multiple candidate SQL query plans.
[0012] In an embodiment of the present disclosure, the accuracy analysis includes: respectively performing the following operations for each candidate SQL query plan among the multiple candidate SQL query plans: generating an accuracy evaluation prompt based on the data query request and the candidate SQL query plan; inputting the accuracy evaluation prompt into a pre-trained large language model, instructing the pre-trained large language model to evaluate the accuracy of the candidate SQL query plan according to the data query request, and outputting the estimated accuracy of the candidate SQL query plan.
[0013] In an embodiment of the present disclosure, the execution performance analysis includes: respectively performing the following operations for each candidate SQL query plan among the multiple candidate SQL query plans: generating an execution performance parameter evaluation prompt based on the candidate SQL query plan; wherein the execution performance parameters include: query time and / or concurrent processing ability; inputting the execution performance parameter evaluation prompt into a pre-trained large language model, instructing the pre-trained large language model to evaluate the execution performance of the candidate SQL query plan, and outputting the execution performance evaluation result of the candidate SQL query plan.
[0014] In an embodiment of the present disclosure, the resource consumption analysis includes: using an SQL project evaluator to separately analyze the central processing unit usage rate and / or memory usage rate of the multiple candidate SQL query plans, and separately obtaining the resource consumption degrees corresponding to the multiple candidate SQL query plans.
[0015] The data query method according to an embodiment of the present disclosure may further include: respectively pre-executing the multiple candidate SQL query plans; and discarding the candidate SQL query plans that fail to execute.
[0016] The data query method described in the embodiments of the present disclosure may further include: determining candidate SQL query plans that need to be pre-executed based on the structural analysis results of the multiple candidate SQL query plans; pre-executing the candidate SQL query plans that need to be pre-executed respectively; discarding the candidate SQL query plans that fail to execute; and adding additional selection weights to the candidate SQL query plans that execute successfully.
[0017] The data query method described in the embodiments of the present disclosure may further include: before executing the target SQL query plan, pre-executing the target SQL query plan; in response to determining that the execution fails, discarding the target SQL query plan, and returning to the step of determining the target SQL query plan from the multiple candidate SQL query plans based on the structural analysis results and the performance analysis results; and in response to determining that the execution is successful, executing the target SQL query plan.
[0018] Corresponding to the above data query method, an embodiment of the present disclosure also discloses a data query device, including:
[0019] An input module, configured to receive a data query request input by a user;
[0020] A plan generation module, configured to generate candidate structured query language (SQL) query plans corresponding to the data query request based on a large language model;
[0021] A structure analysis module, configured to respectively perform structure analysis on the multiple candidate SQL query plans to obtain the structural analysis results corresponding to the multiple candidate SQL query plans;
[0022] A performance analysis module, configured to respectively perform performance analysis on the multiple candidate SQL query plans to obtain the performance analysis results corresponding to the multiple candidate SQL query plans;
[0023] A selection module, configured to determine a target SQL query plan from the multiple candidate SQL query plans based on the structural analysis results and the performance analysis results; and
[0024] An execution module, configured to execute the target SQL query plan, and obtain and return a data query result corresponding to the data query request.
[0025] In addition, an embodiment of the present disclosure further provides an electronic device, including: a memory, a processor, and a computer program stored on the memory and executable on the processor, where the processor implements the above data query method when executing the program.
[0026] Embodiments of the present disclosure also provide a non-transitory computer-readable storage medium storing computer instructions for causing a computer to execute the above data query method.
[0027] Embodiments of the present disclosure also provide a computer program product including computer program instructions that, when running on a computer, cause the computer to execute the above data query method.
[0028] As can be seen, the data query method and related devices provided by some embodiments of the present disclosure can first automatically generate multiple candidate SQL query plans based on a large language model for a data query request in the form of natural language input by a user, then comprehensively evaluate the generated candidate SQL query plans from multiple dimensions, and select an efficient and accurate SQL query plan from them. Finally, the data query is completed by executing the selected SQL query plan. The above data query method can not only support data query requests input in the form of natural language, but also automatically generate efficient and accurate SQL statements based on the input data query request when facing complex queries, so as to efficiently obtain accurate data query results. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] To more clearly illustrate the technical solutions in the present disclosure or related technologies, the following will briefly introduce the drawings required for use in the description of the embodiments or related technologies. Obviously, the drawings in the following description are only embodiments of the present disclosure. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0030] Figure 1 Shows the implementation process of the data query method described in some embodiments of the present disclosure.
[0031] Figure 2 Shows the implementation process of the method for generating candidate SQL query plans corresponding to data query requests based on a large language model described in some embodiments of the present disclosure.
[0032] Figure 3 Shows the implementation process of the method for performing supervised fine-tuning on a large language model described in some embodiments of the present disclosure.
[0033] Figure 4 Shows the implementation process of the method for pre-executing candidate SQL query plans described in some embodiments of the present disclosure.
[0034] Figure 5 Shows the internal structure of the data query device described in some embodiments of the present disclosure.
[0035] Figure 6 FIG. 1 shows a more specific schematic diagram of the hardware structure of an electronic device according to some embodiments of the present disclosure. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0036] To make the objectives, technical solutions, and advantages of the present disclosure more clear and understandable, the present disclosure will be further described in detail below with reference to specific embodiments and the accompanying drawings.
[0037] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in the embodiments of the present disclosure should have the ordinary meanings understood by those of ordinary skill in the art to which the present disclosure belongs. The terms "first", "second", and similar terms used in the embodiments of the present disclosure do not denote any order, quantity, or importance, but are only used to distinguish different components. The terms "including", "comprising", or similar terms mean that the elements or items appearing before the term cover the elements or items listed after the term and their equivalents, without excluding other elements or items. The terms "connected" or "coupled" do not limit to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The terms "upper", "lower", "left", "right", etc. are only used to represent relative positional relationships, and when the absolute position of the object being described changes, the relative positional relationship may also change accordingly.
[0038] It can be understood that before using the technical solutions of the various embodiments of the present disclosure, the types, usage scopes, usage scenarios, etc. of the personal information involved will be informed to the user in an appropriate manner, and the user's authorization will be obtained.
[0039] For example, in response to receiving an active request from the user, a prompt message is sent to the user to clearly prompt the user that the operation requested by the user will require obtaining and using the user's personal information. Thus, the user can autonomously choose whether to provide personal information to the electronic device, application program, server, storage medium, or other software or hardware that executes the operations of the technical solutions of the present disclosure according to the prompt message.
[0040] As an optional but non-limiting implementation manner, the manner of sending a prompt message to the user in response to receiving an active request from the user may be, for example, in the form of a pop-up window, and the prompt message may be presented in text in the pop-up window. In addition, the pop-up window may also carry a selection control for the user to choose "agree" or "disagree" to provide personal information to the electronic device.
[0041] It can be understood that the above process of notifying and obtaining the user's authorization is only illustrative and does not limit the implementation manner of the present disclosure. Other ways that meet relevant laws and regulations can also be applied to the implementation manner of the present disclosure.
[0042] As described above, in a modern data-driven business environment, there is a need to convert the query requirements of natural language input by users into SQL statements for execution in a database. Traditional NL2SQL methods typically include: semantic parsing-based methods, rule-based methods, deep learning-based methods, and interactive methods, etc. These methods perform well in handling simple queries, but when faced with complex queries, especially those involving subqueries, nested queries, or multi-table joins, it is often difficult to generate efficient and accurate SQL statements.
[0043] In view of this, some embodiments of the present disclosure provide a data query method, which can first generate multiple candidate SQL query schemes based on a large language model for the data query request in the form of natural language input by the user, then evaluate the generated candidate SQL query schemes from multiple dimensions, and select an efficient and accurate SQL query scheme according to the evaluation results, so as to complete the data query.
[0044] Figure 1 Shows the implementation process of the data query method described in some embodiments of the present disclosure. As Figure 1 shown, the above data query method may include the following multiple steps:
[0045] In step 110, receive the data query request input by the user.
[0046] In the embodiments of the present disclosure, the above data query request input by the user is usually a data query request in the form of natural language. For example, in a specific example, the data query request input by the user may be "find the top ten products with the highest sales in the past year and their sales amounts".
[0047] As described above, since the above data query request input by the user is in the form of natural language, it is necessary to first generate a corresponding SQL statement based on the above data query request. Then, the query operation on the database is completed by executing the generated SQL statement, so as to obtain the data required by the user.
[0048] In step 120, generate multiple candidate SQL query schemes corresponding to the above data query request based on the large language model.
[0049] In step 130, respectively perform structural analysis on the above multiple candidate SQL query schemes to obtain the structural analysis results corresponding to the above multiple candidate SQL query schemes.
[0050] In step 140, respectively perform performance analysis on the above multiple candidate SQL query schemes to obtain the performance analysis results corresponding to the above multiple candidate SQL query schemes.
[0051] In step 150, a target SQL query plan is determined from the multiple candidate SQL query plans based on the above structural analysis result and the above performance analysis result.
[0052] In step 160, the above target SQL query plan is executed to obtain and return a data query result corresponding to the above data query request.
[0053] The following will specifically describe the specific implementation methods of each step of the data query method described in the embodiments of the present disclosure with specific examples.
[0054] Regarding the above step 120, in some embodiments of the present disclosure, the above large language model may specifically be a pre-trained large language model. Alternatively, in some other embodiments of the present disclosure, the above large language model may also be a supervised fine-tuned large language model obtained by performing supervised fine-tuning on a pre-trained large language model.
[0055] For the method of using a pre-trained large language model, Figure 2 shows the method of inputting a data query request into a large language model described in the embodiments of the present disclosure. As Figure 2 shown, the above method may specifically include the following steps:
[0056] In step 210, a database query operation prompt is generated based on the data query request input by the user.
[0057] In the embodiments of the present disclosure, the above database query operation prompt may specifically include: a database query operation task description and a chain of thought related to the database query operation.
[0058] Among them, the above database query operation task description may be generated based on a preset template and the data query request input by the user. The above database query operation task description is used to instruct the large language model to output multiple candidate SQL query plans related to the database query operation task description.
[0059] The above chain of thought related to the database query operation may include SQL examples related to various database query operations. It can be understood that the chain of thought (Chain of Thought, abbreviated as CoT), as a prompting technique, by adding a step-by-step thinking process that simulates human problem-solving in the database query operation prompt, can guide the large language model to simulate the step-by-step thinking process of human problem-solving, thereby generating multiple different SQL query plans based on the data query request input by the user, thus significantly improving the performance of the large language model in the database query task.
[0060] In some other embodiments of the present disclosure, in addition to the above-described database query operation task description and the chain of thought related to the database query operation, in order to assist the large language model to more accurately complete the database query task, constraint conditions can also be generated according to the actual requirements of the underlying database and input into the large language model as part of the above-described database query operation prompt. Of course, the above-described database query operation prompt can also include other information that can assist the large language model to efficiently and accurately generate SQL statements, and the present application does not limit this.
[0061] In step 220, input the above-described database query operation prompt into the pre-trained large language model, instructing the pre-trained large language model to output multiple candidate SQL query schemes related to the database query operation task description under the guidance of the chain of thought related to the database query operation.
[0062] For the above-described large language model that has been fine-tuned with supervision, in the embodiments of the present disclosure, the pre-trained large language model can be first fine-tuned with supervision. Then, a database query operation prompt is generated based on the data query request. Finally, the database query operation prompt is input into the large language model that has been fine-tuned with supervision, instructing the large language model that has been fine-tuned with supervision to output multiple candidate SQL query schemes related to the database query operation task description. In some embodiments of the present disclosure, the above-described database query operation prompt can include a task description generated based on the above-described data query request. In some other embodiments of the present disclosure, in addition to the above-described database query operation task description, the above-described database query operation prompt can also include constraint conditions generated according to the actual requirements of the underlying database. Of course, the above-described database query operation prompt can also include other information that can assist the large language model to efficiently and accurately generate SQL statements, and the present application does not limit this.
[0063] Figure 3 Shows the implementation process of the method for supervised fine-tuning of the large language model described in the embodiments of the present disclosure. As Figure 3 shown, the method for supervised fine-tuning of the large language model can include the following steps:
[0064] In step 310, construct a database query operation corpus.
[0065] In the embodiments of the present disclosure, the above-described database query operation corpus can include: a data pair composed of multiple database query requests described in natural language and the corresponding SQL query schemes for the above-described database query requests.
[0066] In step 320 above, the pre-trained large language model is subjected to supervised fine-tuning based on the above database query operation corpus to obtain a large language model that has been subjected to supervised fine-tuning.
[0067] It can be understood that during the process of performing supervised fine-tuning on the above pre-trained large language model, the database query requests described in natural language in the above data pair are used as inputs, and then the SQL query solutions in the above data pair are used to supervise the output of the above pre-trained large language model. It can be seen that in the above embodiment, by constructing the data pair composed of the database query request described in natural language and the SQL query solution corresponding to the database query request, and using the above data pair to perform supervised fine-tuning on the pre-trained large language model, the large language model can be assisted in learning the relationship between the query request and the SQL query solution, so that the fine-tuned large language model can output a more effective and accurate SQL query solution based on a simple prompt.
[0068] Regarding step 130 above, in the embodiments of the present disclosure, the structural analysis of a candidate SQL query solution may specifically include the analysis of structural indicators in multiple dimensions. For example, the above structural analysis may include: one or a combination of the analysis of the structural complexity of the SQL Abstract Syntax Tree (AST) and the analysis of the rationality of SQL index usage. In this way, in step 130 above, the structural complexity analysis of the SQL AST and / or the rationality analysis of SQL index usage can be performed on multiple candidate SQL query solutions respectively to obtain the structural complexity and / or index rationality; then, based on the structural complexity and / or index rationality corresponding to the determined multiple candidate SQL query solutions, the structural analysis results corresponding to the multiple candidate SQL query solutions are determined respectively. In the embodiments of the present disclosure, the above structural analysis results may specifically be in the form of scores.
[0069] It can be understood that for SQL, existing evaluation tools for evaluating the structural complexity and index usage of SQL can be used to implement the above structural complexity analysis and index rationality analysis. In the embodiments of the present disclosure, the above evaluation tool is referred to as a SQL engineering evaluator.
[0070] Thus, in some embodiments of the present disclosure, the above-mentioned structural complexity analysis of the SQL AST may include: using a SQL engineering evaluator to respectively perform structural complexity analysis of the SQL AST on the multiple candidate SQL query schemes, and respectively obtaining the structural complexity corresponding to the multiple candidate SQL query schemes. Specifically, the above-mentioned SQL engineering evaluator may analyze the SQL query scheme from multiple aspects such as the length of the SQL statement, the nesting level, and the number of subqueries used. The output result of the structural complexity analysis is usually a structural complexity score or a description of the structural complexity level. For the case where the output result is directly a structural complexity score, the embodiments of the present disclosure may first normalize the structural complexity score output by the SQL engineering evaluator for subsequent integration with other dimensional metrics. For the case where the output result is directly a description of the structural complexity level, the embodiments of the present disclosure may quantify the description of the structural complexity level output by the SQL engineering evaluator based on a pre-set quantization standard to obtain a structural complexity score for subsequent integration with other dimensional metrics. For example, for a simple query with no nesting and moderate length in the evaluation result, 5 points may be assigned (the score range is 0-5, and the higher the score, the lower the structural complexity); for a moderately complex query with a small amount of nesting and moderate use of subqueries in the evaluation result, 3-4 points may be assigned; and for a complex query with multiple levels of nesting and excessive use of subqueries in the evaluation result, 1-2 points may be assigned.
[0071] In addition, in some embodiments of the present disclosure, the above-mentioned analysis of the rationality of SQL index usage includes: using a SQL engineering evaluator to respectively perform analysis of the rationality of SQL index usage on the multiple candidate SQL query schemes, and respectively obtaining the index rationality corresponding to the multiple candidate SQL query schemes. Similar to the structural complexity metric, the above-mentioned SQL engineering evaluator analyzes aspects such as whether an index is used in the query and whether the index selection is reasonable. The output result of the index rationality analysis can be an index rationality score or a description of the index rationality level. For the case where the output result is directly an index rationality score, the embodiments of the present disclosure may first normalize the index rationality score output by the SQL engineering evaluator for subsequent integration with other dimensional metrics. For the case where the output result is directly a description of the index rationality level, the embodiments of the present disclosure may quantify the description of the index rationality level output by the SQL engineering evaluator based on a pre-set quantization standard to obtain an index rationality score for integration with other dimensional metrics. For example, for a level with reasonable index usage and excellent query performance in the evaluation result, 5 points may be assigned (the score range is 0-5, and the higher the score, the higher the index rationality); for a level with partial index usage but room for optimization in the evaluation result, 3-4 points may be assigned; and for a level with no index usage or improper index selection in the evaluation result, 1-2 points may be assigned.
[0072] Further, after determining the structural complexity and index rationality corresponding to the above-mentioned multiple candidate SQL query plans, the above-mentioned structural complexity and index rationality can be directly used as the structural analysis results corresponding to the above-mentioned multiple candidate SQL query plans. Alternatively, as an alternative to the above solution, the structural complexity and index rationality can also be weighted and summed (or weighted averaged) by the weight values corresponding to the respective indicators determined in advance, so as to respectively determine the structural analysis results corresponding to the multiple candidate SQL query plans. It can be seen that in this way, the structural analysis results corresponding to the above-mentioned respective candidate SQL query plans can also be reflected in the form of scoring. Usually, the higher the score, the lower the structural complexity and the higher the index rationality.
[0073] For step 140 above, in the embodiments of the present disclosure, the performance analysis of the candidate SQL query plan can specifically also include the analysis of performance aspect indicators in multiple dimensions. For example, the above performance analysis can include: performing one or any combination of accuracy analysis, execution performance analysis, and resource consumption analysis on the candidate SQL query plan. In this way, in step 140, the accuracy analysis, execution performance analysis, and resource consumption analysis can be respectively performed on the multiple candidate SQL query plans to obtain one or any combination of accuracy, execution performance, and resource consumption; then, based on one or any combination of the accuracy, execution performance, and resource consumption corresponding to the multiple candidate SQL query plans, the performance analysis results corresponding to the multiple candidate SQL query plans are respectively determined. Usually, the performance analysis results corresponding to the above-mentioned candidate SQL query plans can also be in the form of scoring.
[0074] In the embodiments of the present disclosure, the above accuracy analysis and execution performance analysis can be implemented through a large language model, while the resource consumption analysis can also be implemented through the above SQL engineering evaluator or large language model.
[0075] In some embodiments of the present disclosure, the above accuracy analysis may include: performing the following operations for each candidate SQL query plan among the multiple candidate SQL query plans respectively: First, generate an accuracy evaluation prompt based on the above data query request and the above candidate SQL query plan; then, input the accuracy evaluation prompt into a pre-trained large language model, instructing the pre-trained large language model to evaluate the accuracy of the candidate SQL query plan according to the above data query request, and output the accuracy of the above candidate SQL query plan. In the embodiments of the present disclosure, the accuracy output by the above large language model can be directly a normalized accuracy score.
[0076] In some embodiments of the present disclosure, the above-mentioned execution performance analysis may include: performing the following operations for each of a plurality of candidate SQL query schemes respectively: First, generating an execution performance parameter evaluation prompt based on the candidate SQL query scheme; wherein, the execution performance parameters include: query time and / or concurrent processing ability; inputting the above-mentioned execution performance parameter evaluation prompt into a pre-trained large language model, instructing the pre-trained large language model to evaluate the execution performance of the candidate SQL query scheme, and outputting an execution performance evaluation result of the candidate SQL query scheme. In the embodiments of the present disclosure, the execution performance evaluation result output by the above-mentioned large language model may directly be a normalized execution performance score.
[0077] In some embodiments of the present disclosure, the above-mentioned resource consumption analysis may include: using an SQL engineering evaluator to respectively analyze the central processing unit (CPU) usage rate and / or memory usage rate of a plurality of candidate SQL query schemes, and respectively obtaining the resource consumption degrees corresponding to the plurality of candidate SQL query schemes. Specifically, the above-mentioned SQL engineering evaluator will evaluate the resource consumption of the SQL scheme according to historical execution results, including memory usage and CPU occupancy, etc., to avoid excessive consumption of system resources. The resource consumption degree analysis result output by the above-mentioned SQL engineering evaluator may be a resource consumption degree score or a resource consumption degree level description. For the case where the output result is directly a resource consumption degree score, the embodiments of the present disclosure may normalize the resource consumption degree score output by the SQL engineering evaluator for subsequent integration with other dimension indicators. For the case where the output result is directly a resource consumption degree level description, the embodiments of the present disclosure may quantify the resource consumption degree level description output by the SQL engineering evaluator based on a pre-set quantization standard, so as to obtain a resource consumption degree score for subsequent integration with other dimension indicators.
[0078] As an alternative to the above solution, in some other embodiments of the present disclosure, the above-mentioned resource consumption analysis may include: using a large language model to respectively analyze the central processing unit (CPU) usage rate and / or memory usage rate of a plurality of candidate SQL query schemes based on historical execution results, and respectively obtaining the resource consumption degrees corresponding to the plurality of candidate SQL query schemes. It can be understood that the resource consumption degree analysis result output by the above-mentioned large language model may generally be a normalized resource consumption degree score.
[0079] Further, after determining the accuracy rate, execution performance, and resource consumption corresponding to the multiple candidate SQL query solutions, the above accuracy rate, execution performance, and resource consumption can be directly used as the performance analysis results corresponding to the multiple candidate SQL query solutions. Alternatively, as an alternative to the above solution, the accuracy rate, execution performance, and resource consumption can also be weighted and summed (or weighted averaged) using the weight values corresponding to the respective indicators determined in advance, so as to determine the performance analysis results corresponding to the multiple candidate SQL query solutions respectively. It can be seen that in this way, the performance analysis results corresponding to the above candidate SQL query solutions can also be reflected by means of scoring, where the higher the score, the higher the accuracy rate, the better the execution performance, and the lower the resource consumption.
[0080] Next, after respectively determining the structural analysis results and performance analysis results corresponding to the multiple candidate SQL query solutions by the above method, in step 150 above, the structural analysis results and performance analysis results can be weighted and summed (or weighted averaged) to respectively obtain the comprehensive scores corresponding to the multiple candidate SQL query solutions, and the candidate SQL query solution with the highest comprehensive score can be selected as the target SQL query solution. It can be understood that the higher the score of the comprehensive score, the simpler the SQL structure and the better the performance.
[0081] In this way, in step 160 above, after determining the target SQL query solution, the target SQL query solution can be executed to obtain the data query result corresponding to the above data query request, and the obtained query result can be returned to the user.
[0082] Further, in order to enhance the transparency of the data query method and further improve the user experience, the data query method described in the embodiments of the present disclosure may further include: First, generate reasons for selecting the target SQL query solution based on the structural analysis results and performance analysis results corresponding to the multiple candidate SQL query solutions; then, return the generated reasons to the user. In this way, the user can not only obtain the data query result for the data query request, but also learn the SQL query solution selected by the system and the reasons for selecting this SQL query solution.
[0083] Specifically, in some examples, the generation of the above reasons can be implemented by engineering based on a preset template. In other examples, the generation of the above selection reasons can also be implemented based on a large language model, that is, generate a prompt based on the structural analysis results, performance analysis results corresponding to the multiple candidate SQL query solutions, and the target SQL query solution, and input the prompt into the large language model, and the large language model directly outputs the reasons for selecting the target SQL query solution.
[0084] It can be seen that in the above data query method, multiple candidate SQL query solutions can be automatically generated based on the large language model for the data query request in the form of natural language input by the user. Then, the generated candidate SQL query solutions are comprehensively evaluated from multiple dimensions, and an efficient and accurate SQL query solution is selected from them. Finally, the data query is completed by executing the selected SQL query solution. The above data query method can not only support data query requests input in the form of natural language, but also automatically generate efficient and accurate SQL statements based on the input data query request when facing complex queries, so as to efficiently obtain accurate data query results.
[0085] To further ensure the feasibility of the selected target SQL query solution, the embodiments of the present disclosure further add a process of pre-executing the candidate SQL query solutions on the basis of the above data query method.
[0086] In some embodiments of the present disclosure, the above pre-execution process may include: First, pre-execute the above multiple candidate SQL query solutions respectively; Then, discard the candidate SQL query solutions that fail to execute. It can be seen that through the above pre-execution operation, the candidate SQL query solutions that fail to execute can be discarded, so as to only retain the candidate SQL query solutions that execute successfully, and further ensure the feasibility of the selected target SQL query solution.
[0087] It should be particularly noted that in the embodiments of the present disclosure, the above pre-execution needs to be performed without affecting the state of the underlying database. That is, in some examples, the above pre-execution operation can be selected to be performed when the database is idle, so as not to affect the operations of other databases. Or, in other examples, a test database can be used for the above pre-execution, which can not only verify the feasibility of the candidate SQL query solutions, but also not affect the state of the database in use.
[0088] To further reduce the time and resources consumed by the pre-execution, the embodiments of the present disclosure also give an alternative pre-execution method, as Figure 4 shown, which may include:
[0089] In step 410, determine the candidate SQL query solutions that need to be pre-executed based on the structural analysis results of the multiple candidate SQL query solutions.
[0090] In an embodiment of the present disclosure, a predetermined number or a predetermined proportion of candidate SQL query plans with relatively high structural analysis result scores may be selected as the candidate SQL query plans to be pre-executed as described above. As an alternative to the above solution, only one metric of the structural complexity of the SQL AST may be considered during selection, that is, a predetermined number or a predetermined proportion of candidate SQL query plans with relatively low SQL AST structural complexity are selected as the candidate SQL query plans to be pre-executed as described above.
[0091] In step 420, the candidate SQL query plans to be pre-executed as described above are pre-executed respectively.
[0092] In step 430, the candidate SQL query plans that fail to execute are discarded; and
[0093] In step 440, additional selection weights are added to the candidate SQL query plans that execute successfully.
[0094] In an embodiment of the present disclosure, the above additional selection weights may be added to the comprehensive score of the candidate SQL query plans in the above step 150, thereby increasing the possibility of their being selected. The magnitude of the value of the above additional selection weights can be flexibly set according to actual needs, and the embodiments of the present disclosure do not limit this.
[0095] As an alternative to the above solution, after the target SQL query plan is determined, the above target SQL query plan may also be pre-executed. If the execution fails, the target SQL query plan is deleted from the candidate SQL query plans, and then step 150 is returned to re-select the target SQL query plan. If the execution is successful, step 160 is continued to execute the determined target SQL query plan, and the data query result corresponding to the above data query request is obtained and returned.
[0096] It can be seen from this that no matter which of the above pre-execution solutions is adopted, the feasibility of the selected target SQL query plan can be basically ensured, thereby avoiding the situation where the selected target SQL query plan cannot be successfully executed and improving the user experience.
[0097] The above data query method will be described below in combination with a specific example. Suppose the data query request input by the user in natural language form is: "Find the top ten products with the highest sales in the past year and their sales amounts." Then the data query method described in the embodiments of the present disclosure will complete the data query according to the following process:
[0098] 1. Generate a database query operation prompt based on the user's input data query request "Find the top ten products with the highest sales in the past year and their sales amounts". Specifically, the database query operation prompt will include a task description for generating multiple candidate SQL query schemes based on the above data query request. Further, the database query operation prompt may also include a chain of thought for guiding the large language model to accurately output multiple candidate SQL query schemes; and / or limiting conditions for explaining the underlying database requirements, etc.
[0099] 2. The large language model converts the user's input data query request into multiple SQL query schemes based on the database query operation prompt. For example, the large language model generates the following three SQL query schemes: SQL query scheme A, which focuses on using subqueries to handle complex association relationships and obtains the required data by nesting data query requests; SQL query scheme B, which uses window functions to group, sort, and calculate in the query results; SQL query scheme C, which mainly relies on aggregation operations to summarize and statistically analyze large amounts of data.
[0100] 3. Use the SQL engineering evaluator to evaluate the above three SQL query schemes from aspects such as the complexity of the SQL AST structure, the use of SQL indexes, and resource consumption, and obtain the following evaluation results:
[0101] ■ For SQL query scheme A, the complexity of the SQL AST structure is 4 points, the rationality of the SQL index is 4 points, and the resource consumption is 3 points.
[0102] ■ For SQL query scheme B, the complexity of the SQL AST structure is 3 points, the rationality of the SQL index is 3 points, and the resource consumption is 3 points.
[0103] ■ For SQL query scheme C, the complexity of the SQL AST structure is 2 points, the rationality of the SQL index is 1 point, and the resource consumption is 2 points.
[0104] 4. Use the large language model to evaluate the above three SQL query schemes from aspects such as accuracy and execution performance, and obtain the following evaluation results:
[0105] ■ For SQL query scheme A, the accuracy is 4 points and the execution performance is 4 points.
[0106] ■ For SQL query scheme B, the accuracy is 3 points and the execution performance is 3 points.
[0107] ■ For SQL query scheme C, the accuracy is 2 points and the execution performance is 2 points.
[0108] 5. Pre-execute the SQL query plan A with a lower SQL AST complexity among the above three SQL query plans. The pre-execution is successful, and an additional selection weight of 0.1 is added to the SQL query plan A.
[0109] 6. For the above three SQL query plans, respectively perform weighted summation (or weighted average) on the above indicators of SQL AST structure complexity, SQL index usage, resource consumption, accuracy, and execution performance to obtain the comprehensive scores of the above three SQL query plans. And add the additional selection weight of 0.1 of the SQL query plan A to the comprehensive score of the SQL query plan A. Select the SQL query plan A with the highest comprehensive score among the above three SQL query plans as the target SQL query plan.
[0110] 7. Generate the reasons for selecting the SQL query plan A based on the above analysis process and feedback the above reasons to the user, explaining that the reason for selecting the SQL query plan A is that it performs well in terms of complexity, SQL index usage, resource consumption, accuracy, and execution performance.
[0111] 8. Execute the target SQL query plan (i.e., the SQL query plan A), query the top ten products with the highest sales in the past year and their sales amounts in the database, and return the query results to the user.
[0112] As can be seen from the above specific examples, the data query method described in the embodiments of the present disclosure can handle complex natural language data query requests and can efficiently provide accurate data query results for users.
[0113] Corresponding to the above data query method, an embodiment of the present disclosure also discloses a data query device. Figure 5 Shows the internal structure of the data query device described in the embodiments of the present disclosure. As Figure 5 shown, the above data query device may include:
[0114] An input module 510, configured to receive a data query request input by a user;
[0115] A scheme generation module 520, configured to generate a plurality of candidate SQL query schemes corresponding to the data query request based on a large language model;
[0116] A structure analysis module 530, configured to respectively perform structure analysis on the plurality of candidate SQL query schemes to obtain structure analysis results corresponding to the plurality of candidate SQL query schemes;
[0117] A performance analysis module 540, configured to respectively perform performance analysis on the plurality of candidate SQL query schemes to obtain performance analysis results corresponding to the plurality of candidate SQL query schemes;
[0118] A selection module 550, configured to determine a target SQL query solution from the multiple candidate SQL query solutions based on the structure analysis result and the performance analysis result; and
[0119] An execution module 560, configured to execute the target SQL query solution, obtain and return a data query result corresponding to the data query request.
[0120] Further, the above data query device may further include a pre-execution module, configured to pre-execute the candidate SQL query solutions, thereby effectively improving the feasibility of the target SQL query solution.
[0121] It should be noted that each of the above modules may be implemented by using the specific implementation manners of the respective steps in the data query method described in the foregoing embodiments, which will not be elaborated herein.
[0122] Based on the same inventive concept, corresponding to the method in any of the above embodiments, the present disclosure further provides an electronic device, including: a memory, a processor, and a computer program stored on the memory and executable on the processor, where when the processor executes the program, it implements the data query method described in any of the above embodiments.
[0123] Figure 6 FIG. shows a schematic hardware structure diagram of a more specific electronic device provided in this embodiment. The device may include: a processor 2010, a memory 2020, an input / output interface 2030, a communication interface 2040, and a bus 2050. Among them, the processor 2010, the memory 2020, the input / output interface 2030, and the communication interface 2040 are communicatively connected to each other inside the device through the bus 2050.
[0124] The processor 2010 may be implemented in a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, etc., and is configured to execute relevant programs to implement the technical solutions provided in the embodiments of this specification.
[0125] The memory 2020 can be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage devices, dynamic storage devices, etc. The memory 2020 can store an operating system and other application programs. When implementing the technical solutions provided in the embodiments of this specification through software or firmware, the relevant program codes are stored in the memory 2020 and are called and executed by the processor 2010.
[0126] The input / output interface 2030 is used to connect to input / output devices to achieve information input and output. Among them, the input / output devices can be configured as components in the device or externally connected to the device to provide corresponding functions. The input devices can include microphones, various sensors, etc., and the output devices can include displays, speakers, vibrators, indicator lights, etc.
[0127] The communication interface 2040 is used to connect to a communication module (not shown in the figure) to achieve communication interaction between this device and other devices. The communication module can achieve communication through wired means (such as USB, network cable, etc.) or through wireless means (such as mobile network, WIFI, Bluetooth, etc.).
[0128] The bus 2050 includes a path for transmitting information between various components of the device (such as the processor 2010, the memory 2020, the input / output interface 2030, and the communication interface 2040).
[0129] It should be noted that although the above device only shows the processor 2010, the memory 2020, the input / output interface 2030, the communication interface 2040, and the bus 2050, in the specific implementation process, the device may also include other components necessary for normal operation. In addition, those skilled in the art can understand that the above device may also only include the components necessary to implement the solutions of the embodiments of this specification and do not necessarily include all the components shown in the figure.
[0130] The electronic device in the above embodiment is used to implement the corresponding data query method in any of the foregoing embodiments and has the beneficial effects of the corresponding method embodiments, which will not be elaborated here.
[0131] Based on the same inventive concept, corresponding to the method in any of the above embodiments, the present disclosure also provides a non-transitory computer-readable storage medium storing computer instructions for causing the computer to execute the data query method described in any of the foregoing embodiments.
[0132] The computer-readable media of this embodiment include both permanent and non-permanent, removable and non-removable media, and information storage can be implemented by any method or technology. The 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 technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette tapes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible by a computing device.
[0133] The computer instructions stored in the storage media of the above embodiments are used to cause the computer to execute the data query method described in any of the above embodiments, and have the beneficial effects of the corresponding method embodiments, which will not be elaborated here.
[0134] Those of ordinary skill in the art should understand that: the discussion of any of the above embodiments is only exemplary and is not intended to imply that the scope of the present disclosure (including the claims) is limited to these examples; under the concept of the present disclosure, the technical features in the above embodiments or different embodiments can also be combined, the steps can be implemented in any order, and there are many other variations in different aspects of the embodiments of the present disclosure as described above, and they are not provided in detail for the sake of brevity.
[0135] In addition, for the sake of simplicity of explanation and discussion, and in order not to make the embodiments of the present disclosure difficult to understand, the well-known power / ground connections to integrated circuit (IC) chips and other components may or may not be shown in the provided drawings. Moreover, the devices may be shown in block diagram form to avoid making the embodiments of the present disclosure difficult to understand, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform on which the embodiments of the present disclosure are to be implemented (i.e., these details should be fully within the understanding of those skilled in the art). In the case where specific details (such as circuits) are set forth to describe the exemplary embodiments of the present disclosure, it will be apparent to those skilled in the art that the embodiments of the present disclosure can be implemented without these specific details or with variations of these specific details. Therefore, these descriptions should be considered illustrative rather than restrictive.
[0136] Although the present disclosure has been described in connection with specific embodiments thereof, many alternatives, modifications, and variations of these embodiments will be apparent to those of ordinary skill in the art in light of the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) may be used with the embodiments discussed.
[0137] Embodiments of the present disclosure are intended to cover all such alternatives, modifications, and variations that fall within the broad scope of the appended claims. Accordingly, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the embodiments of the present disclosure shall be included within the protection scope of the present disclosure.
Claims
1. A data query method, comprising: Receive data query requests input by users; Generate multiple candidate structured query language SQL query solutions corresponding to the data query request based on the large language model; Performing structural analysis on the multiple candidate SQL query solutions respectively to obtain structural analysis results corresponding to the multiple candidate SQL query solutions; Performing performance analysis on the multiple candidate SQL query solutions respectively to obtain performance analysis results corresponding to the multiple candidate SQL query solutions; Determine a target SQL query solution from the plurality of candidate SQL query solutions based on the structural analysis result and the performance analysis result; as well as Execute the target SQL query solution to obtain and return a data query result corresponding to the data query request.
2. The method according to claim 1, wherein: Generating multiple candidate SQL query solutions corresponding to the data query request based on the large language model includes: Generate a database query operation prompt based on the data query request; wherein the database query operation prompt includes: a database query operation task description and a thought chain related to the database query operation; and The database query operation prompt is input into a pre-trained large language model, and the pre-trained large language model is instructed to output the multiple candidate SQL query solutions related to the database query operation task description according to the guidance of the thought chain related to the database query operation.
3. The method according to claim 1, further comprising: Constructing a database query operation corpus; wherein the database query operation corpus includes: a plurality of data pairs consisting of database query requests described in natural language and SQL query solutions corresponding to the above database query requests; and The pre-trained large language model is fine-tuned in a supervised manner based on the database query operation corpus to obtain a supervised fine-tuned large language model.
4. The method according to claim 3, wherein: Generating multiple candidate SQL query solutions corresponding to the data query request based on the large language model includes: generating a database query operation prompt based on the data query request; wherein the database query operation prompt includes a database query operation task description; and The database query operation prompt is input into the supervised fine-tuned large language model, and the supervised fine-tuned large language model is instructed to output the multiple candidate SQL query solutions related to the database query operation task description.
5. The method according to claim 1, wherein: Performing structural analysis on the multiple candidate SQL query solutions respectively includes: Performing structural complexity analysis of the SQL abstract syntax tree on the multiple candidate SQL query solutions respectively to obtain structural complexities corresponding to the multiple candidate SQL query solutions; and / or Performing SQL index usage rationality analysis on the multiple candidate SQL query solutions respectively to obtain index rationality corresponding to the multiple candidate SQL query solutions; and The structural analysis results corresponding to the multiple candidate SQL query solutions are respectively determined based on the structural complexity and / or index rationality corresponding to the multiple candidate SQL query solutions.
6. The method according to claim 5, wherein: Performing structural complexity analysis of the SQL abstract syntax tree on the multiple candidate SQL query solutions respectively includes: using a SQL engineering evaluator to perform structural complexity analysis of the SQL abstract syntax tree on the multiple candidate SQL query solutions respectively, and obtaining structural complexities corresponding to the multiple candidate SQL query solutions respectively.
7. The method according to claim 5, wherein: Performing SQL index usage rationality analysis on the multiple candidate SQL query solutions respectively includes: using a SQL engineering evaluator to perform SQL index usage rationality analysis on the multiple candidate SQL query solutions respectively, and obtaining index rationality corresponding to the multiple candidate SQL query solutions respectively.
8. The method according to claim 1, wherein: Performing performance analysis on the multiple candidate SQL query solutions respectively includes: Do one or any combination of the following: Perform accuracy analysis on the multiple candidate SQL query solutions respectively, and obtain accuracy rates corresponding to the multiple candidate SQL query solutions respectively; Performing execution performance analysis on the multiple candidate SQL query solutions respectively, and obtaining execution performance corresponding to the multiple candidate SQL query solutions respectively; Performing resource consumption analysis on the multiple candidate SQL query solutions respectively, and obtaining resource consumption corresponding to the multiple candidate SQL query solutions respectively; and The performance analysis results corresponding to the multiple candidate SQL query solutions are determined based on one or any combination of accuracy, execution performance, and resource consumption corresponding to the multiple candidate SQL query solutions.
9. The method according to claim 8, wherein: The accuracy analysis includes: performing the following operations for each of the multiple candidate SQL query solutions: Generate an accuracy evaluation prompt based on the data query request and the candidate SQL query solution; and The accuracy evaluation prompt is input into a pre-trained large language model, instructing the pre-trained large language model to evaluate the accuracy of the candidate SQL query solution according to the data query request, and outputting the estimated accuracy of the candidate SQL query solution.
10. The method according to claim 8, wherein: The execution performance analysis includes: performing the following operations for each candidate SQL query solution in the plurality of candidate SQL query solutions: Generate an execution performance parameter evaluation prompt based on the candidate SQL query solution; wherein the execution performance parameter includes: query time and / or concurrent processing capability; and The execution performance parameter evaluation prompt is input into a pre-trained large language model, the pre-trained large language model is instructed to evaluate the execution performance of the candidate SQL query solution, and the execution performance evaluation result of the candidate SQL query solution is output.
11. The method according to claim 8, wherein: The resource consumption analysis includes: using a SQL engineering evaluator to perform CPU usage and / or memory usage analysis on the multiple candidate SQL query solutions respectively, and obtaining resource consumption corresponding to the multiple candidate SQL query solutions respectively.
12. The method according to claim 1, further comprising: Pre-execute the multiple candidate SQL query solutions respectively; as well as Discard candidate SQL query plans that fail to execute.
13. The method according to claim 1, further comprising: Determine a candidate SQL query solution that needs to be pre-executed based on the structural analysis results of the multiple candidate SQL query solutions; Pre-execute the candidate SQL query solutions that need to be pre-executed respectively; Discard the candidate SQL query plans that failed to execute; as well as Add additional selection weight to the candidate SQL query plan that is successfully executed.
14. The method according to claim 1, further comprising: Before executing the target SQL query solution, pre-execute the target SQL query solution; In response to determining that the execution fails, discarding the target SQL query solution, and returning to the step of determining the target SQL query solution from the plurality of candidate SQL query solutions based on the structure analysis result and the performance analysis result; as well as In response to determining that the execution is successful, the target SQL query plan is executed.
15. A data query device, comprising: An input module, used to receive data query requests input by users; A solution generation module, used to generate a plurality of candidate structured query language SQL query solutions corresponding to the data query request based on the large language model; A structural analysis module, used to perform structural analysis on the multiple candidate SQL query solutions respectively to obtain structural analysis results corresponding to the multiple candidate SQL query solutions; A performance analysis module, used to perform performance analysis on the multiple candidate SQL query solutions respectively, and obtain performance analysis results corresponding to the multiple candidate SQL query solutions; A selection module, configured to determine a target SQL query solution from the plurality of candidate SQL query solutions based on the structure analysis result and the performance analysis result; as well as The execution module is used to execute the target SQL query solution, obtain and return the query result corresponding to the data query request.
16. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the data query method according to any one of claims 1 to 14 is implemented.
17. A non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable a computer to execute the data query method according to any one of claims 1 to 14.
18. A computer program product, comprising computer program instructions, which, when executed on a computer, enable the computer to execute the data query method according to any one of claims 1 to 14.