SQL (Structured Query Language) statement analysis optimization method and system based on large language model
By analyzing the metadata and execution plan of SQL statements and optimizing and rewriting them with the large language model, the problem of insufficient flexibility of the existing SQL statement optimization scheme is solved, and more efficient optimization results are achieved.
Patent Information
- Application Number
- CN202510856968.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2045-06-25
AI Technical Summary
The existing SQL statement optimization scheme lacks flexibility and is difficult to break through the limitations of the original SQL statement, resulting in poor optimization results.
By parsing and extracting the metadata information of the table and the execution plan information of the SQL statement, using a large language model for optimization and rewriting, combining semantic structure analysis and execution plan rules conflict analysis, the semantic structure and execution plan of the SQL statement are optimized.
It improves the business performance, demand carrying capacity and rationality of database server resource utilization of SQL statements, and lowers the threshold for optimization operations.
Smart Images

Figure CN120353822A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a SQL statement analysis and optimization method and system based on a large language model. Background Art
[0002] SQL (Structured Query Language) is a database language with multiple functions such as data manipulation and data definition. Faced with the massive data stored in various databases, prepared SQL statements are generally used to query and process the data. In other words, whether the SQL statements are written appropriately will greatly affect the efficiency of data query processing, such as long query execution time, unnecessary data transmission, inconsistent behavior of SQL statements in different database systems, etc.
[0003] At present, there is no shortage of solutions for analyzing and optimizing SQL statements in the prior art. For example, Patent 1 with application number 202210106821.8 discloses a SQL statement optimization method, which obtains the nodes to be optimized of the SQL statement through analysis; queries a pre-configured general configuration rule set according to the nodes to be optimized; and optimizes the SQL statement based on the general configuration rule set; Patent 2 with application number 201611109489.1 discloses a SQL optimization method, which extracts the basic information of at least two SQL statements, the corresponding relationship between the table corresponding to each SQL statement and the columns of the table, and then deletes useless tables and useless columns in the SQL statement to obtain an optimized SQL statement; Patent 3 with application number 201710772704.4 discloses a SQL optimization method, which extracts the filtering conditions of each query block to be optimized in the SQL statement through analysis, and then optimizes the corresponding query block to be optimized to obtain each optimized query block in the SQL statement.
[0004] It can be seen that the above-mentioned Patent 1 optimizes SQL statements based on a preset configuration rule set, and the optimization rules are fixed and lack flexibility; while the above-mentioned Patents 2 and 3 only analyze the original SQL statements themselves for deletion and optimization, and it is difficult to break through the limitations of the original SQL statements for optimization.
[0005] Currently, no effective solution has been proposed for the problem of how to improve the optimization effect of existing SQL statement optimization solutions in related technologies. Summary of the invention
[0006] The embodiments of the present application provide a method and system for analyzing and optimizing SQL statements based on a large language model, so as to at least solve the problem of how to improve the optimization effect of existing SQL statement optimization solutions in the related art.
[0007] In a first aspect, an embodiment of the present application provides a method for analyzing and optimizing SQL statements based on a large language model, the method comprising: Parsing and extracting the SQL statement to be optimized to obtain metadata information of the involved tables; Parsing and extracting the execution plan of the SQL statement to obtain execution plan information, wherein the execution plan is generated by a preset database management system based on the SQL statement; Inputting the metadata information of the tables and the execution plan information into a large language model, and optimizing and rewriting the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting.
[0008] In some embodiments, optimizing and rewriting the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting includes: Based on the metadata information of the tables, performing a first optimization and rewriting of the SQL statement through the large language model; based on the execution plan information, performing a second optimization and rewriting of the SQL statement through the large language model; Based on the first optimization and rewriting and the second optimization and rewriting, obtaining the target SQL statement after optimization and rewriting.
[0009] In some embodiments, based on the metadata information of the tables, performing a first optimization and rewriting of the SQL statement through the large language model includes: Based on the metadata information of the tables, performing semantic structure analysis on the SQL statement through the large language model to perform an efficient first optimization and rewriting of the SQL statement.
[0010] In some embodiments, performing an efficient first optimization and rewriting of the SQL statement includes: If the semantic structure of the SQL statement is logically complex, performing a first optimization and rewriting of the SQL statement through the large language model so that the semantic structure of the SQL statement is optimized; If the semantic structure of the SQL statement lacks table index information, performing a first optimization and rewriting of the SQL statement through the large language model so that the SQL statement includes an index creation sub-statement.
[0011] In some embodiments, based on the execution plan information, performing a second optimization and rewriting of the SQL statement through the large language model includes: Based on the execution plan information, performing rule conflict analysis on the table query of the execution plan through the large language model to perform a reasonably configured second optimization and rewriting of the SQL statement.
[0012] In some of these embodiments, the second optimized rewrite of the SQL statement with reasonable configuration includes: If the table query in the execution plan does not hit the index, the SQL statement is second-optimized and rewritten through a large language model so that the execution plan generated based on the SQL statement can hit the index; If the table query in the execution plan does not consider the configuration parameters of the preset database management system, the SQL statement is second-optimized and rewritten through a large language model so that the execution plan generated based on the SQL statement matches the preset database management system.
[0013] In some of these embodiments, obtaining the target SQL statement after optimized rewrite based on the first optimized rewrite and the second optimized rewrite includes: Based on the first optimized rewrite and the second optimized rewrite, obtain the target SQL statement after only the first optimized rewrite, the target SQL statement after only the second optimized rewrite, and the target SQL statement after combining the first optimized rewrite and the second optimized rewrite.
[0014] In some of these embodiments, after obtaining the target SQL statement after optimized rewrite based on the first optimized rewrite and the second optimized rewrite, the method includes: Analyze the performance metrics of each target SQL statement through a large language model, determine the target SQL statement with the highest performance metric, and preferentially select the optimization rewrite method of the target SQL statement in subsequent optimization rewrites.
[0015] In some of these embodiments, the preset database management system is a database management system in an electronic game scenario. The database management system generates several candidate plans based on the SQL statement to be optimized, estimates the cost of each candidate plan through a cost model, and selects the candidate plan with the lowest cost as the execution plan.
[0016] In a second aspect, an embodiment of the present application provides an SQL statement analysis and optimization system based on a large language model. The system is used for the method described in the first aspect above. The system includes an analysis and extraction module and an optimization and rewrite module; The analysis and extraction module is used to analyze and extract the SQL statement to be optimized to obtain the metadata information of the involved tables; analyze and extract the execution plan generated based on the SQL statement to obtain the execution plan information; The optimization and rewrite module is used to input the metadata information of the tables and the execution plan information into the large language model, and the large language model performs the first optimized rewrite, the second optimized rewrite, and the third optimized rewrite on the SQL statement, and outputs the target SQL statement after optimized rewrite.
[0017] Compared with the related art, an SQL statement analysis and optimization method and system based on a large language model provided by an embodiment of the present application parse and extract the SQL statement to be optimized to obtain the metadata information of the involved tables; parse and extract the execution plan of the SQL statement to obtain the execution plan information, where the execution plan is generated by a preset database management system based on the SQL statement; input the metadata information of the tables and the execution plan information into the large language model, and rewrite and optimize the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting. This realizes the optimization and rewriting from two dimensions of the SQL statement to be optimized itself and the corresponding execution plan, can effectively optimize the business performance of the statement, the requirement bearing capacity, and the rationality of the utilization of database server resources. At the same time, the addition of the large language model further reduces the operation threshold for optimizing and rewriting SQL statements, and solves the problem of how to improve the optimization effect of the existing SQL statement optimization scheme. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation of the present application. In the drawings: Figure 1 is a flowchart of the steps of the SQL statement analysis and optimization method based on a large language model according to an embodiment of the present application; Figure 2 is an internal structural schematic diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0019] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application will be described and explained below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application. Based on the embodiments provided by the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the scope of protection of the present application.
[0020] Obviously, the drawings in the following description are only some examples or embodiments of the present application. For those of ordinary skill in the art, without creative efforts, the present application can also be applied to other similar scenarios based on these drawings. In addition, it can also be understood that although the efforts made in this development process may be complex and lengthy, for those of ordinary skill in the art related to the content disclosed in the present application, some designs, manufacturing or production changes made based on the technical content disclosed in the present application are only conventional technical means and should not be understood as the content disclosed in the present application being insufficient.
[0021] As used in this application, the mention of "embodiment" means that the specific features, structures, or characteristics described in connection with the embodiment may be included in at least one embodiment of this application. The phrase appears at various positions in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment mutually exclusive with other embodiments. Those of ordinary skill in the art will explicitly and implicitly understand that the embodiments described in this application may be combined with other embodiments without conflict.
[0022] Unless otherwise defined, the technical terms or scientific terms involved in this application should have the ordinary meaning understood by those with ordinary skills in the technical field to which this application belongs. The words such as "a", "an", "one kind", "the" and the like involved in this application do not indicate a quantity limitation and may represent a singular or plural number. The terms "include", "comprise", "have" and any variations thereof involved in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may further include unlisted steps or units, or may further include other steps or units inherent to these processes, methods, products or devices. The terms "connected", "coupled" and the like involved in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The "multiple" involved in this application means two or more. "And / or" describes the association relationship of associated objects and indicates that three relationships may exist. For example, "A and / or B" may represent: A exists alone, A and B exist simultaneously, and B exists alone. The character " / " generally represents an "or" relationship between the associated objects before and after. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific order for the objects.
[0023] An embodiment of this application provides a method for analyzing and optimizing SQL statements based on a large language model. Figure 1 It is a flowchart of the steps of the method for analyzing and optimizing SQL statements based on a large language model according to an embodiment of this application, as Figure 1 shown, and the method includes the following steps: Step S102: Parse and extract the SQL statement to be optimized to obtain the metadata information of the involved tables. Specifically, in step S102, tools such as SQLGlot, SQL-Metadata, and SQLParse are used to parse and extract the metadata information of the tables involved in the SQL statement to be optimized. The metadata information includes the index information of the table, the primary key information of the table, the type of the table (such as whether it is a partitioned table), the total number of records in the table, the data types of the fields of the table, and so on.
[0024] It should be noted that the SQL statement to be optimized can be directly written by developers, or it can be generated by converting natural language to SQL using an LLM. For example, the existing patent with application number 202411139469.3 discloses a method for generating SQL statements based on a large language model.
[0025] Step S104, parse and extract the execution plan of the SQL statement to obtain execution plan information, where the execution plan is generated by a preset database management system based on the SQL statement; Specifically in step S104, use the EXPLAIN command to directly parse and extract the execution plan of the SQL statement to obtain execution plan information, and this execution plan information is specifically the execution information of table queries (whether the table query hits the index, whether the table query has filtering, the number of rows returned by the table query, etc.).
[0026] Preferably in step S104, SQL statements have extensive applications in game development and operation. For example, use SQL statements to manage player account information (login name, password, level, experience value), game progress, virtual items (equipment, props), etc. The preset database management system in step S104 is preferably a database management system in an electronic game scenario. This database management system generates several candidate plans based on the SQL statement to be optimized, and estimates the cost of each candidate plan through a cost model, and selects the candidate plan with the lowest cost as the execution plan.
[0027] It should be noted that the execution plan is generated by the database management system based on the SQL statement. If the execution plan has not been generated yet, it is necessary to obtain it from the corresponding database management system, and this database management system can be selected from database products such as PostgreSQL, MySQL, Hive, Presto, Selectdb, etc.
[0028] Step S106, input the metadata information of the table and the execution plan information into the large language model, and optimize and rewrite the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting.
[0029] Step S106 specifically includes the following steps: Step S1061, based on the metadata information of the table, perform the first optimization and rewriting of the SQL statement through the large language model; Specifically in step S1061, based on the metadata information of the table, perform semantic structure analysis on the SQL statement through the large language model to perform efficient first optimization and rewriting of the SQL statement.
[0030] It should be noted that the large language model (LLM) will analyze the semantic structure of the SQL statement based on the metadata information of the tables involved in the SQL statement to be optimized, and recommend a more efficient equivalent writing method, which can effectively improve the execution efficiency of the SQL statement after optimization and rewriting.
[0031] The preferred way to perform an efficient first optimization and rewriting on the SQL statement in step S1061 is: ① If the semantic structure of the SQL statement is logically complex, the large language model performs the first optimization and rewriting on the SQL statement to optimize the semantic structure of the SQL statement; It should be noted that by analyzing the semantic structure of the SQL statement to be optimized, the large language model can identify the logical complexity of the SQL statement (such as complex subqueries, nested queries, inefficient JOIN logic, etc.), and then optimize and rewrite it into a more concise and efficient statement. For example, the large language model optimizes the use of operators such as SELECT, WHERE, and IN by analyzing the semantic structure of the SQL statement to be optimized. That is, if the SQL statement to be optimized is: SELECT * FROM game_logs WHERE player_id IN (SELECT id FROM playersWHERE level > 10); Then the SQL statement after the large language model performs the first optimization and rewriting can be: SELECT g.* FROM game_logs g JOIN players p ON g.player_id = p.id WHERE p.level > 10; ② If the semantic structure of the SQL statement lacks index information of the table, the large language model performs the first optimization and rewriting on the SQL statement to make the SQL statement contain an index creation sub-statement.
[0032] It should be noted that by analyzing the semantic structure of the SQL statement to be optimized, the large language model can identify the index information of the tables involved in the SQL statement. For tables that have no index and require frequent queries, optimization and rewriting are performed to add index creation. For example, if the table players involved in the SQL statement requires frequent queries, the SQL statement to be optimized can be: SELECT * FROM players WHERE region = 'Asia' AND level > 20; Furthermore, the large language model performs the first optimization and rewriting to add the creation of a composite index: CREATE INDEX idx_region_level ON players (region, level); Step S1062, based on the execution plan information, rewrite the SQL statement for the second optimization through a large language model; Specifically in Step S1062, based on the execution plan information, analyze the rule conflicts in the table queries of the execution plan through a large language model to perform a second optimization rewrite of the SQL statement with reasonable configuration.
[0033] It should be noted that the large language model (LLM) will further analyze the rule conflicts in the table queries of the execution plan according to the execution plan information (such as index misses, resource overhead overflow, etc.) to reverse-derive the second optimization rewrite of the SQL statement, so that the execution plan generated based on the preset database management system can be reasonably configured.
[0034] The second optimization rewrite of the SQL statement with reasonable configuration in Step S1062 is preferably: ① If the table query of the execution plan does not hit the index, rewrite the SQL statement for the second optimization through a large language model so that the execution plan generated based on the SQL statement can hit the index; It should be noted that the execution plan cannot hit the index usually because the SQL statement does not conform to the rules of the database optimizer in the preset database management system. By analyzing the rule conflicts between the execution plan and the preset database management system, the large language model can identify the index hit situation of the table queries in the execution plan. For the table queries where the index is not hit, optimize and rewrite the corresponding SQL statement so that the execution plan generated based on the SQL statement can hit the index. For example, using a function on the index column will cause the database optimizer to be unable to directly match the index, that is, the SQL statement to be optimized is: SELECT * FROM players WHERE YEAR(created_at) = 2025; Then the SQL statement after the large language model performs the second optimization rewrite can be: SELECT * FROM players WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01'; ② If the table query of the execution plan does not consider the configuration parameters of the preset database management system, rewrite the SQL statement for the second optimization through a large language model so that the execution plan generated based on the SQL statement matches the preset database management system.
[0035] It should be noted that the execution plan causing the resource overhead of the preset database management system to overflow is usually due to the SQL statement not taking into account the configuration parameters of the database (such as cache size, concurrency control strategy). The large language model optimizes and rewrites the corresponding SQL statement by analyzing these configuration parameters (such as innodb_buffer_pool_size), so that the execution plan generated based on the SQL statement matches the preset database management system.
[0036] Step S1063: Obtain the target SQL statement after optimized rewriting based on the first optimized rewrite and the second optimized rewrite.
[0037] Specifically, in step S1063, based on the first optimized rewrite and the second optimized rewrite, obtain the target SQL statement after the individual first optimized rewrite, the target SQL statement after the individual second optimized rewrite, and the target SQL statement after combining the first optimized rewrite and the second optimized rewrite.
[0038] After obtaining the target SQL statement after optimized rewriting based on the first optimized rewrite and the second optimized rewrite in step S1063, the method includes step S107. Analyze the performance metrics of each target SQL statement through the large language model, determine the target SQL statement with the highest performance metrics, and preferentially select the optimized rewrite method of the target SQL statement in subsequent optimized rewrites.
[0039] It should be noted that step S1063 includes three optimized rewrite methods: only using the first optimized rewrite, only using the second optimized rewrite, and using the first optimized rewrite and the second optimized rewrite; and the first optimized rewrite and the second optimized rewrite also include specific optimized rewrite means (such as ①② in step S1061 and ①② in step S1062 above). By analyzing various optimized rewrite methods, obtain the optimized rewrite method with the highest performance metrics to be preferentially used in subsequent optimized rewrites, further improving the efficiency and accuracy of optimizing and rewriting SQL statements by the large language model in the future.
[0040] Through the above steps in the embodiments of the present application, optimization and rewriting are achieved from two dimensions of the SQL statement to be optimized itself and the corresponding execution plan, which can effectively optimize the business performance of the statement, the demand-bearing capacity, and the rationality of the utilization of database server resources. At the same time, the addition of the large language model further reduces the operation threshold of optimizing and rewriting SQL statements, solving the problem of how to improve the optimization effect of the existing SQL statement optimization scheme.
[0041] It should be noted that the steps shown in the above process or the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.
[0042] An embodiment of the present application provides a SQL statement analysis and optimization system based on a large language model. The system is used for the method in the first aspect above. The system includes an analysis and extraction module and an optimization and rewriting module; The analysis and extraction module is used to analyze and extract the SQL statement to be optimized to obtain the metadata information of the involved tables; analyze and extract the execution plan generated based on the SQL statement to obtain the execution plan information; The optimization and rewriting module is used to input the metadata information of the tables and the execution plan information into the large language model, and perform the first optimization and rewriting, the second optimization and rewriting, and the third optimization and rewriting of the SQL statement through the large language model, and output the target SQL statement after optimization and rewriting.
[0043] Through the analysis and extraction module and the optimization and rewriting module in the embodiment of the present application, the optimization and rewriting are realized from two dimensions of the SQL statement to be optimized itself and the corresponding execution plan, which can effectively optimize the business performance of the statement, the demand-bearing capacity, and the rationality of the utilization of database server resources. At the same time, the addition of the large language model further reduces the operation threshold for optimizing and rewriting SQL statements, and solves the problem of how to improve the optimization effect of the existing SQL statement optimization scheme.
[0044] It should be noted that the above-mentioned modules can be functional modules or program modules, and can be implemented either by software or by hardware. For the modules implemented by hardware, the above-mentioned modules can be located in the same processor; or the above-mentioned modules can also be located in different processors in any combination form.
[0045] This embodiment provides an electronic device, including a memory and a processor. A computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0046] Optionally, the above-mentioned electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the above-mentioned processor, and the input / output device is connected to the above-mentioned processor.
[0047] Optionally, the electronic device may further include a processor, a memory, a network interface, a display screen, and an input device connected via a system bus. Among them, the processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The network interface of the electronic device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, it implements a method for analyzing and optimizing SQL statements based on a large language model. The display screen of the electronic device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the electronic device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad provided on the housing of the electronic device, or an external keyboard, touchpad, or mouse, etc.
[0048] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and alternative embodiments, and will not be elaborated here.
[0049] In addition, in combination with the method for analyzing and optimizing SQL statements based on a large language model in the above embodiments, an embodiment of the present application can provide a storage medium to implement. A computer program is stored on the storage medium; when the computer program is executed by the processor, it implements any one of the methods for analyzing and optimizing SQL statements based on a large language model in the above embodiments.
[0050] In one embodiment, Figure 2 is a schematic internal structure diagram of an electronic device according to an embodiment of the present application, as Figure 2 shown, a kind of electronic device is provided. The electronic device can be a server, and its internal structure diagram can be as Figure 2 shown. The electronic device includes a processor, a network interface, an internal memory, and a non-volatile memory connected via an internal bus. Among them, the non-volatile memory stores an operating system, computer programs, and a database. The processor is used to provide computing and control capabilities, the network interface is used to communicate with an external terminal via a network connection, the internal memory is used to provide an environment for the operation of the operating system and computer programs, the computer program is executed by the processor to implement a method for analyzing and optimizing SQL statements based on a large language model, and the database is used to store data.
[0051] Those skilled in the art can understand that Figure 2 the structure shown in
[0052] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. This computer program can be stored in a non-volatile computer-readable storage medium. When this computer program is executed, it can include the processes of the embodiments of the above various methods. Among them, any reference to a memory, storage, database, or other medium used in the various embodiments provided in this application can include non-volatile and / or volatile memories. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM), etc.
[0053] Those skilled in the art should understand that the technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.
[0054] The above embodiments only represent several implementation manners of this application. The description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the invention patent. It should be noted that for those of ordinary skill in the art, without departing from the concept of this application, several modifications and improvements can still be made, and these all belong to the protection scope of this application. Therefore, the protection scope of the patent of this application should be subject to the appended claims.
Claims
1. A method for analyzing and optimizing SQL statements based on large language models, characterized in that, The method includes: Parsing and extracting the SQL statement to be optimized to obtain the metadata information of the involved tables; Parsing and extracting the execution plan of the SQL statement to obtain the execution plan information, where the execution plan is generated by a preset database management system based on the SQL statement; Inputting the metadata information of the tables and the execution plan information into a large language model, and optimizing and rewriting the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting.
2. The method according to claim 1, characterized in that, Optimizing and rewriting the SQL statement through the large language model to obtain the target SQL statement after optimization and rewriting includes: Based on the metadata information of the tables, performing a first optimization and rewriting of the SQL statement through the large language model; based on the execution plan information, performing a second optimization and rewriting of the SQL statement through the large language model; Based on the first optimization and rewriting and the second optimization and rewriting, obtaining the target SQL statement after optimization and rewriting.
3. The method according to claim 2, wherein Based on the metadata information of the tables, performing a first optimization and rewriting of the SQL statement through the large language model includes: Based on the metadata information of the tables, performing semantic structure analysis on the SQL statement through the large language model to perform an efficient first optimization and rewriting of the SQL statement.
4. The method according to claim 3, wherein Performing an efficient first optimization and rewriting of the SQL statement includes: If the semantic structure of the SQL statement is logically complex, performing a first optimization and rewriting of the SQL statement through the large language model to optimize the semantic structure of the SQL statement; If the semantic structure of the SQL statement lacks table index information, performing a first optimization and rewriting of the SQL statement through the large language model to make the SQL statement include an index creation sub-statement.
5. The method according to claim 2, wherein Based on the execution plan information, performing a second optimization and rewriting of the SQL statement through the large language model includes: Based on the execution plan information, performing rule conflict analysis on the table query of the execution plan through the large language model to perform a second optimization and rewriting with reasonable configuration of the SQL statement.
6. The method according to claim 5, wherein Performing a second optimization and rewriting with reasonable configuration of the SQL statement includes: If the table query of the execution plan does not hit the index, performing a second optimization and rewriting of the SQL statement through the large language model to make the execution plan generated based on the SQL statement hit the index; If the table query of the execution plan does not consider the configuration parameters of the preset database management system, performing a second optimization and rewriting of the SQL statement through the large language model to make the execution plan generated based on the SQL statement match the preset database management system.
7. The method according to claim 2, characterized in that, Based on the first optimization and rewriting and the second optimization and rewriting, obtaining the target SQL statement after optimization and rewriting includes: Based on the first optimization and rewriting and the second optimization and rewriting, obtaining the target SQL statement after only the first optimization and rewriting, the target SQL statement after only the second optimization and rewriting, and the target SQL statement after combining the first optimization and rewriting and the second optimization and rewriting.
8. The method according to claim 7, wherein After obtaining the target SQL statement after the first optimized rewrite and the second optimized rewrite, the method includes: Analyze the performance metrics of each target SQL statement through a large language model, determine the target SQL statement with the highest performance metrics, and preferentially select the optimization rewrite method of the target SQL statement in subsequent optimization rewrites.
9. The method according to claim 1, wherein The preset database management system is a database management system in an electronic game scenario. The database management system generates several candidate plans based on the SQL statement to be optimized, estimates the cost of each candidate plan through a cost model, and selects the candidate plan with the lowest cost as the execution plan.
10. A SQL statement analysis and optimization system based on a large language model, characterized in that, The system is used to execute the method according to any one of claims 1 to 9. The system includes an analysis and extraction module and an optimization and rewrite module; The analysis and extraction module is used to analyze and extract the SQL statement to be optimized to obtain the metadata information of the involved tables; analyze and extract the execution plan generated based on the SQL statement to obtain the execution plan information; The optimization and rewrite module is used to input the metadata information of the tables and the execution plan information into a large language model, perform the first optimized rewrite, the second optimized rewrite, and the third optimized rewrite on the SQL statement through the large language model, and output the target SQL statement after the optimized rewrite.
Citation Information
Patent Citations
A SQL optimization method and device
CN106611044B
A SQL optimization method and device
CN107704511B
SQL statement optimization method and device
CN116561154B
SQL (Structured Query Language) statement generation method and device based on large language model
CN118656387A
Auxiliary optimization method and device for structured query language
CN115481141A