A sql statement analysis and optimization method and system based on a large language model

By parsing the metadata and execution plan information of SQL statements and optimizing and rewriting them using a large language model, the problem of insufficient flexibility in existing SQL statement optimization schemes is solved, and the optimization effect of SQL statements is improved.

CN120353822BActive Publication Date: 2025-11-07HANGZHOU ELECTRONICS SOUL NETWORK TECH
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202510856968.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-25
Publication Date
2025-11-07
Estimated Expiration
2045-06-25

AI Technical Summary

Technical Problem

Existing SQL statement optimization solutions lack flexibility and are difficult to overcome the limitations of the original SQL statement, resulting in poor optimization effects.

Method used

By parsing the metadata and execution plan information of SQL statements, semantic structure analysis and rule conflict analysis are performed using a large language model to optimize and rewrite SQL statements to improve efficiency.

Benefits of technology

It achieves optimization from two dimensions: the SQL statement itself and the execution plan, improving business performance, demand capacity, and the rationality of database resource utilization, while lowering the threshold for optimization operations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120353822B_ABST
    Figure CN120353822B_ABST
Patent Text Reader

Abstract

The application relates to a SQL statement analysis optimization method and system based on a large language model, wherein the method comprises the following steps: parsing and extracting a SQL statement to be optimized to obtain metadata information of a table involved; parsing and extracting an execution plan generated based on the SQL statement to obtain execution plan information; inputting the metadata information of the table and the execution plan information into the large language model, optimizing and rewriting the SQL statement through the large language model, and obtaining a target SQL statement after optimization and rewriting. Through the application, optimization and rewriting are realized from two dimensions of the SQL statement to be optimized itself and a corresponding execution plan, the business performance, demand bearing capacity and rationality of database server resource utilization of the statement can be effectively optimized, the operation threshold of SQL statement optimization and rewriting is further reduced through the addition of the large language model, and the problem of how to improve the optimization effect of an existing SQL statement optimization scheme is solved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of data processing, and in particular, relates to a SQL statement analysis and optimization method and system based on a large language model. BACKGROUND

[0002] SQL (Structured Query Language) is a database language with functions such as data manipulation and data definition. In the face of massive data stored in various databases, a well-written SQL statement is generally used to query and process data. In other words, whether the SQL statement is properly written greatly affects the efficiency of data query processing, such as excessively long query execution time, unnecessary data transmission, and inconsistent behavior of SQL statements in different database systems.

[0003] At present, there are many schemes for analyzing and optimizing SQL statements in the prior art. For example, Patent One with the application number 202210106821.8 discloses a SQL statement optimization method, which obtains a to-be-optimized node of a SQL statement by analysis; queries a pre-configured general configuration rule set according to the to-be-optimized node; and optimizes the SQL statement based on the general configuration rule set. Patent Two with the application number 201611109489.1 discloses a SQL optimization method, which extracts basic information of at least two SQL statements, a corresponding relationship between a table corresponding to each SQL statement and columns of the table, and then deletes useless tables and columns in the SQL statements to obtain optimized SQL statements. Patent Three with the application number 201710772704.4 discloses a SQL optimization method, which extracts a filtering condition of each to-be-optimized query block in a SQL statement, and then optimizes the corresponding to-be-optimized query block to obtain each optimized query block in the SQL statement.

[0004] It can be seen that Patent One optimizes the SQL statement based on a pre-configured configuration rule set, and the optimization rule is fixed and lacks flexibility. Patent Two and Patent Three only analyze the original SQL statement itself to optimize and reduce, and it is difficult to break through the limitations of the original SQL statement to optimize.

[0005] At present, there is no effective solution to the problem of how to improve the optimization effect of the existing SQL statement optimization scheme in the related art. SUMMARY

[0006] The embodiments of the present application provide a SQL statement analysis and optimization method and system based on a large language model, to at least solve the problem of how to improve the optimization effect of the existing SQL statement optimization scheme in the related art.

