SQL statement optimization method and device

By analyzing and optimizing SQL statements, using common configuration rules sets to merge subqueries, deleting redundant fields, converting data types and adjusting table locations, the problem of poor performance of SQL statements is solved, and efficient query across databases is achieved.

CN116561154BActive Publication Date: 2025-09-02BANK OF CHINA FINANCIAL TECH CO LTD

Patent Information

Application Number
CN202210106821.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-28
Publication Date
2025-09-02
Estimated Expiration
2042-01-28

AI Technical Summary

Technical Problem

SQL statements written in databases have poor performance, resulting in low query efficiency and need to write different SQL statements according to different databases, affecting the execution efficiency of data queries.

Method used

By analyzing SQL statements, obtaining the nodes to be optimized, and optimizing them based on the pre-configured general configuration ruleset, including merging subqueries, deleting redundant fields, converting data types, adjusting table locations, and optimizing execution plans.

Benefits of technology

Improves the query efficiency of SQL statements, making them suitable for a variety of databases, and improves query performance and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116561154B_ABST
    Figure CN116561154B_ABST
Patent Text Reader

Abstract

The present application provides a method and device for optimizing SQL statements. The method comprises: analyzing the SQL statement to obtain the node to be optimized of the SQL statement; querying a pre-configured general configuration rule set according to the node to be optimized; optimizing the SQL statement based on the general configuration rule set to obtain a target SQL statement. The SQL statement optimization method provided in the embodiment of the present application performs equivalent optimization on the SQL statement through general configuration rules, so that even if the performance of the written SQL statement is poor, it can be converted into a valid SQL statement for query, and the SQL statement is optimized through general configuration rules so that it can be applied to queries of multiple databases, thereby improving query efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of database technology, and in particular to a method and device for optimizing SQL statements. Background Art

[0002] SQL, short for Structured Query Language, is a database query and programming language used to access data, query, update, and manage relational database systems. It is also the extension for database script files. Currently, SQL is commonly used to query and analyze data in various databases that store massive amounts of data.

[0003] However, in actual applications, developers often need to write SQL statements with complex logic. Some developers, unfamiliar with the underlying principles of SQL, may write SQL statements with poor performance, affecting query efficiency. Furthermore, different SQL statements must be written for different databases or storage methods, resulting in low data query execution efficiency. Summary of the Invention

[0004] The embodiments of the present application provide a method and apparatus for optimizing SQL statements, which optimize SQL statements from a general and practical perspective to improve query efficiency.

[0005] In a first aspect, an embodiment of the present application provides a method for optimizing SQL statements, including:

[0006] Analyze the SQL statement to obtain the node to be optimized of the SQL statement;

[0007] Querying a pre-configured general configuration rule set according to the node to be optimized;

[0008] Based on the general configuration rule set, the SQL statement is optimized to obtain a target SQL statement.

[0009] In one embodiment, analyzing the SQL statement to obtain the node to be optimized of the SQL statement includes:

[0010] Performing grammatical analysis on the SQL statement to detect the grammar of the SQL statement, and when detecting that the grammar of the SQL statement meets a preset condition, performing lexical analysis on the SQL statement to obtain a node to be optimized of the SQL statement.

[0011] In one embodiment, optimizing the SQL statement based on the general configuration rule set includes:

[0012] Based on the general configuration rule set, a subquery in an SQL statement is detected, and when it is detected that the subquery does not contain an aggregate function, the subquery is merged with a main query in the SQL statement.

[0013] In one embodiment, optimizing the SQL statement based on the general configuration rule set includes:

[0014] Based on the general configuration rule set, a query field in an SQL statement is detected, and when it is detected that the query field is a redundant field, the query field is deleted.

[0015] In one embodiment, optimizing the SQL statement based on the general configuration rule set includes:

[0016] Based on the general configuration rule set, an association condition in an SQL statement is detected, and when it is detected that the association condition is an association between a function and a constant, an equivalent function of the function is added to the constant.

[0017] In one embodiment, optimizing the SQL statement based on the general configuration rule set includes:

[0018] Based on the general configuration rule set, the associated fields in the SQL statement are detected, and when it is detected that the data types of the data associated with the associated fields are inconsistent, the data types of the data associated with the associated fields are type-converted to make the data types of the data associated with the associated fields consistent.

[0019] In one embodiment, it further includes:

[0020] The position of each data table in the target SQL statement is adjusted according to the data size of each data table associated with the query in the target SQL statement.

