A method, device, equipment, medium and product for optimizing structured query statements
By optimizing like expressions and equivalent expressions in structured query statements, the problem of low query efficiency of SQL statements under large data volume is solved, and significant shortening of query time and efficiency improvement is achieved.
Patent Information
- Application Number
- CN202411994809.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-04-08
AI Technical Summary
In the prior art, SQL statement query containing like expressions is too long to perform and query efficiency is too low in the case of large data volume.
By optimizing structured query statements, sub-branch expressions are obtained according to the priority of logical operators. If specific optimization conditions are met, like expressions and equivalent expressions are combined and optimized, optimization marks are determined and corresponding optimization operations are performed until all sub-branch expressions are optimized.
This greatly shortens the query time and improves query efficiency.
Smart Images

Figure CN119782356A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the technical field of database query, and in particular to a method, device, equipment, medium and product for optimizing structured query statements. Background Art
[0002] With the advent of the big data era, databases often need to select data that meets specific needs from massive amounts of data to complete query operations. Structured Query Language (SQL) is the most important and most commonly used relational database operation language. In SQL query statements, the like predicate is often used for fuzzy matching filtering.
[0003] SQL statement queries containing like expressions are a common type of query in databases. Like expressions are generally used to match strings. You can use expressions containing like to identify that a query item's string contains a specific string.
[0004] When the existing technology uses the Like expression as a filtering condition, it usually scans the entire table for rough data and then uses the Like regular expression for calculation. For example, the structured query statement is:
[0005] SELECT COUNT(1)FROM TEST WHERE C2 LIKE'%JING'AND C2='BEIJING';
[0006] When this statement is executed, the like expression on one side of the and expression will first scan the entire table data, and then use the calculated result of the like to compare with the equivalent expression condition on the other side of the and expression to finally get the execution result. However, when the amount of data in the table is large, it will take too long to execute and the query efficiency will be too low. Summary of the invention
[0007] The present invention provides a method, device, equipment, medium and product for optimizing structured query statements, so as to solve the problems that the execution of structured query statements takes too long and the query efficiency is too low.
[0008] According to one aspect of the present invention, a method for optimizing a structured query statement is provided, comprising:
[0009] Obtaining a structured query statement, and obtaining a first sub-branch expression from the structured query statement according to the priority of the logical operator;
[0010] If the first sub-branch expression satisfies the first optimization condition, obtaining a second sub-branch expression from the structured query statement according to the priority of the logical operator;
[0011] If the second sub-branch expression satisfies a second optimization condition, combining the first sub-branch expression and the second sub-branch expression into a first logical operator expression;
[0012] After determining that the first logical operator expression is the first target logical operator expression including a like expression and an equivalent expression, determining an optimization mark of the first target logical operator expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression, and performing corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression;
[0013] Obtaining a third sub-branch expression from the structured query statement according to the priority of the logical operator, and if the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, combining the first optimization expression and the third sub-branch expression into a second logical operator expression;
[0014] After determining that the second logical operator expression is the second target logical operator expression containing a like expression and an equal value expression, repeat the above process until all sub-branch expressions in the structured query statement are optimized, and add all optimized expressions in the sub-branch optimization linked list to the sub-branch linked list.
[0015] According to another aspect of the present invention, there is provided a device for optimizing a structured query statement, comprising:
[0016] A first acquisition module, used to acquire a structured query statement, and acquire a first sub-branch expression from the structured query statement according to the priority of a logical operator;
[0017] A second acquisition module, configured to acquire a second sub-branch expression from the structured query statement according to the priority of a logical operator if the first sub-branch expression satisfies a first optimization condition;
[0018] a first combining module, configured to combine the first sub-branch expression and the second sub-branch expression into a first logical operator expression if the second sub-branch expression satisfies a second optimization condition;
[0019] A first optimization module is used to determine that the first logical operator expression is a first target logical operator expression including a like expression and an equivalent expression, determine an optimization mark of the first target logical operator expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression, and perform corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression;
[0020] The second combination module is used to obtain a third sub-branch expression from the structured query statement according to the priority of the logical operator. If the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, the first optimization expression and the third sub-branch expression are combined into a second logical operator expression.
[0021] The second optimization module is used to determine that the second logical operator expression is a second target logical operator expression containing a like expression and an equal expression, and then repeat the above process until all sub-branch expressions in the structured query statement are optimized, and all optimized expressions in the sub-branch optimization linked list are added to the sub-branch linked list.
[0022] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:
[0023] at least one processor;
[0024] and a memory communicatively coupled to the at least one processor;
[0025] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the method for optimizing structured query statements described in any embodiment of the present invention.
[0026] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the method for optimizing structured query statements described in any embodiment of the present invention when executed.
[0027] According to another aspect of the present invention, a computer program product is provided. The computer program product includes a computer program. When the computer program is executed by a processor, the method for optimizing a structured query statement according to any embodiment of the present invention is implemented.
[0028] The technical solution of the embodiment of the present invention achieves the beneficial effect of greatly shortening the query time and improving the query efficiency by optimizing the conditions of the structured query statement.
[0029] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0031] Figure 1 A schematic diagram of a flow chart of a method for optimizing a structured query statement provided in Embodiment 1 of the present invention;
[0032] Figure 2 A schematic diagram of a flow chart of a method for optimizing a structured query statement provided in Embodiment 2 of the present invention;
[0033] Figure 3 A schematic diagram of the structure of a device for optimizing structured query statements provided in Embodiment 3 of the present invention;
[0034] Figure 4 The present invention is a schematic diagram of the structure of an electronic device for a method for optimizing structured query statements according to an embodiment of the present invention. DETAILED DESCRIPTION
[0035] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiment of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiment of the present invention. Obviously, the described embodiment is only an embodiment of a part of the present invention, rather than all the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work should belong to the scope of protection of the present invention. It should be understood that the various steps recorded in the method implementation of the present invention can be performed in different orders and / or in parallel. In addition, the method implementation may include additional steps and / or omit the steps shown. The scope of the present invention is not limited in this respect.
[0036] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0037] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0038] It should be noted that the modifications of "one" and "plurality" mentioned in the present invention are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0039] The names of the messages or information exchanged between multiple devices in the embodiments of the present invention are only used for illustrative purposes, and are not used to limit the scope of these messages or information.
[0040] Embodiment 1
[0041] Figure 1 A flow chart of a method for optimizing a structured query statement provided in Embodiment 1 of the present invention is applicable to the case of querying a database using an SQL statement containing a like expression. The method can be executed by an optimization device for structured query statements, wherein the device can be implemented by software and / or hardware and is generally integrated on an electronic device. In this embodiment, the electronic device includes but is not limited to: a computer device.
[0042] like Figure 1 As shown, a method for optimizing a structured query statement provided by Embodiment 1 of the present invention includes the following steps:
[0043] S110: Obtain a structured query statement, and obtain a first sub-branch expression from the structured query statement according to the priority of the logical operator.
[0044] In this embodiment, the structured query statement may be obtained in the following ways: way 1, through the official document of the database management system; way 2, through the online learning platform; way 3, through actual database operation.
[0045] Among them, the priority of the logical operator can be that the priority of the and logical operator is higher than that of the or logical operator, and the sub-branch expression on the left side of the and logical operator is obtained first, and the sub-branch expression on the right side of the or logical operator is obtained last.
[0046] Further, the obtaining of the first sub-branch expression from the structured query statement according to the priority of the logical operator expression includes: generating a memory structure of the structured query statement after analyzing the structured query statement; if the where clause in the memory structure is a logical operator expression, and the where clause includes both an and logical operator and an or logical operator, obtaining at least two sub-branch expressions connected by the first and logical operator from the logical operator expression, and obtaining the sub-branch expression located at the leftmost side of the logical operator expression among the at least two sub-branch expressions as the first sub-branch expression;
[0047] If the where clause in the memory structure is a logical operator expression, and the where clause only includes the or logical operator, then the sub-branch expression located at the leftmost side of the logical operator expression is obtained from the logical operator expression as the first sub-branch expression.
[0048] If the where clause in the memory structure is a logical operator expression, and the where clause only includes the and logical operator, then the sub-branch expression located at the leftmost side of the logical operator expression is obtained from the logical operator expression as the first sub-branch expression.
[0049] The where clause can be an or expression or an and expression.
[0050] Exemplarily, the where clause is nbexp1 and nbexp2 and nbexp3 or nbexp4, the where clause is an or expression, and the where clause includes both and logical operators and or logical operators. nbexp1 and nbexp2 and nbexp3 are obtained from the where clause, then nbexp1 and nbexp2 are obtained, and then nbexp1 is obtained as the first sub-branch expression.
[0051] S120: If the first sub-branch expression satisfies a first optimization condition, obtaining a second sub-branch expression from the structured query statement according to the priority of the logical operator.
[0052] Among them, the first optimization condition includes: the expression type is a like type or an equal value type; both sides of the expression are a column and a constant respectively.
[0053] Among them, the like expression is an operator used for pattern matching in SQL, which is mainly used to perform fuzzy queries on character data columns in the where clause of statements such as SELECT, DELETE, and UPDATE. The equivalent expression is an expression with the operator equal to.
[0054] In this embodiment, the method of obtaining the second sub-branch expression is similar to the method of obtaining the first sub-branch expression. For example, the where clause is nbexp1 and nbexp2 and nbexp3 or nbexp4. nbexp1 and nbexp2 and nbexp3 are obtained from the where clause, and then nbexp1 and nbexp2 are obtained, and then nbexp2 is obtained as the second sub-branch expression.
[0055] Furthermore, if the first sub-branch expression does not meet the first optimization condition, the second sub-branch expression is obtained from the structured query statement according to the priority of the logical operator, and it is determined whether the second sub-branch expression meets the first optimization condition until a sub-branch expression that meets the first optimization condition is determined.
[0056] Exemplarily, the where clause is nbexp1 and nbexp2 and nbexp3 or nbexp4. If the first sub-branch expression nbexp1 obtained does not meet the first optimization condition, then nbexp2 is obtained. If nbexp2 does not meet the first optimization condition, then nbexp3 is obtained. If nbexp3 meets the first optimization condition, nbexp3 is used as the sub-branch expression that meets the first optimization condition.
[0057] S130. If the second sub-branch expression satisfies a second optimization condition, combining the first sub-branch expression and the second sub-branch expression into a first logical operator expression.
[0058] The second optimization condition includes: the expression type is a like type or an equal value type; the two sides of the expression are a column and a constant respectively; the column name of the expression is the same as the column name of the sub-branch expression that meets the first optimization condition. Further, if the second sub-branch expression does not meet the second optimization condition, the next sub-branch expression is obtained from the where clause until a sub-branch molecular expression that meets the second optimization condition is obtained.
[0059] In this embodiment, the first logical operator expression can be an and expression composed of two sub-branch expressions, or an or expression composed of two sub-branch expressions. Exemplarily, the where clause is nbexp1 and nbexp2and nbexp3 or nbexp4. If nbexp1 satisfies the first optimization condition and nbexp2 satisfies the second optimization condition, then nbexp1 and nbexp2 can form an and expression to obtain nbexp1 and nbexp2; if nbexp1 satisfies the first optimization condition, nbexp2 and nbexp3 do not satisfy the second optimization condition, and nbexp4 satisfies the second optimization condition, then no optimization is performed. Exemplarily, the where clause is nbexp1 or nbexp2 or nbexp3. If nbexp1 satisfies the first optimization condition and nbexp2 satisfies the second optimization condition, nbexp1 and nbexp2 can form an or expression to obtain nbexp1or nbexp2; if nbexp1 satisfies the first optimization condition, nbexp2 does not satisfy the second optimization condition, and nbexp3 satisfies the second optimization condition, nbexp1 and nbexp3 can form an or expression to obtain nbexp1or nbexp3.
[0060] S140. After determining that the first logical operator expression is the first target logical operator expression including a like expression and an equivalent expression, determine the optimization mark of the first target logical operator expression according to the overlapping relationship between the condition string of the like expression and the condition string of the equivalent expression, and perform corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression.
[0061] Among them, the first target logical operator expression satisfies that one of the first sub-branch expression and the second sub-branch expression is an equal expression, and the other is a like expression.
[0062] For example, the first logical operator expression is nbexp1 and nbexp2. If nbexp1 is a like expression and nbexp2 is an equal expression, then nbexp1 and nbexp2 can be used as an and expression containing the like expression and the equal expression; if nbexp1 is an equal expression and nbexp2 is a like expression, then nbexp1 and nbexp2 can also be used as an and expression containing the like expression and the equal expression.
[0063] For example, the first logical operator expression is nbexp1 or nbexp2. If nbexp1 is a like expression and nbexp2 is an equal expression, then nbexp1 or nbexp2 can be used as an or expression containing the like expression and the equal expression; if nbexp1 is an equal expression and nbexp2 is a like expression, then nbexp1or nbexp2 can also be used as an or expression containing the like expression and the equal expression.
[0064] In this embodiment, the overlapping relationship between the conditional string of the like expression and the conditional string of the equivalent expression may include the following two situations: the conditional string of the like expression may contain the conditional string of the equivalent expression; the intersection of the conditional string of the like expression and the conditional string of the equivalent expression is empty.
[0065] For example, C2 LIKE'%JING'AND C2='BEIJING', the condition string of the like expression means that the last four characters of the C2 column value are 'JING', and the condition string of the equal value expression means that the C2 column value is 'BEJING'. It can be determined that the condition string of the like expression can contain the condition string of the equal value expression; for example, C2 LIKE'%JIN'AND C2='BEIJING', the condition string of the like expression means that the last three characters of the C2 column value are 'JIN', and the condition string of the equal value expression means that the C2 column value is 'BEJING'. It can be determined that the intersection of the condition string of the like expression and the condition string of the equal value expression is empty.
[0066] In this embodiment, the first target logical operator expression can be an and expression; accordingly, if the condition string of the like expression can contain the condition string of the equivalent expression, the optimization mark can be set to the first optimization mark or the second optimization mark, and the optimization operation corresponding to the first optimization mark and the second optimization mark can be to optimize the and expression to the equivalent expression; if the intersection of the condition string of the like expression and the condition string of the equivalent expression is empty, the optimization mark can be set to the third optimization mark, and the optimization operation corresponding to the third optimization mark can be to optimize the and expression to FALSE.
[0067] In this embodiment, the first target logical operator expression can be an or expression; accordingly, if the condition string of the like expression can contain the condition string of the equivalent expression, the optimization mark can be set to the first optimization mark or the second optimization mark; the optimization operation corresponding to the first optimization mark and the second optimization mark can be to optimize the or expression to a like expression; if the intersection of the condition string of the like expression and the condition string of the equivalent expression is empty, no optimization operation is performed.
[0068] It should be noted that if the where clause only includes the first sub-branch expression and the second sub-branch expression, the first optimized expression is the final optimization result, the optimization is terminated, and steps S150 to S160 are no longer executed.
[0069] It should be noted that if the first optimization expression is FLASE, the optimization is terminated and steps S150 to S160 are no longer executed.
[0070] S150. Obtain a third sub-branch expression from the structured query statement according to the priority of the logical operator. If the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, combine the first optimization expression and the third sub-branch expression into a second logical operator expression.
[0071] Among them, the method of obtaining the third sub-branch expression is the same as the method of obtaining the second sub-branch expression, which will not be repeated here.
[0072] In this embodiment, after optimizing and obtaining the first optimized expression, it is necessary to obtain the third sub-branch expression and combine it with the first optimized expression into a second logical operator expression to continue optimizing. S160, after determining that the second logical operator expression is the second target logical operator expression containing a like expression and an equivalent expression, repeat the above process until all sub-branch expressions in the structured query statement are optimized, and add all optimized expressions in the sub-branch optimization linked list to the sub-branch linked list.
[0073] In this embodiment, after performing an optimization operation, the optimized expression obtained after each optimization and the next sub-branch expression in the where clause that meets the optimization condition can be continuously optimized until all sub-branch expressions in the where clause are obtained.
[0074] Exemplarily, the where clause is nbexp1 and nbexp2 and nbexp3 or nbexp4. If the first optimization expression is nbexp1 and nbexp3 satisfies the second optimization condition, nbexp1 and nbexp3 are further optimized to obtain nbexp1, and nbexp1 and nbexp4 are combined into an or expression to continue optimization.
[0075] A method for optimizing a structured query statement provided in a first embodiment of the present invention comprises the following steps: first, obtaining a structured query statement, and obtaining a first sub-branch expression from the structured query statement according to the priority of a logical operator; secondly, if the first sub-branch expression satisfies a first optimization condition, obtaining a second sub-branch expression from the structured query statement according to the priority of a logical operator; then, if the second sub-branch expression satisfies a second optimization condition, combining the first sub-branch expression and the second sub-branch expression into a first logical operator expression; thereafter, after determining that the first logical operator expression is a first target logical operator expression including a like expression and an equivalent expression, determining the first target expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression. The optimization mark of the logical operator expression, and according to the optimization mark, the corresponding optimization operation is performed on the first target logical operator expression to obtain the first optimized expression; the third sub-branch expression is obtained from the structured query statement according to the priority of the logical operator, if the first optimized expression satisfies the first optimization condition, and the third sub-branch expression satisfies the second optimization condition, then the first optimized expression and the third sub-branch expression are combined into a second logical operator expression; finally, after determining that the second logical operator expression is the second target logical operator expression containing a like expression and an equivalent expression, the above process is repeated until all sub-branch expressions in the structured query statement are optimized, and all optimized expressions in the sub-branch optimization linked list are added to the sub-branch linked list. The above method can optimize the query conditions, effectively shorten the query time, and improve the query efficiency by optimizing the conditions of the structured query statement.
[0076] Further, the first target logical operator expression is an and expression. Accordingly, the optimization mark of the first target logical operator expression is determined according to the covering relationship between the condition string of the like expression and the condition string of the equivalent expression, including:
[0077] If the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the first optimization flag;
[0078] If the first sub-branch expression in the and expression is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the second optimization flag;
[0079] If the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization flag of the first target logical operator expression is set to a third optimization flag;
[0080] If the first sub-branch expression in the and expression that meets the first optimization condition is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization mark of the first target logical operator expression is set to the third optimization mark.
[0081] Exemplarily, the and expression is nbexp1 and nbexp2. If nbexp1 is an equal expression, nbexp2 is a like expression, and the condition string of nbexp2 can contain the condition string of nbexp1, the optimization flag can be set to USE_B1; if nbexp1 is a like expression, nbexp2 is an equal expression, and the condition string of nbexp1 can contain the condition string of nbexp2, the optimization flag can be set to USE_B2; if nbexp1 is an equal expression, nbexp2 is a like expression, and the intersection of the condition string of nbexp2 and the condition string of nbexp1 is empty, the optimization flag can be set to USE_B3; if nbexp1 is a like expression, nbexp2 is an equal expression, and the intersection of the condition string of nbexp1 and the condition string of nbexp2 is empty, the optimization flag can be set to USE_B3.
[0082] Based on the above technical solution, performing corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression includes:
[0083] If the optimization mark is the first optimization mark, the equivalent expression and the like expression in the and expression are optimized to the equivalent expression as the first optimization expression, and the like expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list;
[0084] If the optimization mark is the second optimization mark, the like expression and the equivalent expression in the and expression are optimized to the equivalent expression as the first optimization expression, and the like expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list;
[0085] If the optimization mark is the third optimization mark, the and expression is optimized to FALSE, and the like expression and the equivalent expression are removed from the sub-branch linked list, and FALSE is added to the sub-branch optimization linked list.
[0086] For example, if the where clause is nbexp1 and nbexp2 and nbexp3 or nbexp4, the sub-branch linked list consists of nbexp1, nbexp2, nbexp3, and nbexp4.
[0087] Exemplarily, if the and expression is nbexp1 and nbexp2, if nbexp1 is an equal expression, nbexp2 is a like expression, and the optimization mark is USE_B1, then the and expression is optimized to nbexp1, and nbexp2 is removed from the sub-branch linked list, and nbexp1 is added to the sub-branch optimization linked list; if nbexp1 is a like expression, nbexp2 is an equal expression, and the optimization mark is USE_B2, then the and expression is optimized to nbexp2, and nbexp1 is removed from the sub-branch linked list, and nbexp2 is added to the sub-branch optimization linked list.
[0088] Further, the first logical operator expression is an or expression. Accordingly, the optimization mark of the first target logical operator expression is determined according to the covering relationship between the condition string of the like expression and the condition string of the equivalent expression, including:
[0089] Determining the optimization mark of the first target logical operator expression according to the covering relationship between the conditional string of the like expression and the conditional string of the equivalent expression includes:
[0090] If the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the first optimization flag;
[0091] If the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the second optimization flag;
[0092] If the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization mark of the first target logical operator expression is set to the fourth optimization mark.
[0093] Exemplarily, the or expression is nbexp1 or nbexp2. If nbexp1 is a like expression, nbexp2 is an equal expression, and the condition string of nbexp1 can contain the condition string of nbexp2, the optimization flag can be set to USE_B1; if nbexp1 is an equal expression, nbexp2 is a like expression, and the condition string of nbexp2 can contain the condition string of nbexp1, the optimization flag can be set to USE_B2; if nbexp1 is a like expression, nbexp2 is an equal expression, and the intersection of the condition string of nbexp1 and the condition string of nbexp2 is empty, the optimization flag can be set to USE_B4.
[0094] Based on the above technical solution, performing corresponding optimization operations on the first target logical operator expression according to the optimization mark includes:
[0095] If the optimization mark is the first optimization mark, the like expression and the equivalent expression in the or expression are optimized to the like expression as the first optimization expression, and the equivalent expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list;
[0096] If the optimization mark is the second optimization mark, the equivalent expression and the like expression in the or expression are optimized to the like expression as the first optimization expression, and the equivalent expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list.
[0097] Exemplarily, the or expression is nbexp1 or nbexp2. If nbexp1 is a like expression, nbexp2 is an equal expression, and the optimization mark is USE_B1, the or expression is optimized to nbexp1, and nbexp2 is removed from the sub-branch linked list, and nbexp1 is added to the sub-molecule optimization linked list; if nbexp1 is an equal expression, nbexp2 is a like expression, and the optimization mark is USE_B2, the or expression is optimized to nbexp2, and nbexp1 is removed from the sub-branch linked list, and nbexp2 is added to the sub-molecule optimization linked list.
[0098] Furthermore, if the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, no optimization operation is performed.
[0099] An optimization method for a structured query statement provided by an embodiment of the present invention utilizes the relationship between a like expression and an equivalent expression to equivalently merge and simplify the two, thereby improving query efficiency.
[0100] Embodiment 2
[0101] Figure 2 This is a flow chart of a method for optimizing a structured query statement provided in the second embodiment of the present invention. The second embodiment is optimized on the basis of the above embodiments. For details not yet provided in this embodiment, please refer to the first embodiment.
[0102] like Figure 2 As shown, a method for optimizing a structured query statement provided by Embodiment 2 of the present invention includes the following steps:
[0103] S210: Obtain a structured query statement, and obtain a first sub-branch expression from the structured query statement according to the priority of the logical operator.
[0104] S220: If the first sub-branch expression satisfies a first optimization condition, obtaining a second sub-branch expression from the structured query statement according to the priority of the logical operator.
[0105] S230. If the second sub-branch expression satisfies a second optimization condition, combining the first sub-branch expression and the second sub-branch expression into a first logical operator expression.
[0106] S240. After determining that the first logical operator expression is the first target logical operator expression including a like expression and an equivalent expression, determine the optimization mark of the first target logical operator expression according to the overlapping relationship between the condition string of the like expression and the condition string of the equivalent expression, and perform corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression.
[0107] S250. Obtain a third sub-branch expression from the structured query statement according to the priority of the logical operator. If the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, combine the first optimization expression and the third sub-branch expression into a second logical operator expression.
[0108] S260. After determining that the second logical operator expression is the second target logical operator expression containing a like expression and an equal value expression, repeat the above process until all sub-branch expressions in the structured query statement are optimized, and add all optimized expressions in the sub-branch optimization linked list to the sub-branch linked list.
[0109] S270. If the first target logical operator does not perform an optimization operation, obtain a fourth sub-branch expression.
[0110] Among them, the method for obtaining the fourth sub-branch expression is the same as the method for obtaining the second sub-branch expression, which will not be repeated here.
[0111] S280. After determining that the fourth sub-branch expression satisfies the second optimization condition, combine the first sub-branch expression and the fourth sub-branch expression into a third logical operator expression.
[0112] S290. After determining that the third logical operator expression is the third target logical operator expression including a like expression and an equivalent expression, determine the optimization mark of the third target logical operator expression according to the overlapping relationship between the condition string of the like expression and the condition string of the equivalent expression, and perform corresponding optimization operations on the third target logical operator expression according to the optimization mark to obtain a third optimized expression.
[0113] Among them, the optimization process of the third target logical operator expression is the same as the optimization process of the first target logical operator expression, and will not be repeated here.
[0114] A method for optimizing a structured query statement provided in a second embodiment of the present invention can optimize the query conditions of the structured query statement and improve the query efficiency.
[0115] The embodiment of the present invention provides a specific implementation method based on the technical solutions of the above embodiments.
[0116] As a specific implementation of this embodiment, a table containing a string column is defined:
[0117]
[0118] 1) Execute the SQL statement: SELECT COUNT(1) FROM TEST WHERE C2 LIKE'%JING'AND C2='BEIJING';
[0119] If the condition string of the like expression in the SQL statement contains the condition string of the equal value expression, the AND statement in the query can be optimized to the equal value expression.
[0120] The execution plan before optimization is:
[0121]
[0122] The execution time is 10.734 seconds.
[0123] The optimized execution plan is:
[0124]
[0125] The execution time is 2.152 seconds.
[0126] By comparing the execution plans, we can see that the optimization can optimize the like conditions that can be covered, and the execution time will be greatly shortened, that is, the optimization can improve the optimization efficiency.
[0127] 2) Execute the SQL statement: SELECT COUNT(1) FROM TEST WHERE C2 LIKE'%JIN'AND C2='BEIJING';
[0128] If the intersection of the condition string of the like expression and the condition string of the equal value expression of the SQL statement is empty, the AND statement in the query can be optimized to FALSE.
[0129] The execution plan before optimization is:
[0130]
[0131] The execution time is 9.816 seconds.
[0132] The optimized execution plan is:
[0133]
[0134] The execution time is 2.173 milliseconds.
[0135] By comparing the execution plans, we can see that the query condition is optimized to FALSE, and the execution time is greatly shortened, that is, this optimization can improve the optimization efficiency.
[0136] Embodiment 3
[0137] Figure 3 A schematic diagram of the structure of an optimization device for structured query statements provided in Embodiment 3 of the present invention, wherein the device can be applied to situations where a database is queried using an SQL statement containing a like expression, wherein the device can be implemented by software and / or hardware and is generally integrated on an electronic device.
[0138] like Figure 3 As shown, the device includes: a first acquisition module 110 , a second acquisition module 120 , a first combination module 130 , a first optimization module 140 , a second combination module 150 and a second optimization module 160 .
[0139] A first acquisition module 110 is used to acquire a structured query statement, and acquire a first sub-branch expression from the structured query statement according to the priority of a logical operator;
[0140] A second acquisition module 120, configured to acquire a second sub-branch expression from the structured query statement according to the priority of a logical operator if the first sub-branch expression satisfies a first optimization condition;
[0141] A first combining module 130, configured to combine the first sub-branch expression and the second sub-branch expression into a first logical operator expression if the second sub-branch expression satisfies a second optimization condition;
[0142] A first optimization module 140 is used to determine that the first logical operator expression is a first target logical operator expression including a like expression and an equivalent expression, determine an optimization mark of the first target logical operator expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression, and perform corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression;
[0143] The second combination module 150 is used to obtain a third sub-branch expression from the structured query statement according to the priority of the logical operator. If the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, the first optimization expression and the third sub-branch expression are combined into a second logical operator expression.
[0144] The second optimization module 160 is used to determine that the second logical operator expression is a second target logical operator expression containing a like expression and an equal expression, and then repeat the above process until all sub-branch expressions in the structured query statement are optimized, and all optimized expressions in the sub-branch optimization linked list are added to the sub-branch linked list.
[0145] In this embodiment, the device first obtains a structured query statement through a first acquisition module 110, and obtains a first sub-branch expression from the structured query statement according to the priority of the logical operator; secondly, through a second acquisition module 120, if the first sub-branch expression satisfies a first optimization condition, then obtains a second sub-branch expression from the structured query statement according to the priority of the logical operator; through a first combination module 130, if the second sub-branch expression satisfies a second optimization condition, then combines the first sub-branch expression and the second sub-branch expression into a first logical operator expression; then, after determining through a first optimization module 140 that the first logical operator expression is a first target logical operator expression containing a like expression and an equivalent expression, the first is determined according to the covering relationship between the condition string of the like expression and the condition string of the equivalent expression. The optimization mark of the target logical operator expression is obtained, and the corresponding optimization operation is performed on the first target logical operator expression according to the optimization mark to obtain the first optimized expression; then, the third sub-branch expression is obtained from the structured query statement according to the priority of the logical operator through the second combination module 150, and if the first optimized expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, the first optimized expression and the third sub-branch expression are combined into a second logical operator expression; finally, after the second optimization module 160 determines that the second logical operator expression is the second target logical operator expression containing a like expression and an equal expression, the above process is repeated until all sub-branch expressions in the structured query statement are optimized, and all optimized expressions in the sub-branch optimization linked list are added to the sub-branch linked list.
[0146] This embodiment provides a structured query statement optimization device, which can improve query efficiency.
[0147] Furthermore, the first acquisition module 110 includes:
[0148] A generating unit, configured to generate a memory structure of the structured query statement after analyzing the structured query statement;
[0149] An acquisition unit is used to acquire at least two sub-branch expressions connected by the first and logical operator from the logical operator expression if the where clause in the memory structure is a logical operator expression, and the where clause includes both an and logical operator and an or logical operator, and acquire the sub-branch expression located at the leftmost side of the logical operator expression among the at least two sub-branch expressions as the first sub-branch expression.
[0150] Furthermore, the first optimization condition includes: the expression type is a like type or an equivalent type; the two sides of the expression are a column and a constant respectively; the second optimization condition includes: the expression type is a like type or an equivalent type; the two sides of the expression are a column and a constant respectively; the column name of the expression is the same as the column name of the sub-branch expression that meets the first optimization condition.
[0151] Furthermore, the device also includes: a determination module, which is used to obtain a second sub-branch expression from the structured query statement according to the priority of the logical operator if the first sub-branch expression does not meet the first optimization condition, and determine whether the second sub-branch expression meets the first optimization condition until a sub-branch expression that meets the first optimization condition is determined.
[0152] Furthermore, the first target logical operator expression satisfies that one of the first sub-branch expression and the second sub-branch expression is an equal expression and the other is a like expression.
[0153] Further, the first target logical operator expression is an and expression, and accordingly, the first optimization module 140 includes: a first setting submodule and a first optimization submodule.
[0154] Wherein, the first setting submodule includes:
[0155] a first setting unit, configured to set the optimization flag of the first target logical operator expression to a first optimization flag if the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression;
[0156] a second setting unit, configured to set the optimization flag of the first target logical operator expression to a second optimization flag if the first sub-branch expression in the and expression is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression;
[0157] a third setting unit, configured to set the optimization flag of the first target logical operator expression to a third optimization flag if the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty;
[0158] The fourth setting unit is used to set the optimization mark of the first target logical operator expression to the third optimization mark if the first sub-branch expression in the and expression that meets the first optimization condition is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty.
[0159] Based on the above technical solution, the first optimization submodule includes:
[0160] a first optimization unit, configured to optimize the equivalent expression and the like expression in the and expression into the equivalent expression as the first optimized expression if the optimization mark is the first optimization mark, remove the like expression from the sub-branch linked list, and add the first optimized expression to the sub-branch optimized linked list;
[0161] a second optimization unit, configured to optimize the like expression and the equivalent expression in the and expression into the equivalent expression as the first optimization expression if the optimization mark is the second optimization mark, and remove the like expression from the sub-branch linked list, and add the first optimization expression to the sub-branch optimization linked list;
[0162] The third optimization unit is used to optimize the and expression to FALSE if the optimization mark is the third optimization mark, and remove the like expression and the equivalent expression from the sub-branch linked list, and add FALSE to the sub-branch optimization linked list.
[0163] Further, the first logical operator expression is an or expression, and accordingly, the first optimization module 140 includes: a second setting submodule and a second optimization submodule.
[0164] Wherein, the second setting submodule includes:
[0165] a first setting unit, configured to set the optimization flag of the first target logical operator expression to a first optimization flag if the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression;
[0166] a second setting unit, configured to set the optimization flag of the first target logical operator expression to a second optimization flag if the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression;
[0167] The third setting unit is used to set the optimization mark of the first target logical operator expression to the fourth optimization mark if the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty.
[0168] Based on the above technical solution, the second optimization submodule includes:
[0169] a first optimization unit, configured to optimize the like expression and the equivalent expression in the or expression into the like expression as the first optimized expression if the optimization mark is the first optimization mark, remove the equivalent expression from the sub-branch linked list, and add the first optimized expression to the sub-branch optimized linked list;
[0170] The second optimization unit is used to optimize the equivalent expression and the like expression in the or expression into the like expression as the first optimization expression if the optimization mark is the second optimization mark, and remove the equivalent expression from the sub-branch linked list and add the first optimization expression to the sub-branch optimization linked list.
[0171] Furthermore, if the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, no optimization operation is performed.
[0172] Furthermore, the device also includes:
[0173] A third acquisition module, configured to acquire a fourth sub-branch expression if the first target logical operator does not perform an optimization operation;
[0174] A third combining module, configured to combine the first sub-branch expression and the fourth sub-branch expression into a third logical operator expression after determining that the fourth sub-branch expression satisfies the second optimization condition;
[0175] The third optimization module is used to determine that the third logical operator expression is a third target logical operator expression containing a like expression and an equivalent expression, determine the optimization mark of the third target logical operator expression according to the overlapping relationship between the condition string of the like expression and the condition string of the equivalent expression, and perform corresponding optimization operations on the third target logical operator expression according to the optimization mark to obtain a third optimized expression.
[0176] Furthermore, if the first optimization expression is FLASE, the optimization is terminated.
[0177] The above-mentioned structured query statement optimization device can execute the structured query statement optimization method provided by any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0178] Embodiment 4
[0179] Figure 4 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.
[0180] like Figure 4As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0181] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0182] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the optimization method of the structured query statement.
[0183] In some embodiments, the optimization method of structured query statements may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps in the optimization method of structured query statements described above may be executed. Alternatively, in other embodiments, the processor 11 may be configured to execute the optimization method of structured query statements in any other appropriate manner (e.g., by means of firmware).
[0184] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0185] In some embodiments, the optimization method of structured query statements can be implemented as a computer program, which is invisibly included in a computer program product. When the computer program is executed by a processor, the optimization method of the structured query statement of the present invention is implemented. The computer program product can be understood as a software product that mainly implements its solution through a computer program. The computer program for implementing the method of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program can be executed entirely on the machine, partially on the machine, as an independent software package, partially on the machine and partially on a remote machine, or completely on a remote machine or server.
[0186] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0187] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0188] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0189] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0190] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0191] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for optimizing structured query statements, characterized in that: The method comprises: Obtaining a structured query statement, and obtaining a first sub-branch expression from the structured query statement according to the priority of the logical operator; If the first sub-branch expression satisfies the first optimization condition, obtaining a second sub-branch expression from the structured query statement according to the priority of the logical operator; If the second sub-branch expression satisfies a second optimization condition, combining the first sub-branch expression and the second sub-branch expression into a first logical operator expression; After determining that the first logical operator expression is the first target logical operator expression including a like expression and an equivalent expression, determining an optimization mark of the first target logical operator expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression, and performing corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression; Obtaining a third sub-branch expression from the structured query statement according to the priority of the logical operator, and if the first optimization expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, combining the first optimization expression and the third sub-branch expression into a second logical operator expression; After determining that the second logical operator expression is the second target logical operator expression containing a like expression and an equal value expression, repeat the above process until all sub-branch expressions in the structured query statement are optimized, and add all optimized expressions in the sub-branch optimization linked list to the sub-branch linked list.
2. The method according to claim 1, characterized in that The obtaining the first sub-branch expression from the structured query statement according to the priority of the logical operator expression includes: After analyzing the structured query statement, a memory structure of the structured query statement is generated; If the where clause in the memory structure is a logical operator expression, and the where clause includes both an and logical operator and an or logical operator, then obtain at least two sub-branch expressions connected by the first and logical operator from the logical operator expression, and obtain the sub-branch expression located at the leftmost side of the logical operator expression among the at least two sub-branch expressions as the first sub-branch expression.
3. The method according to claim 1, characterized in that: The first optimization condition includes: the expression type is a like type or an equal value type; both sides of the expression are a column and a constant respectively; The second optimization condition includes: the expression type is a like type or an equal value type; the two sides of the expression are a column and a constant respectively; the column name of the expression is the same as the column name of the sub-branch expression that meets the first optimization condition.
4. The method according to claim 1 or 3, characterized in that: Also includes: If the first sub-branch expression does not meet the first optimization condition, obtain the second sub-branch expression from the structured query statement according to the priority of the logical operator, and judge whether the second sub-branch expression meets the first optimization condition until a sub-branch expression that meets the first optimization condition is determined.
5. The method according to claim 1, characterized in that The first target logical operator expression satisfies that one of the first sub-branch expression and the second sub-branch expression is an equal expression and the other is a like expression.
6. The method according to claim 1 or 5, characterized in that: The first target logical operator expression is an and expression, and accordingly, Determining the optimization mark of the first target logical operator expression according to the covering relationship between the conditional string of the like expression and the conditional string of the equivalent expression includes: If the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the first optimization flag; If the first sub-branch expression in the and expression is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the second optimization flag; If the first sub-branch expression in the and expression is an equal value expression, the second sub-branch expression in the and expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization flag of the first target logical operator expression is set to a third optimization flag; If the first sub-branch expression in the and expression that meets the first optimization condition is a like expression, the second sub-branch expression in the and expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization mark of the first target logical operator expression is set to the third optimization mark.
7. The method according to claim 6, characterized in that Performing a corresponding optimization operation on the first target logical operator expression according to the optimization mark to obtain a first optimized expression includes: If the optimization mark is the first optimization mark, the equivalent expression and the like expression in the and expression are optimized to the equivalent expression as the first optimization expression, and the like expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list; If the optimization mark is the second optimization mark, the like expression and the equivalent expression in the and expression are optimized to the equivalent expression as the first optimization expression, and the like expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list; If the optimization mark is the third optimization mark, the and expression is optimized to FALSE, and the like expression and the equivalent expression are removed from the sub-branch linked list, and FALSE is added to the sub-branch optimization linked list.
8. The method according to claim 1, characterized in that The first logical operator expression is an or expression, and accordingly, Determining the optimization mark of the first target logical operator expression according to the covering relationship between the conditional string of the like expression and the conditional string of the equivalent expression includes: If the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the first optimization flag; If the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the condition string of the like expression contains the condition string of the equal value expression, then the optimization flag of the first target logical operator expression is set to the second optimization flag; If the first sub-branch expression in the or expression is a like expression, the second sub-branch expression in the or expression is an equal value expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, then the optimization mark of the first target logical operator expression is set to the fourth optimization mark.
9. The method according to claim 8, characterized in that Performing a corresponding optimization operation on the first target logical operator expression according to the optimization mark includes: If the optimization mark is the first optimization mark, the like expression and the equivalent expression in the or expression are optimized to the like expression as the first optimization expression, and the equivalent expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list; If the optimization mark is the second optimization mark, the equivalent expression and the like expression in the or expression are optimized to the like expression as the first optimization expression, and the equivalent expression is removed from the sub-branch linked list, and the first optimization expression is added to the sub-branch optimization linked list.
10. The method according to claim 9, characterized in that Also includes: If the first sub-branch expression in the or expression is an equal value expression, the second sub-branch expression in the or expression is a like expression, and the intersection of the condition string of the like expression and the condition string of the equal value expression is empty, no optimization operation is performed.
11. The method according to claim 1, characterized in that: The method further comprises: If the first target logical operator does not perform an optimization operation, obtaining a fourth sub-branch expression; After determining that the fourth sub-branch expression satisfies the second optimization condition, combining the first sub-branch expression and the fourth sub-branch expression into a third logical operator expression; After determining that the third logical operator expression is the third target logical operator expression containing a like expression and an equivalent expression, the optimization mark of the third target logical operator expression is determined according to the overlapping relationship between the condition string of the like expression and the condition string of the equivalent expression, and the corresponding optimization operation is performed on the third target logical operator expression according to the optimization mark to obtain the third optimized expression.
12. The method according to claim 1, characterized in that The method further comprises: If the first optimization expression is FLASE, the optimization is terminated.
13. A structured query statement optimization device, characterized in that: The device comprises: A first acquisition module, used to acquire a structured query statement, and acquire a first sub-branch expression from the structured query statement according to the priority of a logical operator; A second acquisition module, configured to acquire a second sub-branch expression from the structured query statement according to the priority of a logical operator if the first sub-branch expression satisfies a first optimization condition; a first combining module, configured to combine the first sub-branch expression and the second sub-branch expression into a first logical operator expression if the second sub-branch expression satisfies a second optimization condition; A first optimization module is used to determine that the first logical operator expression is a first target logical operator expression including a like expression and an equivalent expression, determine an optimization mark of the first target logical operator expression according to an overlapping relationship between a condition string of the like expression and a condition string of the equivalent expression, and perform corresponding optimization operations on the first target logical operator expression according to the optimization mark to obtain a first optimized expression; a second combining module, configured to obtain a third sub-branch expression from the structured query statement according to the priority of the logical operator, and if the first optimized expression satisfies the first optimization condition and the third sub-branch expression satisfies the second optimization condition, then combining the first optimized expression and the third sub-branch expression into a second logical operator expression; The second optimization module is used to determine that the second logical operator expression is a second target logical operator expression containing a like expression and an equal expression, and then repeat the above process until all sub-branch expressions in the structured query statement are optimized, and all optimized expressions in the sub-branch optimization linked list are added to the sub-branch linked list.
14. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively coupled to the at least one processor; The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the method for optimizing structured query statements according to any one of claims 1 to 12.
15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the method for optimizing a structured query statement according to any one of claims 1 to 12 when the processor executes the instructions.
16. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the computer program implements the method for optimizing the structured query statement according to any one of claims 1 to 12.
Citation Information
Cited By
Filtering condition rewriting method, device, equipment, medium and product
CN121455995A