[0007] In a first aspect, the embodiments of the present application provide a SQL statement analysis and optimization method based on a large language model, which comprises:

[0008] parsing and extracting the metadata information of the tables involved in the SQL statement to be optimized;

[0009] parsing and extracting the execution plan information of the SQL statement, wherein the execution plan is generated by a preset database management system based on the SQL statement;

[0010] inputting the metadata information of the tables and the execution plan information into a large language model, and optimizing and rewriting the SQL statement by the large language model to obtain the target SQL statement after optimization and rewriting.

[0011] In some embodiments, the optimization and rewriting of the SQL statement by the large language model to obtain the target SQL statement after optimization and rewriting comprises:

[0012] based on the metadata information of the tables, first optimization and rewriting of the SQL statement by the large language model; and based on the execution plan information, second optimization and rewriting of the SQL statement by the large language model;

[0013] based on the first optimization and rewriting and the second optimization and rewriting, obtaining the target SQL statement after optimization and rewriting.

[0014] In some embodiments, the first optimization and rewriting of the SQL statement by the large language model based on the metadata information of the tables comprises:

[0015] based on the metadata information of the tables, semantic structure analysis of the SQL statement by the large language model for efficient first optimization and rewriting of the SQL statement.

[0016] In some embodiments, the efficient first optimization and rewriting of the SQL statement comprises:

[0017] if the semantic structure of the SQL statement is logically complex, the first optimization and rewriting of the SQL statement by the large language model to optimize the semantic structure of the SQL statement;

[0018] if the semantic structure of the SQL statement lacks index information of the tables, the first optimization and rewriting of the SQL statement by the large language model to make the SQL statement contain an index creation sub-statement.

[0019] In some embodiments, the second optimization and rewriting of the SQL statement by the large language model based on the execution plan information comprises:

[0020] Based on the execution plan information, the table query of the execution plan is analyzed for rule conflict by a large language model to perform a second optimization rewriting of the SQL statement.

[0021] In some embodiments, the second optimization rewriting of the SQL statement includes:

[0022] If the table query of the execution plan does not hit the index, the SQL statement is rewritten by a large language model to enable the execution plan generated based on the SQL statement to hit the index.

[0023] If the table query of the execution plan does not consider the configuration parameters of the preset database management system, the SQL statement is rewritten by a large language model to enable the execution plan generated based on the SQL statement to cooperate with the preset database management system.

[0024] In some embodiments, based on the first optimization rewriting and the second optimization rewriting, the target SQL statement after optimization rewriting includes:

[0025] Based on the first optimization rewriting and the second optimization rewriting, the target SQL statement after the first optimization rewriting alone, the target SQL statement after the second optimization rewriting alone, and the target SQL statement after the combination of the first optimization rewriting and the second optimization rewriting are obtained.

[0026] In some embodiments, after obtaining the target SQL statement after optimization rewriting based on the first optimization rewriting and the second optimization rewriting, the method includes:

[0027] The performance indicators of each target SQL statement are analyzed by a large language model to determine the target SQL statement with the highest performance indicator, and the optimization rewriting mode of the target SQL statement is preferentially selected in subsequent optimization rewriting.

[0028] In some embodiments, the preset database management system is a database management system in an electronic game scenario, and the database management system generates a plurality of candidate plans based on the SQL statement to be optimized, estimates the overhead of each candidate plan by a cost model, and selects the candidate plan with the lowest cost as the execution plan.

[0029] In a second aspect, the embodiments of the present application provide a SQL statement analysis and optimization system based on a large language model, which is used for the method of the first aspect and includes an analysis and extraction module and an optimization rewriting module.

[0030] The analysis extraction module is configured to analyze and extract a SQL statement to be optimized to obtain metadata information of a table involved; and analyze and extract an execution plan generated based on the SQL statement to obtain execution plan information.