[0021] In one embodiment, it further includes:

[0022] According to the data volume of the data table used for query in the target SQL statement, the frequency of historical execution plans corresponding to the data volume of the data table is detected in the historical query records, so as to optimize the execution plan of the data table according to the historical execution plan with the highest frequency.

[0023] In a second aspect, an embodiment of the present application provides a SQL statement optimization device, comprising:

[0024] An optimization node acquisition module is used to analyze SQL statements and obtain nodes to be optimized in the SQL statements;

[0025] A rule query module, configured to query a pre-configured general configuration rule set according to the node to be optimized;

[0026] The SQL statement optimization module is used to optimize the SQL statement based on the general configuration rule set to obtain a target SQL statement.

[0027] In a third aspect, an embodiment of the present application provides an electronic device comprising a processor and a memory storing a computer program, wherein when the processor executes the program, the steps of the SQL statement optimization method described in the first aspect are implemented.

[0028] In a fourth aspect, an embodiment of the present application provides a computer program product, comprising a computer program, which, when executed by a processor, implements the steps of the SQL statement optimization method described in the first aspect.

[0029] The SQL statement optimization method and device provided in the embodiments of the present application analyze the SQL statement, obtain the nodes to be optimized of the SQL statement, and then perform equivalent optimization on the SQL statement from the perspective of generality and usage through general configuration rules. Even if the written SQL statement has poor performance, it can be converted into a valid SQL statement for query. Moreover, through optimization through general configuration rules, the SQL statement can be applied to queries of various databases, thereby improving query efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0031] Figure 1 This is a flow chart of a SQL statement optimization method provided by an embodiment of the present invention;

[0032] Figure 2 It is a structural diagram of the SQL statement optimization device provided by the present invention;

[0033] Figure 3 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0034] To make the objectives, technical solutions, and advantages of this application more clear, the technical solutions in this application will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of this application. Obviously, the described embodiments are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making any creative efforts are within the scope of protection of this application.

[0035] Below, the SQL statement optimization method provided by the embodiment of the present application will be introduced and explained in detail through several specific embodiments.

[0036] Reference Figure 1 , is one of the flow charts of a SQL statement optimization method provided in this embodiment, which is applied to a server or other monitoring device to optimize SQL statements. Figure 1 As shown, this embodiment provides a SQL statement optimization method including:

[0037] Step 101: Analyze the SQL statement to obtain the node to be optimized of the SQL statement;

[0038] Step 102: querying a pre-configured general configuration rule set according to the node to be optimized;

[0039] Step 103: Optimize the SQL statement based on the general configuration rule set to obtain a target SQL statement.

[0040] By analyzing the SQL statements and obtaining the nodes to be optimized in the SQL statements, the SQL statements are equivalently optimized from a general and usage perspective through common configuration rules. This allows even if the written SQL statements have poor performance to be converted into valid SQL statements for querying. Furthermore, through optimization through common configuration rules, the SQL statements can be applied to queries of multiple databases, thereby improving query efficiency.

[0041] In one embodiment, after obtaining an SQL statement for querying data, the SQL statement is pre-parsed for lexical and syntactic analysis to identify object information, field information, association information, filter conditions, sorting conditions, and other information within the SQL statement as nodes to be optimized. Object information refers to the data to be queried by the SQL statement, while field information includes the main query in the SQL statement, subqueries nested within the complete main query, and remaining query fields. Association information includes association fields, etc.

[0042] To avoid submitting abnormal queries, in one embodiment, analyzing the SQL statement to obtain the node to be optimized of the SQL statement includes:

[0043] Performing grammatical analysis on the SQL statement to detect the grammar of the SQL statement, and when detecting that the grammar of the SQL statement meets a preset condition, performing lexical analysis on the SQL statement to obtain a node to be optimized of the SQL statement.

[0044] In one embodiment, the obtained SQL statement is first parsed based on the syntax supported by different database types to detect whether the SQL statement conforms to the syntax requirements supported by the different databases, thereby identifying whether there are any problems with the SQL statement syntax. If the SQL statement conforms to the syntax requirements supported by the different databases, the SQL statement is determined to meet the preset conditions and lexical analysis is performed on it. Otherwise, an alarm is generated to indicate that the SQL statement is abnormal, and the SQL statement is filtered to prevent the submission of abnormal queries.

