Method and system for generating SQL (Structured Query Language) rewriting rule for SQL optimization based on case
By generating logical plans for SQL query statements and applying generalization rules to generate equivalent plan pairs, the difficulty of discovering and optimizing complex SQL rewriting rules in the existing technology is solved, and efficient SQL optimization and rule set simplification is achieved.
Patent Information
- Application Number
- CN202411881351.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2044-12-19
AI Technical Summary
The prior art has difficulties in discovering and optimizing SQL rewriting rules involving more operator numbers or specific aggregation functions and predicates, and it is difficult to remove substituted rules, resulting in redundancy in rule sets and affecting the efficiency of SQL optimization.
Generate logical plans by generating the input SQL query statement and generate equivalent logical plans pairs through generalization rules as rewrite rules. The method includes parsing, generalization and verification steps, which can automatically discover overwrite rules and remove redundant rules.
It significantly improves the efficiency of discovering rewrite rules, can handle rewrite rules of more operators and specific aggregation functions and predicates, removes redundant rules, optimizes the capabilities of the SQL optimization engine, and reduces the delay of SQL optimization.
Smart Images

Figure CN119848083A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of SQL optimization, and in particular to a method and system for generating SQL rewriting rules based on cases for SQL optimization. Background Art
[0002] Relational databases are often used by Internet applications to store and manage data, and usually use Structured Query Language (SQL) to add, delete, modify, and query data. The SQL optimization method based on rewriting rules is an important method in the database field. It uses rewriting rules to rewrite SQL statements or their corresponding logical plans, thereby reducing the execution time of SQL statements. It can be used in all scenarios that require the execution of SQL statements, and discovering rewriting rules is a fundamental problem in the database field. Therefore, database developers have been looking for more effective SQL rewriting rule discovery methods.
[0003] There are two main existing methods for discovering rewriting rules: database experts manually write rewriting rules, and automatically discover rewriting rules through brute force enumeration. In the first method, database experts summarize several rewriting rules based on their understanding and experience of SQL and its execution methods. The disadvantage of this method is that it requires a lot of manpower to discover and summarize rewriting rules, and it is easy to miss potential rules due to the limitations of human thinking and experience. In the second method, the existing representative rule discovery tool is WeTune, which enumerates logical plans with a certain range of operator numbers and calls a validator to verify the equivalence between these plans. Equivalent plan pairs constitute rewriting rules. This process of discovering rewriting rules is fully automatic and does not require human intervention, but it has several disadvantages: first, it is difficult to enumerate logical plans with a large number of operators, because as the number of operators increases, the number of possible logical plans increases exponentially; second, the enumerated plans do not contain specific aggregate functions and predicates, which limits its discovery of rewriting rules related to specific aggregate functions and predicates; finally, among the rules it discovers, there may be a situation where one rule can be completely replaced by another rule, which will meaninglessly increase the number of rewriting rules, thereby increasing the delay of rewriting and the delay from SQL statement input to execution completion. Therefore, previous work has certain limitations.
[0004] Patent document CN117194473A discloses a SQL statement processing method, database system, device and storage medium, and specifically discloses obtaining a first SQL statement for querying target data, a timestamp field to be segmented and counted, and a segment duration; rewriting the first SQL statement according to a set rewriting rule to obtain a second SQL statement for further counting the number of rows by time period for the target data; executing the second SQL statement to obtain the corresponding time period statistics result; wherein the rewriting rule includes a selection clause modification sub-rule and a grouping clause modification sub-rule; the selection clause modification sub-rule includes: adding a first field and a second field, retaining the same fields in the selection clause included in the first SQL statement and the grouping clause included in the first SQL statement; the grouping clause modification sub-rule includes: adding the first field. The comparative document does not have the ability to automatically and efficiently discover rewriting rules involving more operators or specific aggregation functions and predicates, and remove rules that can be replaced. Summary of the invention
[0005] In view of the defects in the prior art, the object of the present invention is to provide a method and system for generating SQL rewriting rules based on cases for SQL optimization.
[0006] A method for generating SQL rewriting rules based on cases for SQL optimization provided by the present invention includes:
[0007] Step S1: parse the input SQL query statement to generate a logical plan;
[0008] Step S2: generalize the logic plan pairs until they cannot be generalized any further, and use the equivalent logic plan pairs that cannot be generalized as rewriting rules;
[0009] Step S3: Verify the equivalence of a pair of logical plans.
[0010] Preferably, in step S1:
[0011] Parse a pair of input SQL query statements and generate a pair of logical plans;
[0012] Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
[0013] Preferably, in step S2:
[0014] For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; the equivalence of the generated logical plan pairs is verified by the verifier in step S3, the inequivalent ones are discarded, and the equivalent ones are used as new inputs to execute this generalization step again; if a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
[0015] Preferably, in step S2:
[0016] Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the verifier in step S3.
[0017] Preferably, in step S3:
[0018] Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance.
[0019] The possible output results are equivalence and inequality, where equivalence means that the verifier proves the equivalence of the input logical plans, and inequality means that the verifier cannot prove the equivalence of the input logical plans. The logical plan equivalence and inequality mentioned in the generalizer correspond to the two output results of the verifier here.
[0020] According to the present invention, a system for generating SQL rewriting rules based on cases for SQL optimization is provided, comprising:
[0021] Module M1: parse the input SQL query statement to generate a logical plan;
[0022] Module M2: Generalize the logical plan pairs until they cannot be generalized any further, and use the equivalent logical plan pairs that cannot be generalized as rewriting rules;
[0023] Module M3: Verify the equivalence of a pair of logical plans.
[0024] Preferably, in the module M1:
[0025] Parse a pair of input SQL query statements and generate a pair of logical plans;
[0026] Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
[0027] Preferably, in the module M2:
[0028] For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; among the generated logical plan pairs, inequivalent ones are discarded, and equivalent ones are used as new inputs to execute this generalization step again; if a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
[0029] Preferably, in the module M2:
[0030] Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the validator.
[0031] Preferably, in the module M3:
[0032] Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance.
[0033] The possible output results are equivalence and inequality. Equivalence means that the verifier proves the equivalence of the input logical plan, and inequality means that the verifier cannot prove the equivalence of the input logical plan. The equivalence and inequality of the logical plan mentioned in the generalizer correspond to the two output results of the verifier here. Specifically, any verifier that can prove the equivalence of logical plans can be used, including existing verifiers.
[0034] Compared with the prior art, the present invention has the following beneficial effects:
[0035] 1. The present invention proposes a method for generating rewriting rules based on equivalent SQL statements, so that users can quickly discover potential rules based on a pair of SQL statements, avoiding exhaustive enumeration of all possible SQL statements or logical plans, and greatly improving the efficiency of discovering rewriting rules;
[0036] 2. The rules that can be discovered by the rule discovery method proposed in the present invention are not limited by the number of operators, which enhances the ability to discover rewriting rules containing more operators;
[0037] 3. The rule discovery method proposed in the present invention can discover rewriting rules involving specific aggregation functions or predicates, and its discovery ability depends on the proof ability of the verification tool used;
[0038] 4. The rule discovery method proposed in the present invention can obtain rules with a wider range of applications based on known rules, and effectively remove rules that can be completely replaced by other rules, so that the discovered rewriting rule set is more streamlined and more universal, thereby enhancing the SQL optimization capability while minimizing the increase in SQL optimization latency. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0040] Figure 1 It is a schematic diagram of the overall structure of the present invention;
[0041] Figure 2 The figure is a schematic diagram of a specific implementation of a generalizer and a verifier of the present invention. DETAILED DESCRIPTION
[0042] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.
[0043] Embodiment 1:
[0044] The present invention provides a method for generating SQL rewriting rules based on cases for SQL optimization, comprising: a parsing step: parsing an input SQL query statement to generate a logical plan; a generalization step: continuously generalizing a logical plan pair until generalization cannot be continued, and using an equivalent logical plan pair that cannot be generalized as a rewriting rule; a verification step: verifying the equivalence of a pair of SQL query statements. The present invention provides a method for automatically discovering rewriting rules through cases, which significantly enhances the optimization capability of a SQL optimization engine based on rewriting rules.
[0045] According to a method for generating SQL rewriting rules based on cases for SQL optimization provided by the present invention, Figure 1-Figure 2 As shown, including:
[0046] Step S1: parse the input SQL query statement to generate a logical plan;
[0047] Specifically, in step S1:
[0048] Parse a pair of input SQL query statements and generate a pair of logical plans;
[0049] Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
[0050] Step S2: generalize the logic plan pairs until they cannot be generalized any further, and use the equivalent logic plan pairs that cannot be generalized as rewriting rules;
[0051] Specifically, in step S2:
[0052] For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; among the generated logical plan pairs, inequivalent ones are discarded, and equivalent ones are used as new inputs to execute this generalization step again; if a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
[0053] Specifically, in step S2:
[0054] Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the validator.
[0055] Step S3: Verify the equivalence of a pair of logical plans.
[0056] Specifically, in step S3:
[0057] Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance.
[0058] The possible output results are equivalence and inequality, where equivalence means that the verifier proves the equivalence of the input logical plans, and inequality means that the verifier cannot prove the equivalence of the input logical plans. The logical plan equivalence and inequality mentioned in the generalizer correspond to the two output results of the verifier here.
[0059] Embodiment 2:
[0060] Embodiment 2 is a preferred example of Embodiment 1, and is used to illustrate the present invention in more detail.
[0061] The present invention also provides a system for generating SQL rewriting rules based on cases for SQL optimization. The system for generating SQL rewriting rules based on cases for SQL optimization can be implemented by executing the process steps of the method for generating SQL rewriting rules based on cases for SQL optimization, that is, those skilled in the art can understand the method for generating SQL rewriting rules based on cases for SQL optimization as a preferred implementation of the system for generating SQL rewriting rules based on cases for SQL optimization.
[0062] According to the present invention, a system for generating SQL rewriting rules based on cases for SQL optimization is provided, comprising:
[0063] Module M1: parse the input SQL query statement to generate a logical plan;
[0064] Specifically, in the module M1:
[0065] Parse a pair of input SQL query statements and generate a pair of logical plans;
[0066] Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
[0067] Module M2: Generalize the logical plan pairs until they cannot be generalized any further, and use the equivalent logical plan pairs that cannot be generalized as rewriting rules;
[0068] Specifically, in the module M2:
[0069] For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; among the generated logical plan pairs, inequivalent ones are discarded, and equivalent ones are used as new inputs to execute this generalization step again; if a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
[0070] Specifically, in the module M2:
[0071] Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the validator.
[0072] Module M3: Verify the equivalence of a pair of logical plans.
[0073] Specifically, in the module M3:
[0074] Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance.
[0075] The possible output results are equivalence and inequality, where equivalence means that the verifier proves the equivalence of the input logical plans, and inequality means that the verifier cannot prove the equivalence of the input logical plans. The logical plan equivalence and inequality mentioned in the generalizer correspond to the two output results of the verifier here.
[0076] Embodiment 3:
[0077] Embodiment 3 is a preferred example of Embodiment 1, and is used to illustrate the present invention in more detail.
[0078] The embodiment of the present invention provides a system for generating SQL rewriting rules based on cases for SQL optimization, generalizing a pair of SQL statements and submitting them to a validator for verification of equivalence, thereby obtaining rewriting rules. Compared with the prior art, the present invention can generate rewriting rules involving more operators and specific aggregation functions and predicates, and remove redundant rewriting rules, solving the problem that it is difficult to exhaustively enumerate rewriting rules involving more operators and specific aggregation functions and predicates in the prior art, and it is difficult to remove redundant rewriting rules, greatly enhancing the capabilities of the rule-based SQL optimization engine.
[0079] The embodiment of the present invention provides an automated system for realizing a method for generating SQL rewriting rules based on cases for SQL optimization, parsing a specific SQL statement pair into a logical plan pair and generalizing it, generating a number of logical plan pairs, and calling a validator to check the equivalence of the logical plan pairs; the equivalent logical plan pairs that cannot be generalized are the rewriting rules. Figure 1 As shown, the modules that make up the system include:
[0080] Parser: Parses the input SQL query statement and generates a logical plan. Each SQL statement corresponds to a logical plan. The logical plan is a representation used to describe the SQL query operation process and contains a series of operators.
[0081] Generalizer: For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. Among the generated logical plan pairs, the inequivalent ones are discarded, and the equivalent ones are used as new inputs to execute this generalization step again; if a pair of logical plans does not generate a new logical plan, the pair of logical plans is used as a new rewriting rule. Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule should be a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pairs is verified by the validator.
[0082] Verifier: Verifies whether a pair of logical plans are equivalent, that is, they produce the same results when executed on any database instance. There are two possible output results: "equivalence" and "inequivalence", where "equivalence" means that the verifier proves that the input logical plans are equivalent, and "inequivalence" means that the verifier cannot prove that the input logical plans are equivalent; the logical plans "equivalence" and "inequivalence" mentioned in the generalizer correspond to the two output results of the verifier here.
[0083] The complete implementation process of the present invention is described by the following contents:
[0084] The system receives a pair of SQL statements and their context as input, where the context is the definition of the database table involved, including the names of the columns, data types, and integrity constraints of the table.
[0085] Parser: Parses SQL statements. First, it converts each SQL statement into an abstract syntax tree according to the SQL grammar. The abstract syntax tree represents the grammatical structure of the SQL statement, which is convenient for subsequent analysis. Then it analyzes the semantics contained in the abstract syntax tree and generates a logical plan based on the semantics of the abstract syntax tree. Each abstract syntax tree generates a logical plan, which represents the specific process of executing the SQL statement.
[0086] Generalizer: Input a pair of logical plans, generalize them, generate several pairs of logical plans, and collect potential rewriting rules. Figure 2 As shown in the figure, the generalizer consists of several modules, among which the generalization queue is used to store the logical plan pairs waiting for generalization; the controller is responsible for process control; abstract replacement and symbol renaming are two generalization rules, which are used to generalize a pair of logical plans and generate several pairs of logical plans. In the initial state, the generalization queue only stores the input logical plan pairs. The controller controls the process inside the generalizer, and a complete generalization process implements the following steps in sequence:
[0087] 1. If the generalization queue is not empty, obtain a pair of logical plans from the generalization queue. Otherwise, the generalization process terminates.
[0088] 2. Apply abstract replacement and symbol renaming to the pair of logical plans to obtain several pairs of logical plans. Specifically, abstract replacement replaces all or part of the specific sub-plans or predicates in the two logical plans with abstract symbols; symbol renaming replaces several identical symbols in the two logical plans with different symbols, where the symbols include table names and column names.
[0089] In order to explain abstract replacement and symbol renaming, we first need to introduce the form of logical plan. Take the logical plan project(a0,filter(union(t1,t2),a0=a1)) as an example. The execution flow it represents is: first take database tables t1 and t2, apply the union operator to them to generate a new table, then apply the filter operator (with parameter a0=a1) to the resulting table to get a new table, and finally apply the project operator (with parameter a0) to the resulting table to get the final result. Among them, project, filter, and union are operators. Each operator represents an operation. The input of the operator is several tables (such as t1 and t2 in union(t1,t2) and union(t1,t2) in filter(union(t1,t2),a0=a1)). Some operators require additional parameters (such as a0=a1 in filter(union(t1,t2),a0=a1) is an additional parameter of filter, indicating that the filter operator filters out the rows in its input that meet the condition a0=a1 and forms a new table as output). The output of the operator is a table.
[0090] To explain abstract replacement, as an example, assume that the current pair of logical plans are project(a0,filter(union(t1,t2),a0=a1)) and project(a1,filter(union(t1,t2),a0=a1)), where union(t1,t2) appears in both logical plans. Therefore, the same symbol r can be used to replace union(t1,t2), and the resulting pair of new logical plans are project(a0,filter(r,a0=a1)) and project(a1,filter(r,a0=a1)); the process of replacing the same sub-logical plans (such as union(t1,t2) in the example) or predicates (such as a0=a1 in the example, no replacement is made in this example) in a pair of logical plans with the same symbol is called abstract replacement. Since r can represent any table and the output of union(t1,t2) is a table, the original logical plan pair can be regarded as a special case of the new logical plan pair when r=union(t1,t2); the new logical plan pair is more general as a rewriting rule.
[0091] To explain symbol renaming, as an example, assume that the current pair of logical plans are project(r.a0, join(r,r,r.a0=r.a1)) and project(r.a1, join(r,r,r.a0=r.a1)), where symbol r represents any table and all occurrences of r represent the same table. A part of the occurrences of symbol r can be replaced with a different symbol, and the resulting pair of new logical plans are project(r.a0, join(r,r0,r.a0=r0.a1)) and project(r0.a1, join(r,r0,r.a0=r0.a1)); the process of replacing part of the same symbol in a pair of logical plans with another symbol is called conforming renaming. Since r0 and r can represent the same table or two different tables, the original logical plan pair can be regarded as a special case of the new logical plan pair when r0=r; the new logical plan pair is more general as a rewriting rule.
[0092] 3. Input the logical plan pair obtained in the previous step to the validator, and the validator determines whether the two are equivalent. If no logical plan pair was generated in the previous step, or this step determines that all the logical plan pairs generated in the previous step are not equivalent, it is considered that the logical plan pair obtained in step 1 cannot be generalized further, and it is output to the rewrite rule set and executed again from step 1; otherwise, the logical plan pair determined to be equivalent in this step is inserted into the generalization queue.
[0093] Validator: Input a pair of logical plan and context and determine whether they are equivalent. Specifically, refer to Figure 2 As shown, the validator consists of two modules, wherein the validator front end is used to convert a pair of logical plans and contexts into a pair of SQL statements and the definitions of the tables involved; the SQL validator is used to prove the equivalence of a pair of SQL statements under a given table definition. The SQL validator can use existing SQL equivalence verification tools.
[0094] The front end of the validator constructs a pair of SQL statements and the definition of the tables involved according to the input pair of logical plans and context. The transformation of the logical plan to the SQL statement can start from the bottom-level table symbol and be transformed into SQL statements layer by layer from bottom to top, thus completing the transformation of the entire logical plan; this transformation process does not need to consider the execution efficiency of the transformed SQL statement, but only needs to ensure that the transformation process does not change the query results in all cases. The names and data types of each column of the table come directly from the context; for the table represented by the new symbol generated during the generalization process, the names and data types of each column can be inferred from the table (database table or operator output) replaced by the symbol. The integrity constraints of the table come directly from the integrity constraints in the context. The table represented by the new symbol generated during the generalization process has no integrity constraints, which will not affect the reliability of the validator.
[0095] Finally, the rewritten rule set output by the generalizer is the final output of this system.
[0096] The present invention generates a rewriting rule according to two pairs of equivalent queries respectively, and both rewriting rules can reduce query delay by up to 34%. One of the rewriting rules can optimize SQL query statements that cannot be optimized by the SQL optimization engine of the existing database system SQL Server in the industry.
[0097] Those skilled in the art know that, in addition to realizing the system and its various devices, modules, and units provided by the present invention in a purely computer-readable program code, it is entirely possible to realize the same functions in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers by logically programming the method steps. Therefore, the system and its various devices, modules, and units provided by the present invention can be considered as a hardware component, and the devices, modules, and units included therein for realizing various functions can also be regarded as structures within the hardware component; the devices, modules, and units for realizing various functions can also be regarded as both software modules for realizing the method and structures within the hardware component.
[0098] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A method for generating SQL rewriting rules based on cases for SQL optimization, characterized in that: include: Step S1: parse the input SQL query statement to generate a logical plan; Step S2: generalize the logic plan pairs until they cannot be generalized any further, and use the equivalent logic plan pairs that cannot be generalized as rewriting rules; Step S3: Verify the equivalence of a pair of logical plans.
2. The method for generating SQL rewriting rules based on cases for SQL optimization according to claim 1, characterized in that: In step S1: Parse a pair of input SQL query statements and generate a pair of logical plans; Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
3. The method for generating SQL rewriting rules based on cases for SQL optimization according to claim 1, characterized in that: In step S2: For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; among the generated logical plan pairs, the inequivalent ones are discarded, and the equivalent ones are used as new inputs to execute this generalization step again; If a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
4. The method for generating SQL rewriting rules based on cases for SQL optimization according to claim 3, characterized in that: In step S2: Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the validator.
5. The method for generating SQL rewriting rules based on cases for SQL optimization according to claim 1, characterized in that: In step S3: Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance. The possible output results are equivalence and inequality, where equivalence means that the verifier proves the equivalence of the input logical plans, and inequality means that the verifier cannot prove the equivalence of the input logical plans. The logical plan equivalence and inequality mentioned in the generalizer correspond to the two output results of the verifier here.
6. A system for generating SQL rewriting rules based on cases for SQL optimization, characterized in that: include: Module M1: parse the input SQL query statement to generate a logical plan; Module M2: Generalize the logical plan pairs until they cannot be generalized any further, and use the equivalent logical plan pairs that cannot be generalized as rewriting rules; Module M3: Verify the equivalence of a pair of logical plans.
7. The system for generating SQL rewriting rules based on cases for SQL optimization according to claim 6, characterized in that: In the module M1: Parse a pair of input SQL query statements and generate a pair of logical plans; Each SQL statement corresponds to a logical plan, which is a representation used to describe the SQL query operation process and contains a series of operators.
8. The system for generating SQL rewriting rules based on cases for SQL optimization according to claim 6, characterized in that: In the module M2: For any pair of logical plans, several pairs of logical plans are generated through predefined generalization rules. The generated logical plan pairs should be able to completely replace the input logical plan pairs as rewriting rules; among the generated logical plan pairs, the inequivalent ones are discarded, and the equivalent ones are used as new inputs to execute this generalization step again; If a pair of logical plans does not generate a new equivalent logical plan, the pair of logical plans are collected as new rewriting rules.
9. The system for generating SQL rewriting rules based on cases for SQL optimization according to claim 8, characterized in that: In the module M2: Among them, the generalization rule is a rule that generates several pairs of logical plans based on a pair of logical plans; if a pair of logical plans is regarded as a rewriting rule, then the set of SQL statements that can be rewritten by each pair of logical plans generated after applying the generalization rule is a subset of the logical plan pair before applying the generalization rule; the equivalence of the logical plan pair is verified by the validator.
10. The system for generating SQL rewriting rules based on cases for SQL optimization according to claim 6, characterized in that: In the module M3: Verify that a pair of logical plans are equivalent and produce the same results when executed on any database instance. The possible output results are equivalence and inequality, where equivalence means that the verifier proves the equivalence of the input logical plans, and inequality means that the verifier cannot prove the equivalence of the input logical plans. The logical plan equivalence and inequality mentioned in the generalizer correspond to the two output results of the verifier here.
Citation Information
Patent Citations
SQL statement processing method and device, server and storage medium
CN108304505A
Database query rewriting rule automatic generation method and system
CN114756579A
Equivalence verification method and system for template database query plan
CN114756581A
SQL statement optimization method and device
CN116561154A
SQL (Structured Query Language) statement processing method, database system, equipment and storage medium
CN117194473A