[0031] The optimization rewriting module is configured to input the metadata information of the table and the execution plan information into a large language model, perform first, second and third optimization rewrites on the SQL statement by the large language model, and output a target SQL statement after optimization rewriting.

[0032] Compared with the related art, the SQL statement analysis optimization method and system based on a large language model provided by the embodiments of the present application can analyze and extract a SQL statement to be optimized to obtain metadata information of a table involved; analyze and extract an 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; input the metadata information of the table and the execution plan information into a large language model, and perform optimization rewriting on the SQL statement by the large language model to obtain a target SQL statement after optimization rewriting, which realizes optimization rewriting from two dimensions of the SQL statement to be optimized itself and the corresponding execution plan, can effectively optimize the business performance, demand carrying capacity and rationality of database server resource utilization of the statement, and further reduces the operation threshold of SQL statement optimization rewriting by the addition of the large language model, thereby solving the problem of how to improve the optimization effect of an existing SQL statement optimization scheme. BRIEF DESCRIPTION OF DRAWINGS

[0033] The accompanying drawings, which are included to provide a further understanding of the present application, constitute a part of this application and help to explain the present application, but do not limit the present application in any way. In the drawings:

[0034] Figure 1 is a step flowchart of the SQL statement analysis optimization method based on a large language model according to an embodiment of the present application;

[0035] Figure 2 is a schematic diagram of the internal structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0036] In order to make the objectives, technical solutions and advantages of the present application clearer, the present application is described and explained in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and do not limit the present application in any way. Based on the embodiments provided in the present application, all other embodiments obtained by those of ordinary skill in the art without creative labor fall within the scope of the present application.

[0037] It is apparent that the drawings in the following description merely show some examples or embodiments of the present application, and the present application can be applied to other similar situations without creative efforts by those skilled in the art based on these drawings. In addition, it can be understood that, although the efforts made in this development process can be complex and lengthy, some design, manufacture or production changes based on the technical content disclosed in the present application are only routine technical means for those skilled in the art related to the content disclosed in the present application, and should not be understood as insufficient disclosure of the content disclosed in the present application.

[0038] Reference to "an embodiment" in this application means that a particular feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the application. The appearance of the phrase in various places in the specification does not necessarily all refer to the same embodiment, nor is it necessarily mutually exclusive of other embodiments. It is explicitly and implicitly understood that the embodiments described in the application are not mutually exclusive, and can be combined with each other.

[0039] Unless otherwise defined, technical terms or scientific terms used in the present application should be understood as their common meanings to those skilled in the art. The terms "a", "an", "one", "the", and similar words in the present application do not represent quantity restrictions, but can represent singular or plural. The terms "include", "contain", "have", and any variations thereof in the present application are intended to cover non-exclusive inclusion; for example, a process, method, system, product or device including a series of steps or modules (units) is not limited to the listed steps or units, but can also include steps or units not listed, or can also include other steps or units inherent to the process, method, product or device. The terms "connected", "connected", "coupled" and similar words in the present application are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. The term "multiple" in the present application means two or more. The association between the associated objects is described by the term "and / or", which means that there can be three relationships, for example, "A and / or B" can mean that A exists alone, A and B exist together, and B exists alone. The character " / " generally represents an "or" relationship between the associated objects. The terms "first", "second", "third" and the like in the present application are merely to distinguish similar objects, and do not represent a specific order for the objects.

[0040] The embodiment of the present application provides a SQL statement analysis and optimization method based on a large language model, Figure 1is a step flowchart of a SQL statement analysis and optimization method based on a large language model according to an embodiment of the present application, as shown in Figure 1 The method comprises the following steps:

[0041] In step S102, the SQL statement to be optimized is parsed and extracted to obtain metadata information of the tables involved.

[0042] In step S102, SQLGlot, SQL-Metadata, SQLParse and other tools are used to parse and extract the metadata information of the tables involved in the SQL statement to be optimized, including index information of the tables, primary key information of the tables, types of the tables (such as whether they are partitioned tables), total number of records of the tables, data types of the fields of the tables, etc.