[0045] In one embodiment, when performing grammatical queries, time-consuming operations such as identifying Cartesian product relationships in large data tables and removing duplicate data from large data tables are also performed. However, such operations can affect database performance. Therefore, in one embodiment, time-consuming operations such as identifying Cartesian product relationships in large data tables and removing duplicate data from large data tables are omitted. Instead, normal SQL statements are directly lexically analyzed to obtain nodes to be optimized in the SQL statements, thereby maintaining database stability and security.

[0046] In one embodiment, to further prevent abnormal queries, lexical analysis can be performed on the acquired SQL statements based on the lexical support of different database types to detect whether the SQL statements meet the lexical requirements supported by different databases and identify any lexical issues with the SQL statements. If the SQL statements meet the lexical requirements supported by different databases, the SQL statements are determined to meet the preset conditions, and the nodes to be optimized in the SQL statements are determined based on the lexical analysis results. Otherwise, an alarm is generated to indicate that the SQL statement is abnormal, and the SQL statement is filtered to prevent the submission of abnormal queries.

[0047] In one embodiment, after obtaining each node to be optimized of the SQL statement, each node to be optimized is used to query a pre-configured general configuration rule set, and each general configuration rule corresponding to each node to be optimized is obtained from the general configuration rule set, and the corresponding nodes to be optimized are detected based on each general configuration rule, so as to detect whether each node to be optimized needs to be optimized according to each general configuration rule, and the nodes to be optimized that need to be optimized are optimized according to their corresponding general configuration rules, thereby obtaining the target SQL statement.

[0048] In one embodiment, the node to be optimized includes a subquery;

[0049] Optimizing the SQL statement based on the general configuration rule set to obtain a target SQL statement includes:

[0050] Based on the general configuration rule set, a subquery in an SQL statement is detected, and when it is detected that the subquery does not contain an aggregate function, the subquery is merged with a main query in the SQL statement.

[0051] In one embodiment, based on a pre-configured general configuration rule set for the sub-query, a general configuration rule corresponding to the sub-query is obtained, and based on the general configuration rule, it is detected whether the sub-query contains complex operations such as aggregate functions; if it is detected that the sub-query does not contain complex operations such as aggregate functions, it is merged with the main query into one query.

[0052] For example, if the subquery is Select*from(select*from b)b, it is equivalently optimized to select*from b; if the subquery is Select*from a,join(select*from b)bwhere a.id=b.id, it is equivalently optimized to select*from a,b where a.id=b.id.

[0053] By merging subqueries that do not contain complex operations such as aggregate functions with the main query, the complexity of the SQL statement is reduced, thereby improving the execution performance of the SQL statement and the query efficiency of the SQL statement.

[0054] In one embodiment, the node to be optimized includes a query field;

[0055] Optimizing the SQL statement based on the general configuration rule set includes:

[0056] Based on the general configuration rule set, a query field in an SQL statement is detected, and when it is detected that the query field is a redundant field, the query field is deleted.

[0057] In one embodiment, a pre-configured set of general configuration rules is searched based on a query field in an SQL statement to obtain a general configuration rule corresponding to the query field. Based on the general configuration rule, a check is then performed to determine whether the query field is a redundant field. If it is detected that the column corresponding to the query field does not exist in the data table corresponding to the SQL statement, the query field is determined to be a redundant field and is deleted from the SQL statement.

[0058] The query field is a field used for data query in the SQL statement.

[0059] For example, when the query field in the SQL statement is Select count(1)from(select id,name,sex from a)a, if the name and sex fields do not exist in the data table corresponding to the SQL statement, that is, the name and sex fields are not used in the query process, then the name and sex fields are determined to be redundant fields, and Select count(1)from(select id,name,sex from a)a is optimized to Select count(1)from(select id from a).

[0060] By detecting whether the query field is a redundant field and deleting the field when it is detected that the query field is a redundant field, the optimization effect of the SQL statement is improved, the processing time of the SQL statement is saved, and the execution performance of the SQL statement is further improved, thereby improving the query efficiency of the SQL statement.

[0061] In one embodiment, the node to be optimized includes an associated condition;

[0062] Optimizing the SQL statement based on the general configuration rule set includes:

[0063] Based on the general configuration rule set, an association condition in an SQL statement is detected, and when it is detected that the association condition is an association between a function and a constant, an equivalent function of the function is added to the constant.