[0043] It should be noted that the SQL statement to be optimized can be directly written by the developer, or the SQL statement to be optimized can be generated by using LLM to convert natural language to SQL, such as the existing patent with application number 202411139469.3, which discloses a SQL statement generation method based on a large language model.

[0044] In step S104, the execution plan of the SQL statement is parsed and extracted to obtain execution plan information, wherein the execution plan is generated by a preset database management system based on the SQL statement.

[0045] In step S104, the execution plan of the SQL statement is parsed and extracted to obtain execution plan information, wherein the execution plan is generated by a preset database management system based on the SQL statement.

[0046] In step S104, the SQL statement is widely used in game development and operation, such as using SQL statement to manage player's 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 the electronic game scenario, which generates a plurality of 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.

[0047] 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, it needs to be obtained from the corresponding database management system, which can be selected from PostgreSQL, MySQL, Hive, Presto, Selectdb and other database products.

[0048] Step S106, input the metadata information and execution plan information of the table into the large language model, optimize and rewrite the SQL statement through the large language model, and obtain the target SQL statement after optimization and rewriting.

[0049] Step S106 specifically includes the following steps:

[0050] Step S1061, based on the metadata information of the table, the SQL statement is first optimized and rewritten through the large language model;

[0051] Step S1061 specifically, based on the metadata information of the table, the SQL statement is analyzed by the large language model to perform semantic structure analysis, so as to perform efficient first optimization and rewriting of the SQL statement.

[0052] It should be noted that the large language model (LLM) can analyze the semantic structure of the SQL statement based on the metadata information of the table involved in the SQL statement to be optimized, recommend a more efficient equivalent writing, and effectively improve the execution efficiency of the SQL statement after optimization and rewriting.

[0053] The first optimization and rewriting of the SQL statement in step S1061 is preferably:

[0054] ① If the semantic structure of the SQL statement is logically complex, the first optimization and rewriting of the SQL statement is performed through the large language model, so that the semantic structure of the SQL statement is optimized;

[0055] It should be noted that the large language model can identify the logical complexity of the SQL statement (such as complex subqueries, nested queries, inefficient JOIN logic, etc.) by analyzing the semantic structure of the SQL statement to be optimized, and then optimizes and rewrites it to a more concise and efficient statement. For example, the large language model optimizes the use of SELECT, WHERE, IN, etc. operators by analyzing the semantic structure of the SQL statement to be optimized, i.e., if the SQL statement to be optimized is:

[0056] SELECT * FROM game_logs WHERE player_id IN (SELECT id FROM playersWHERE level > 10);

[0057] The SQL statement after the first optimization and rewriting performed by the large language model can be:

[0058] SELECT g.*

[0059] FROM game_logs g

[0060] JOIN players p ON g.player_id = p.id

[0061] WHERE p.level > 10;

[0062] If the semantic structure of the SQL statement lacks index information of the table, the large language model is used to perform first optimization rewriting on the SQL statement, so that the SQL statement contains an index creation sub-statement.

[0063] It should be noted that the large language model can identify the index information of the table involved in the SQL statement by analyzing the semantic structure of the SQL statement to be optimized. For tables without indexes and requiring high-frequency queries, the optimization rewriting is performed to increase the creation of indexes. For example, the table players involved in the SQL statement requires high-frequency queries, and the SQL statement to be optimized can be:

[0064] SELECT * FROM players WHERE region = 'Asia' AND level > 20;

[0065] Further, the large language model performs first optimization rewriting to increase the creation of a combined index:

[0066] CREATE INDEX idx_region_level ON players (region, level);

[0067] In step S1062, the SQL statement is second-optimized and rewritten by the large language model based on the execution plan information.

[0068] In step S1062, the large language model performs rule conflict analysis on the table query of the execution plan based on the execution plan information to perform second optimization rewriting of the SQL statement for reasonable configuration.

[0069] It should be noted that the large language model (LLM) further analyzes the rule conflicts (such as index miss, resource overflow, etc.) of the table query of the execution plan according to the execution plan information to reversely deduce the second optimization rewriting of the SQL statement, so that the execution plan generated based on the preset database management system can be reasonably configured.

[0070] The second optimization rewriting of the SQL statement for reasonable configuration in step S1062 is preferably:

[0071] If the table query of the execution plan does not hit the index, the large language model is used to perform second optimization rewriting on the SQL statement, so that the execution plan generated based on the SQL statement can hit the index.

[0072] It should be noted that the execution plan cannot hit the index is usually due to the SQL statement does not meet the rules of the database optimizer in the preset database management system. The large language model can identify the index hit condition of the table query of the execution plan by analyzing the rule conflict between the execution plan and the preset database management system, and optimize and rewrite the corresponding SQL statement for the table query without index hit, 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:

[0073] SELECT * FROM players WHERE YEAR(created_at) = 2025;

[0074] The second optimization and rewriting SQL statement executed by the large language model can be:

[0075] SELECT * FROM players

[0076] WHERE created_at >= '2025-01-01' AND created_at < '2026-01-01';

[0077] If the table query of the execution plan does not consider the configuration parameters of the preset database management system, the large language model can perform second optimization and rewriting on the SQL statement, so that the execution plan generated based on the SQL statement is compatible with the preset database management system.

[0078] It should be noted that the execution plan causes the resource overhead of the preset database management system to overflow, which is usually due to the SQL statement not considering the configuration parameters (such as cache size, concurrency control strategy) of the database. The large language model can optimize and rewrite 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 is compatible with the preset database management system.

[0079] In step S1063, the target SQL statement after optimization and rewriting is obtained based on the first optimization and rewriting and the second optimization and rewriting.

[0080] In step S1063, the target SQL statement after optimization and rewriting is obtained based on the first optimization and rewriting and the second optimization and rewriting.

[0081] After obtaining the target SQL statement after optimization rewriting based on the first optimization rewriting and the second optimization rewriting in step S1063, the method comprises step S107,

[0082] By analyzing the performance indicators of each target SQL statement through the large language model, the target SQL statement with the highest performance indicator is determined, and the optimization rewriting mode of the target SQL statement is preferentially selected in subsequent optimization rewriting.

[0083] It should be noted that the step S1063 includes three optimization rewriting modes: using only the first optimization rewriting, using only the second optimization rewriting, and using the first optimization rewriting and the second optimization rewriting; and the first optimization rewriting and the second optimization rewriting include specific optimization rewriting means (such as the above steps S1061 ①②, steps S1062 ①②). By analyzing various optimization rewriting modes, the optimization rewriting mode with the highest performance indicator is obtained to be preferentially used in subsequent optimization rewriting, further improving the efficiency and accuracy of subsequent optimization rewriting of SQL statements through the large language model.

[0084] Through the above steps in the embodiments of the present application, optimization rewriting is realized from two dimensions of the to-be-optimized SQL statement itself and the corresponding execution plan, which can effectively optimize the business performance, demand carrying capacity and rationality of database server resource utilization of the statement. At the same time, the addition of the large language model further reduces the operation threshold of SQL statement optimization rewriting, solving the problem of how to improve the optimization effect of the existing SQL statement optimization scheme.

[0085] It should be noted that the steps shown in the above flow or the flowchart of the accompanying drawings can be executed in a computer system such as a group 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 an order different from that here.

[0086] The embodiments of the present application provide a SQL statement analysis optimization system based on a large language model, which is used for the method of the first aspect, and the system comprises an analysis extraction module and an optimization rewriting module;

[0087] The analysis extraction module is used for analyzing and extracting the to-be-optimized SQL statement to obtain the metadata information of the involved table; and analyzing and extracting the execution plan generated based on the SQL statement to obtain the execution plan information.