[0064] In one embodiment, a pre-configured general configuration rule set is queried according to the association condition in the SQL statement, the general configuration rule corresponding to the association condition is obtained, and based on the general configuration rule, it is detected whether the association condition is an association between a function and a constant; if so, an equivalent function to the function is used on the constant.

[0065] For example, in the SQL statement, the join condition for querying the name field of table a with the number # padded to 10 digits on the left and equal to Xiao Zhang's data is:

[0066] Select id,name,sex from a,b where lpad(a.name,10,'#')='Xiao Zhang'and a.id=b.id

[0067] Then, the association condition is optimized as follows:

[0068] Select id,name,sex from a,b where a.name=ltrim('Xiao Zhang','#')and a.id=b.id.

[0069] In one embodiment, the node to be optimized includes an associated field of data;

[0070] Optimizing the SQL statement based on the general configuration rule set includes:

[0071] Based on the general configuration rule set, the associated fields in the SQL statement are detected, and when it is detected that the data types of the data associated with the associated fields are inconsistent, the data types of the data associated with the associated fields are type-converted to make the data types of the data associated with the associated fields consistent.

[0072] In one embodiment, a pre-configured set of general configuration rules is queried based on the associated fields of the data in the SQL statement to obtain the general configuration rules corresponding to the associated fields of the data. Based on the general configuration rules, the data types of the data involved in the associated fields are checked for consistency. If not, the data types of the associated data are converted in the associated fields. For example, if the associated field is "Select*from a where a.age = a.memo," and the data type of a.age is a numeric type and a.memo is a character type, "Select*from a where a.age = a.memo" is optimized to "Select*from a where to_char(a.age) = a.memo," thereby unifying the data types of the two associated data, avoiding the risks of implicit conversion, and ensuring the performance of SQL statement execution.

[0073] As you can understand, when optimizing SQL statements, you can optimize by testing one or more of the following: subqueries, query fields, join conditions, and join fields. The specific testing method used can be selected based on actual circumstances. To achieve better optimization results, you can simultaneously test subqueries, query fields, join conditions, and join fields to optimize SQL statements in multiple dimensions, thereby ensuring query efficiency.

[0074] In one embodiment, after obtaining the target SQL statement, the target SQL statement may be further optimized, including:

[0075] The position of each data table in the target SQL statement is adjusted according to the data size of each data table associated with the query in the target SQL statement.

[0076] In one embodiment, the amount of data involved in each data table of the associated query in the target SQL statement is pre-detected, and each data table is sorted from small to large according to the amount of data involved. Then, the table with the smallest amount of data in the associated query is optimized to the first place among the associated tables in the associated query to improve the efficiency of the associated query.

[0077] For example, consider table a, which contains 1,000 data items; table b, which contains 1 million data items; and the target SQL statement contains the join query "Select * from b join a on a.id = b.id." In this case, table a is modified to be the driving table, optimizing the join query to "Select * from a join b on a.id = b.id."

[0078] In one embodiment, in addition to optimizing the target SQL statement based on the data size of each data table it queries, the target SQL statement can also be optimized based on historical query information. Specifically, based on the data size of the data table queried in the target SQL statement, the frequency of historical execution plans corresponding to the data size of the data table is detected in historical query records, and the execution plan for the data table is optimized based on the historical execution plan with the highest frequency.

[0079] In one embodiment, the relationship between data tables is pre-recorded based on historical query records, along with the frequency of execution plans executed for different data volumes in the historical query records. When a target SQL statement uses a data table for a query, the highest-frequency historical execution plan corresponding to the data volume is retrieved based on the data volume of the data table. This means the most frequently occurring historical execution plan for the data table is retrieved. Based on these historical execution plans, an optimization method is then determined to optimize the execution plan for the data table in the target SQL statement, thereby avoiding query anomalies caused by incorrect execution plans.

[0080] For example, if the common execution plan for two tables a and b in the target SQL statement is hash_join, table c generated from table a has a similar data volume to table a, and the execution plan for joining tables b and c is nestloop, then add optimizations such as hints to optimize the execution plan for the data tables in the target SQL statement.

[0081] It is understood that when further optimizing the target SQL statement, the target SQL statement can be optimized based on the data size of the tables associated with the target SQL statement, or based on historical query information. The specific optimization method used can be selected based on actual circumstances. To achieve better optimization results for the target SQL statement, both of these methods can be used simultaneously to optimize the target SQL statement, thereby further ensuring SQL statement query efficiency.