[0088] The optimization rewriting module is used for inputting the metadata information of the table and the execution plan information into the large language model, performing first optimization rewriting, second optimization rewriting and third optimization rewriting on the SQL statement through the large language model, and outputting the target SQL statement after optimization rewriting.

[0089] Through the analysis extraction module and the optimization rewriting module in the embodiments of the present application, optimization rewriting is realized from two dimensions of the to-be-optimized SQL statement itself and the corresponding execution plan, which can effectively optimize the business performance, demand carrying capacity and rationality of database server resource utilization of the statement, and the addition of the large language model further reduces the operation threshold of SQL statement optimization rewriting, solving the problem of how to improve the optimization effect of the existing SQL statement optimization scheme.

[0090] It should be noted that each of the above modules can be a functional module or a program module, which can be implemented by software or hardware. For the modules implemented by hardware, each of the above modules can be located in the same processor; or each of the above modules can also be located in different processors in any combination.

[0091] The embodiment provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any of the method embodiments.

[0092] Optionally, the electronic device can further include a transmission device and an input and output device, wherein the transmission device is connected with the processor, and the input and output device is connected with the processor.

[0093] Optionally, the electronic device can further include a processor, a memory, a network interface, a display screen and an input device connected through a system bus. 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 a computer program. The internal memory provides an environment for the operating system and the computer program in the non-volatile storage medium to run. The network interface of the electronic device is used to communicate with external terminals through network connection. The computer program is executed by the processor to implement a SQL statement analysis optimization method 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. The input device of the electronic device can be a touch layer overlaid on the display screen, or a key, trackball or touchpad arranged on the shell of the electronic device, or an external keyboard, touchpad or mouse, etc.

[0094] It should be noted that the specific examples in the embodiment can refer to the examples described in the above embodiments and optional implementation manners, which will not be described here again.

[0095] In addition, in combination with the SQL statement analysis and optimization method based on the large language model in the above-mentioned embodiments, the embodiments of the present application can provide a storage medium for implementation. The storage medium stores a computer program; the computer program is executed by a processor to implement any one of the SQL statement analysis and optimization methods based on the large language model in the above-mentioned embodiments.

[0096] In one embodiment, Figure 2 is a schematic diagram of the internal structure of an electronic device according to an embodiment of the present application, as Figure 2 indicated, an electronic device, which can be a server, is provided, and the internal structure diagram thereof can be as Figure 2 indicated. The electronic device includes a processor, a network interface, an internal memory and a non-volatile memory connected by an internal bus, wherein the non-volatile memory stores an operating system, a computer program and a database. The processor is used to provide computing and control capability, the network interface is used to communicate with the external terminal through the network connection, the internal memory is used to provide an environment for the operation of the operating system and the computer program, the computer program is executed by the processor to implement a SQL statement analysis and optimization method based on a large language model, and the database is used to store data.

[0097] Those skilled in the art can understand that Figure 2 the structure shown in the above-mentioned embodiments is only a block diagram of part of the structure related to the scheme of the present application, and does not constitute a limitation on the electronic device to which the scheme of the present application is applied. The specific electronic device can include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0098] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer readable storage medium. When the computer program is executed, it can include the processes of the above-mentioned embodiments of each method. Any reference to memory, storage, database or other medium used in each embodiment provided by the present application can include non-volatile and / or volatile memory. 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. As an illustration but 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 (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.

[0099] Those skilled in the art should understand that each technical feature of the above-mentioned embodiments can be combined arbitrarily, and in order to make the description simple, not all possible combinations of each technical feature in the above-mentioned embodiments are described, however, as long as the combination of these technical features does not exist contradictory, it should be considered as the scope of the present application.

[0100] The above-mentioned embodiments only express several implementation manners of the present application, and the description is more specific and detailed, but it should not be understood as a limitation on the scope of the patent. It should be pointed out that for ordinary skilled in the art, without departing from the concept of the present application, a number of modifications and improvements can be made, which are all within the scope of the present application. Therefore, the protection scope of the patent of the present application should be subject to the appended claims.

Claims

1. A method for analyzing and optimizing SQL statements based on a large language model, characterized in that, The method comprises: parsing and extracting a SQL statement to be optimized to obtain metadata information of a table involved; generating a plurality of candidate plans based on the SQL statement to be optimized using a preset database management system, and estimating the overhead of each candidate plan through a cost model, and selecting the candidate plan with the lowest cost as an execution plan, wherein the preset database management system is a database management system in an electronic game scenario; directly parsing and extracting the execution plan of the SQL statement using an EXPLAIN command to obtain execution plan information; inputting the metadata information of the table and the execution plan information into a large language model, performing first optimization rewriting of the SQL statement based on the metadata information of the table through the large language model, performing second optimization rewriting of the SQL statement based on the execution plan information through the large language model, and obtaining a target SQL statement after optimization rewriting based on the first optimization rewriting and the second optimization rewriting.

2. The method of claim 1, wherein, Performing first optimization rewriting of the SQL statement based on the metadata information of the table through the large language model comprises: performing semantic structure analysis of the SQL statement based on the metadata information of the table through the large language model to perform efficient first optimization rewriting of the SQL statement.

3. The method of claim 2, wherein, Efficient first optimization rewriting of the SQL statement comprises: if the semantic structure of the SQL statement is logically complex, performing first optimization 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 index information of a table, performing first optimization rewriting of the SQL statement through the large language model to make the SQL statement contain an index creation sub-statement.

4. The method of claim 1, wherein, Performing second optimization rewriting of the SQL statement based on the execution plan information through the large language model comprises: performing rule conflict analysis of table queries of the execution plan based on the execution plan information through the large language model to perform reasonably configured second optimization rewriting of the SQL statement.

5. The method of claim 4, wherein, Reasonably configured second optimization rewriting of the SQL statement comprises: if the table queries of the execution plan do not hit an index, performing second optimization rewriting of the SQL statement through the large language model to enable the execution plan generated based on the SQL statement to hit the index; if the table queries of the execution plan do not consider configuration parameters of the preset database management system, performing second optimization rewriting of the SQL statement through the large language model to enable the execution plan generated based on the SQL statement to cooperate with the preset database management system.

6. The method of claim 1, wherein, Based on the first optimization rewriting and the second optimization rewriting, obtaining a target SQL statement after optimization rewriting comprises: based on the first optimization rewriting and the second optimization rewriting, obtaining a target SQL statement after the first optimization rewriting alone, a target SQL statement after the second optimization rewriting alone, and a target SQL statement after the first optimization rewriting and the second optimization rewriting in combination.

7. The method of claim 6, wherein, After the first optimization rewriting and the second optimization rewriting, the method comprises: The performance indicators of each target SQL statement are analyzed by the large language model to determine the target SQL statement with the highest performance indicator, and the optimization rewriting mode of the target SQL statement is preferentially selected in subsequent optimization rewriting.

8. The method of claim 1, wherein, The preset database management system is a database management system in an electronic game scenario, and the database management system generates a plurality of candidate plans based on the SQL statement to be optimized, estimates the overhead of each candidate plan through a cost model, and selects the candidate plan with the lowest cost as an execution plan. 9.A system for analyzing and optimizing SQL statements based on a large language model, characterized in that, The system is used to execute the method of any one of claims 1 to 8, and the system comprises an analysis extraction module and an optimization rewriting module. The analysis extraction module is used to analyze and extract the SQL statement to be optimized to obtain metadata information of the tables involved; and analyze and extract the execution plan generated based on the SQL statement to obtain execution plan information. The optimization rewriting module is used to input the metadata information of the tables and the execution plan information into a large language model, perform first, second and third optimization rewriting on the SQL statement by the large language model, and output the target SQL statement after optimization rewriting.

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

  • Method, device and equipment for automatically optimizing SQL (Structured Query Language) execution performance and medium

    CN119003574A