[0082] The SQL statement optimization device provided by the present invention is described below. The SQL statement optimization device described below and the SQL statement optimization method described above can be referenced to each other.

[0083] In one embodiment, if Figure 2 As shown, a SQL statement optimization device is provided, comprising:

[0084] The optimization node acquisition module 210 is used to analyze the SQL statement and obtain the node to be optimized of the SQL statement;

[0085] A rule query module 220 is configured to query a pre-configured general configuration rule set according to the node to be optimized;

[0086] The SQL statement optimization module 230 is configured to optimize the SQL statement based on the general configuration rule set to obtain a target SQL statement.

[0087] By analyzing the SQL statements and obtaining the nodes to be optimized in the SQL statements, the SQL statements are equivalently optimized from a general and usage perspective through common configuration rules. This allows even if the written SQL statements have poor performance to be converted into valid SQL statements for querying. Furthermore, through optimization through common configuration rules, the SQL statements can be applied to queries of multiple databases, thereby improving query efficiency.

[0088] In one embodiment, the optimization node acquisition module 210 is specifically configured to:

[0089] Performing grammatical analysis on the SQL statement to detect the grammar of the SQL statement, and when detecting that the grammar of the SQL statement meets a preset condition, performing lexical analysis on the SQL statement to obtain a node to be optimized of the SQL statement.

[0090] In one embodiment, the node to be optimized includes a subquery;

[0091] The SQL statement optimization module 220 is specifically used for:

[0092] Based on the general configuration rule set, a subquery in an SQL statement is detected, and when it is detected that the subquery does not contain an aggregate function, the subquery is merged with a main query in the SQL statement.

[0093] In one embodiment, the node to be optimized includes a query field;

[0094] The SQL statement optimization module 220 is specifically used for:

[0095] Based on the general configuration rule set, a query field in an SQL statement is detected, and when it is detected that the query field is a redundant field, the query field is deleted.

[0096] In one embodiment, the node to be optimized includes an associated condition;

[0097] The SQL statement optimization module 220 is specifically used for:

[0098] Based on the general configuration rule set, an association condition in an SQL statement is detected, and when it is detected that the association condition is an association between a function and a constant, an equivalent function of the function is added to the constant.

[0099] In one embodiment, the node to be optimized includes an associated field of data;

[0100] The SQL statement optimization module 220 is specifically used for:

[0101] Based on the general configuration rule set, the associated fields in the SQL statement are detected, and when it is detected that the data types of the data associated with the associated fields are inconsistent, the data types of the data associated with the associated fields are type-converted to make the data types of the data associated with the associated fields consistent.

[0102] In one embodiment, the SQL statement optimization module 220 is further configured to:

[0103] The position of each data table in the target SQL statement is adjusted according to the data size of each data table associated with the query in the target SQL statement.

[0104] In one embodiment, the SQL statement optimization module 220 is further configured to:

[0105] According to the data volume of the data table used for query in the target SQL statement, the frequency of historical execution plans corresponding to the data volume of the data table is detected in the historical query records, so as to optimize the execution plan of the data table according to the historical execution plan with the highest frequency.

[0106] Figure 3 An example of a physical structure diagram of an electronic device is shown below. Figure 3 As shown, the electronic device may include: a processor 810, a communication interface 820, a memory 830, and a communication bus 840, wherein the processor 810, the communication interface 820, and the memory 830 communicate with each other via the communication bus 840. The processor 810 may call a computer program in the memory 830 to execute the steps of the SQL statement optimization method, for example, including:

[0107] Analyze the SQL statement to obtain the node to be optimized of the SQL statement;

[0108] Querying a pre-configured general configuration rule set according to the node to be optimized;

[0109] Based on the general configuration rule set, the SQL statement is optimized to obtain a target SQL statement.

[0110] In addition, the logic instructions in the above-mentioned memory 830 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0111] On the other hand, embodiments of the present application further provide a computer program product, comprising a computer program. The computer program may be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can perform the steps of the SQL statement optimization method provided in each of the above embodiments, for example, including:

[0112] Analyze the SQL statement to obtain the node to be optimized of the SQL statement;

[0113] Querying a pre-configured general configuration rule set according to the node to be optimized;

[0114] Based on the general configuration rule set, the SQL statement is optimized to obtain a target SQL statement.

[0115] On the other hand, an embodiment of the present application further provides a processor-readable storage medium, wherein the processor-readable storage medium stores a computer program, wherein the computer program is configured to cause a processor to execute the steps of the methods provided in the above embodiments, for example, including:

[0116] Analyze the SQL statement to obtain the node to be optimized of the SQL statement;

[0117] Querying a pre-configured general configuration rule set according to the node to be optimized;

[0118] Based on the general configuration rule set, the SQL statement is optimized to obtain a target SQL statement.

[0119] The processor-readable storage medium can be any available medium or data storage device that can be accessed by the processor, including but not limited to magnetic storage (such as floppy disks, hard disks, magnetic tapes, magneto-optical disks (MO)), optical storage (such as CDs, DVDs, BDs, HVDs, etc.), and semiconductor storage (such as ROMs, EPROMs, EEPROMs, non-volatile memories (NANDFLASH), solid-state drives (SSDs)), etc.

[0120] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.

[0121] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, or of course, by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or certain parts of the embodiments.

[0122] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A SQL statement optimization method, characterized in that: include: Analyze the SQL statement to obtain the node to be optimized of the SQL statement; The nodes to be optimized in the SQL statement include subqueries, query fields, associated fields, and associated conditions in the SQL statement; Querying a pre-configured general configuration rule set according to the node to be optimized; Optimizing the SQL statement based on the general configuration rule set to obtain a target SQL statement; Optimizing the SQL statement based on the general configuration rule set includes: Based on the general configuration rule set, an association condition in an SQL statement is detected, and when it is detected that the association condition is an association between a function and a constant, an equivalent function of the function is added to the constant.

2. The SQL statement optimization method according to claim 1, characterized in that: Analyzing the SQL statement to obtain the node to be optimized of the SQL statement includes: Performing grammatical analysis on the SQL statement to detect the grammar of the SQL statement, and when detecting that the grammar of the SQL statement meets a preset condition, performing lexical analysis on the SQL statement to obtain a node to be optimized of the SQL statement.

3. The SQL statement optimization method according to claim 1, characterized in that: Optimizing the SQL statement based on the general configuration rule set includes: Based on the general configuration rule set, a subquery in an SQL statement is detected, and when it is detected that the subquery does not contain an aggregate function, the subquery is merged with a main query in the SQL statement.

4. The SQL statement optimization method according to claim 1, characterized in that: Optimizing the SQL statement based on the general configuration rule set includes: Based on the general configuration rule set, a query field in an SQL statement is detected, and when it is detected that the query field is a redundant field, the query field is deleted.

5. The SQL statement optimization method according to claim 1, characterized in that: Optimizing the SQL statement based on the general configuration rule set includes: Based on the general configuration rule set, the associated fields in the SQL statement are detected, and when it is detected that the data types of the data associated with the associated fields are inconsistent, the data types of the data associated with the associated fields are type-converted to make the data types of the data associated with the associated fields consistent.

6. The SQL statement optimization method according to any one of claims 1 to 5, characterized in that: Also includes: The position of each data table in the target SQL statement is adjusted according to the data size of each data table associated with the query in the target SQL statement.

7. The SQL statement optimization method according to any one of claims 1 to 5, characterized in that: Also includes: According to the data volume of the data table used for query in the target SQL statement, the frequency of historical execution plans corresponding to the data volume of the data table is detected in the historical query records, so as to optimize the execution plan of the data table according to the historical execution plan with the highest frequency.

8. A SQL statement optimization device, characterized in that: include: An optimization node acquisition module is used to analyze SQL statements and obtain nodes to be optimized in the SQL statements; The nodes to be optimized in the SQL statement include subqueries, query fields, associated fields, and associated conditions in the SQL statement; A rule query module, configured to query a pre-configured general configuration rule set according to the node to be optimized; An SQL statement optimization module is used to optimize the SQL statement based on the general configuration rule set to obtain a target SQL statement; Optimizing the SQL statement based on the general configuration rule set includes: Based on the general configuration rule set, an association condition in an SQL statement is detected, and when it is detected that the association condition is an association between a function and a constant, an equivalent function of the function is added to the constant.

9. An electronic device comprising a processor and a memory storing a computer program, characterized in that: When the processor executes the computer program, the steps of the SQL statement optimization method according to any one of claims 1 to 7 are implemented.

10. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the SQL statement optimization method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Query statement optimization method and device

    CN112559554A

  • Data query method, middleware, electronic device and storage medium

    CN113553339A

Cited By

  • SQL (Structured Query Language) statement analysis optimization method and system based on large language model

    CN120353822A

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

    CN120353822B