Query optimization method, storage medium and computer equipment
By traversing the constraints of query statements in the database, constructing constant relations and deriving table connection conditions, the problem of Cartesian product in multi-table connections is solved, and query performance and database efficiency are improved.
Patent Information
- Application Number
- CN202410823021.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-24
- Publication Date
- 2024-10-08
AI Technical Summary
In multi-table join queries, the lack of join conditions leads to the formation of Cartesian products, resulting in long query times and low execution efficiency.
By traversing the constraints in the query statement, collect conditions that match the constant relationship, construct the constant relationship, and deduce the connection conditions between tables to determine whether these conditions need to be used for optimization. If necessary, execute the query to eliminate the Cartesian product , to achieve non-equivalent connections.
It significantly improves the execution efficiency of query statements, improves query performance, avoids the problems of long query time and low execution efficiency caused by Cartesian product, and thus improves the performance of the database.
Smart Images

Figure CN118747183A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to database technology, and in particular to a query optimization method, a storage medium and a computer device. Background Art
[0002] At present, when the optimizer processes multiple tables and there is a lack of connection conditions between the multiple tables, the multi-table connection will be connected in the form of Cartesian product. For example: when the optimizer processes the query statement "SELECT*FROM T1,T2 WHERE T1.a>5and T2.a<3;", most optimizers on the market usually push down the constraints T1.a>5 of the T1 table and T2.a<3 of the T2 table to the base table first, and filter tuples when scanning the T1 and T2 tables. Due to the lack of connection conditions between the T1 and T2 tables, the two tables are connected using the Cartesian product, that is, the Cartesian product of the T1 and T2 tables is taken.
[0003] However, in the above query scenario, if the data size of the table to be connected is large, it will lead to long query time and low execution efficiency. Therefore, how to eliminate the Cartesian product when connecting the tables and make the two tables non-equivalently connected to improve query performance has become an urgent problem to be solved. Summary of the invention
[0004] In view of the above problems, the present invention proposes a query optimization method, a storage medium and a computer device that overcome the above problems or at least partially solve the above problems.
[0005] One object of the present invention is to eliminate the Cartesian product, thereby improving the execution efficiency of the query statement.
[0006] A further object of the present invention is to improve the accuracy of connection condition derivation.
[0007] In particular, the present invention provides a query optimization method, which comprises:
[0008] Traverse the constraints in the query statement and collect the constraints that meet the requirements for connection condition derivation based on constant relations into the set to be processed;
[0009] Extract constants from the set to be processed, and construct constant relational expressions based on the connection relationships between the constants;
[0010] Derives the join conditions between multiple tables involved in the query statement based on constant relational expressions;
[0011] Determine whether to use the derived join condition based on the join cost between multiple tables;
[0012] If necessary, the query statement is executed based on the inferred join condition.
[0013] Optionally, extracting constants from the set to be processed and constructing a constant relational expression according to the connection relationship between the constants includes:
[0014] Respectively obtain multiple constants in multiple constraint conditions for different tables in the set to be processed;
[0015] According to the size relationship between multiple constants, a constant relational expression consisting of multiple constants and operators is constructed.
[0016] Optionally, the connection conditions between multiple tables involved in the query statement derived based on the constant relational expression include:
[0017] The constant relational expression and the constraint conditions where the multiple constants in the constant relational expression are located are combined to construct the constraint condition relational expression;
[0018] The constraint condition relational expressions are derived based on the transitivity rule to form the connection conditions.
[0019] Optionally, the connection conditions between multiple tables involved in the query statement derived based on the constant relational expression include:
[0020] The constant relational expression and the constraint conditions where the multiple constants in the constant relational expression are located are combined to construct the constraint condition relational expression;
[0021] The constraint condition relational expressions are derived based on the transitivity rule to form the connection conditions.
[0022] Optionally, traversing the constraints in the query statement and collecting the constraints that meet the requirements for deducing the join conditions based on the constant relationship into a to-be-processed set includes:
[0023] Get the constraints in the query statement one by one;
[0024] According to the type and content of the constraint conditions, determine whether the obtained constraint conditions meet the requirements for connection condition derivation based on constant relationships;
[0025] The constraint conditions that meet the requirements for connection condition derivation based on constant relations are stored in a to-be-processed set.
[0026] Optionally, judging whether the acquired constraint condition meets the requirement of connection condition derivation based on the constant relationship according to the type and content of the constraint condition includes:
[0027] Determine whether the obtained constraint condition has a binary expression, does not contain a volatile function, does not contain a subquery, and is composed of column attributes, operators, and constants;
[0028] When the above judgment results are all yes, it is determined that the obtained constraint conditions meet the requirements for deducing connection conditions based on constant relationships.
[0029] Optionally, judging whether to use the derived join condition according to the join cost between the multiple tables includes:
[0030] Judging whether to use the derived join conditions based on the join costs between multiple tables includes:
[0031] Determine whether there are original connection conditions between multiple tables in the query statement;
[0032] If not, it is determined that the derived join condition needs to be used;
[0033] If so, perform an execution cost estimation and determine whether the execution cost of the derived join condition is less than the execution cost of the original join condition;
[0034] If it is less than, it is determined that the derived connection condition needs to be used;
[0035] If not, it is determined that the inferred join condition does not need to be used.
[0036] According to another aspect of the present invention, a machine-readable storage medium is provided, on which a machine-executable program is stored. When the machine-executable program is executed by a processor, any one of the above-mentioned query optimization methods is implemented.
[0037] According to another aspect of the present invention, a computer device is provided, comprising a memory, a processor, and a machine executable program stored in the memory and running on the processor, and the processor implements any of the above query optimization methods when executing the machine executable program.
[0038] The query optimization method of the present invention collects the constraints that meet the requirements of connection condition derivation based on constant relations into a to-be-processed set by traversing the constraints in the query statement, extracts the constants in the to-be-processed set, and constructs a constant relational expression according to the connection relationship between the constants, and derives the connection conditions between multiple tables involved in the query statement based on the constant relational expression, thereby realizing the derivation of connection conditions based on constant relations under specific conditions. In addition, the query optimization method of the present invention determines whether it is necessary to use the derived connection conditions according to the connection costs between multiple tables, and executes the query statement based on the derived connection conditions when the derived connection conditions are needed, thereby eliminating the Cartesian product operation performed when multiple tables are connected, making multiple tables non-equivalent connections, improving the execution efficiency of the query statement, thereby significantly improving the query performance, avoiding the problem of long query time and low execution efficiency due to the use of Cartesian products for table connection, and thus improving the performance of the database.
[0039] Furthermore, the query optimization method of the present invention, by respectively obtaining multiple constants in multiple constraint conditions for different tables in the to-be-processed set, and constructing a constant relational expression composed of multiple constants and operators according to the size relationship between the multiple constants, realizes the construction of the constant relational expression according to the connection relationship between the constants, ensures the fast and accurate generation of the constant relational expression, thereby improving the execution accuracy of the subsequent connection condition derivation, and further ensuring the performance of the database.
[0040] Based on the following detailed description of specific embodiments of the present invention in conjunction with the accompanying drawings, those skilled in the art will become more aware of the above and other objects, advantages and features of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] Hereinafter, some specific embodiments of the present invention will be described in detail in an exemplary and non-limiting manner with reference to the accompanying drawings. The same reference numerals in the accompanying drawings indicate the same or similar components or parts. It should be understood by those skilled in the art that these drawings are not necessarily drawn to scale. In the accompanying drawings:
[0042] Figure 1 is a schematic diagram of a query optimization method according to an embodiment of the present invention;
[0043] Figure 2 is a flow chart of a query optimization method according to an embodiment of the present invention;
[0044] Figure 3 is a schematic diagram of a machine-readable storage medium according to an embodiment of the present invention; and
[0045] Figure 4 is a schematic diagram of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0046] The exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to enable the scope of the present invention to be fully communicated to those skilled in the art.
[0047] Figure 1 FIG. 1 is a schematic diagram of a query optimization method according to an embodiment of the present invention. Figure 1 As shown, the query optimization method may generally include:
[0048] Step S102, traverse the constraints in the query statement, and collect the constraints that meet the requirements for connection condition derivation based on constant relations into a set to be processed. It should be noted that constants may include constants, and constant relations may refer to natural relations between constants. For example, there is a natural connection relationship 5>3 between constants 5 and 3. Further, the set to be processed may be pre-set in the database for all constraints that meet the requirements for connection condition derivation based on constant relations. In addition, the connection condition derivation based on constant relations requires to be pre-set and stored in the database, which is used to determine whether the constraints can be deduced based on constant relations.
[0049] Step S104, extract the constants in the set to be processed, and construct a constant relational expression according to the connection relationship between the constants. In this step, the connection relationship between the constants can be a natural relationship between the constants. For example, there is a natural relationship of 5>3 between constant 5 and constant 3, so the connection relationship between constant 5 and constant 3 can be 5>3.
[0050] Step S106, deriving the connection conditions between multiple tables involved in the query statement based on the constant relational expression. Specifically, this step can be specifically performed as follows: combining the constant relational expression and the constraint conditions where the constants in the constant relational expression are located to derive the connection conditions between multiple tables involved in the query statement.
[0051] Step S108, judging whether the derived connection condition needs to be used according to the connection cost between the multiple tables, and if so, executing step S110. That is, after the connection condition is derived based on the constant relationship, the query optimization method of the embodiment of the present invention further judges whether the derived connection condition needs to be used, and optimizes only the query statements that need to use the derived connection condition in a targeted manner, thereby avoiding performance waste caused by useless operations.
[0052] Step S110, executing the query statement based on the derived connection condition. Specifically, this step can be specifically performed as: generating an execution plan by using the derived connection condition as the connection condition between multiple tables; and executing the query statement according to the execution plan.
[0053] The query optimization method of the embodiment of the present invention, by traversing the constraints in the query statement, collects the constraints that meet the requirements of the connection condition derivation based on the constant relationship into the to-be-processed set, extracts the constants in the to-be-processed set, and constructs a constant relational expression according to the connection relationship between the constants, and derives the connection conditions between multiple tables involved in the query statement based on the constant relational expression, thereby realizing the derivation of the connection conditions based on the constant relationship under specific conditions. In addition, the query optimization method of the present invention determines whether it is necessary to use the derived connection conditions according to the connection costs between multiple tables, and executes the query statement based on the derived connection conditions when it is necessary to use the derived connection conditions, thereby eliminating the Cartesian product operation performed when multiple tables are connected, making multiple tables non-equivalent connections, improving the execution efficiency of the query statement, thereby significantly improving the query performance, avoiding the problem of long query time and low execution efficiency due to the use of Cartesian products for table connection, and thus improving the performance of the database.
[0054] In some embodiments, the above step S102 may include the following steps: obtaining the constraints in the query statement one by one; judging whether the obtained constraints meet the requirements for deriving the connection conditions based on the constant relationship according to the type of the constraints and the content contained therein; and storing the constraints that meet the requirements for deriving the connection conditions based on the constant relationship in the to-be-processed set. It should be noted that the content contained in the constraints may include binary expressions, volatile functions, and subqueries, etc. The type of the constraints may refer to the type of the filter condition or the query condition in the constraints.
[0055] The query optimization method of the embodiment of the present invention obtains the constraint conditions in the query statement one by one, and judges one by one whether the obtained constraint conditions meet the requirements for deriving connection conditions based on constant relations according to the type of constraint conditions and the contents contained therein, thereby achieving accurate and comprehensive acquisition of all constraint conditions in the query statement that meet the requirements for deriving connection conditions based on constant relations, thereby further improving the accuracy of query optimization.
[0056] In some embodiments, the step of obtaining the constraints in the query statement one by one may include the following steps: traversing the clauses in the query statement; if there is a subquery, recursively traversing into the subquery; until all the constraints in the query statement are traversed. It should be noted that the clauses in the query statement may include a WHERE clause and an ON clause.
[0057] The query optimization method of the embodiment of the present invention realizes the one-by-one acquisition of all constraints in the query statement by traversing the query, thereby ensuring the comprehensiveness of the constraint acquisition, facilitating the accurate and comprehensive acquisition of all constraints in the query statement that meet the requirements for deriving connection conditions based on constant relationships in subsequent operations, and further ensuring the accuracy of query optimization.
[0058] In some embodiments, the above step of judging whether the obtained constraints meet the requirements for deriving join conditions based on constant relationships based on the types of constraints and the contents contained therein may include the following steps: judging whether the obtained constraints have binary expressions, whether they do not contain volatile functions, whether they do not contain subqueries, and whether they are composed of column attributes, operators, and constants; if the above judgment results are all yes, it is determined that the obtained constraints meet the requirements for deriving join conditions based on constant relationships.
[0059] It should be noted that binary expressions are composed of variables and constants. For example, if the constraint in the query statement for table T1 contains T1.a>3, it can be determined that the constraint contains a binary expression. Volatile functions refer to functions that will be recalculated as long as there are any changes in the workbook or worksheet, even if these changes are unrelated to the input data of the function, such as the function RAND() that returns a random number, the function NOW() that returns the current date and time, etc. In other words, the result of each execution of a volatile function is uncertain. Based on this, the constraints containing volatile functions lack a stable constant relationship. Therefore, there is no need to collect constraints containing volatile functions. Because subqueries may introduce additional performance overhead and potential non-deterministic results, there is no need to collect constraints containing subqueries.
[0060] In addition, the above step of determining whether the acquired constraint condition consists of a column attribute, an operator and a constant can also be executed as the following steps: Check whether the constraint condition is of the type of <column attribute op constant>. op refers to an operator, which is the abbreviation of operator.
[0061] Further, in a specific embodiment, the above-mentioned step of determining whether the obtained constraint condition has a binary expression, whether it does not contain a volatile function, whether it does not contain a subquery, and whether it is composed of column attributes, operators, and constants can be specifically performed by sequentially determining whether the obtained constraint condition has a binary expression, whether it does not contain a volatile function, whether it does not contain a subquery, and whether it is composed of column attributes, operators, and constants, thereby improving the accuracy of the determination.
[0062] In another specific embodiment, the judgment order in the above steps of judging whether the obtained constraint condition has a binary expression, whether it does not contain a volatile function, whether it does not contain a subquery, and whether it is composed of column attributes, operators, and constants can be freely adjusted according to needs, thereby improving the judgment flexibility and efficiency.
[0063] The query optimization method of the embodiment of the present invention achieves accurate and comprehensive acquisition of all constraint conditions in the query statement that meet the requirements of connection condition derivation based on constant relations by adding the logical optimization rules of the optimizer, thereby further ensuring the accuracy of query optimization.
[0064] In some embodiments, the above step S104 includes the following steps: respectively obtaining multiple constants in multiple constraints for different tables in the set to be processed; constructing a constant relational expression consisting of multiple constants and operators according to the size relationship between the multiple constants. In other words, the connection relationship between the constants includes the size relationship between the constants. The constant relational expression consists of multiple constants and operators. For example, there is a natural connection relationship 5>3 between constant 5 and constant 3, and the constant relational expression consisting of constant 5, constant 3 and operators is 5>3.
[0065] The query optimization method of the embodiment of the present invention obtains multiple constants in multiple constraint conditions for different tables in the to-be-processed set respectively, and constructs a constant relational expression composed of multiple constants and operators according to the size relationship between the multiple constants, thereby realizing the construction of the constant relational expression according to the connection relationship between the constants, ensuring the fast and accurate generation of the constant relational expression, thereby improving the execution accuracy of the subsequent connection condition derivation, and further ensuring the performance of the database.
[0066] In some embodiments, the above step S106 includes the following steps: combining the constant relational expression with the constraints where the constants in the constant relational expression are located to construct the constraint relational expression; and deriving the constraint relational expression based on the transitivity rule to form a connection condition. In other words, the above step S106 can be specifically performed as follows: combining the constant relational expression with the constraints where the constants in the constant relational expression are located to derive the connection conditions between the multiple tables involved in the query statement.
[0067] For example, for the first constraint condition T1.a>5 involving the T1 table and the second constraint condition T2.a<3 involving the T2 table, after the step of constructing the constant relation formula 5>3 according to the connection relationship 5>3 between constant 5 and constant 3, first merge the constant relation formula 5>3 with the first constraint condition T1.a>5 and the second constraint condition T2.a<3 involving the T2 table to construct the constraint relation formula T1.a>5>3>T2.a, and then derive the constraint relation formula T1.a>5>3>T2.a based on the transitivity rule to form the connection condition T1.a>T2.a.
[0068] The query optimization method of the embodiment of the present invention achieves accurate derivation of join conditions based on the natural relationship between constants by refining the derivation process of join conditions, can eliminate Cartesian products, provides a join method with lower execution cost, and further ensures the performance of the database.
[0069] In some embodiments, the above step S108 may include the following steps: determine whether there is an original connection condition between multiple tables in the query statement; if not, determine that the derived connection condition needs to be used; if so, perform an execution cost estimation, and determine whether the execution cost of the derived connection condition is less than the execution cost of the original connection condition; if it is less, determine that the derived connection condition needs to be used; if not, determine that the derived connection condition does not need to be used.
[0070] That is to say, first determine whether there is a connection condition between the two tables. If there is no connection condition, directly take the derived constraint condition as the connection condition of the two tables to generate an execution plan. If there is a connection condition, the query statement composed of the original connection condition needs to be handed over to the physical optimization module to generate the first execution plan, and then the derived connection condition replaces the original connection condition between the two tables to form a new query statement and hand it over to the physical optimization module to generate the second execution plan. Compare the first execution plan and the second execution plan, and take the connection condition corresponding to the execution plan with the lowest execution cost as the connection condition for the final execution.
[0071] The query optimization method of the embodiment of the present invention selects the execution plan with the lowest execution cost by comparing the connection costs of different connection schemes, provides a connection method with lower execution cost, and further improves the performance of the database.
[0072] In some embodiments, the above step S110 may include the following steps: using the derived connection condition as the connection condition between multiple tables to generate an execution plan. Thus, the query optimization method of the embodiment of the present invention realizes the equivalent logical transformation of the query statement, provides a connection method with lower execution cost, and further ensures the performance of the database.
[0073] In a specific embodiment, taking the query statement "SELECT * FROM T1, T2 WHERE T1.a>5and T2.a<3;" as an example, the query optimization solution is as follows:
[0074] 1. Traverse the WHERE clause and ON clause in the query statement. If there is a subquery, recursively traverse the subquery until all constraints in the query statement are traversed, and make the following judgments on the constraints to collect the constraints that meet the requirements. Specifically, step 1 includes:
[0075] (1) Check whether the constraint condition is composed of binary expressions, such as T1.a>5 and T2.a<3; if so, execute the following step (2);
[0076] (2) Check whether the constraint condition does not contain a volatile function. If so, execute the following step (3);
[0077] (3) Check whether the constraint condition does not contain a subquery. If so, execute the following step (4);
[0078] (4) Check whether the constraint is of the <column attribute op constant> type. For example, T1.a>5 and T2.a<3 are both constraints of the <column attribute op constant> type.
[0079] (5) Collect the constraints that meet the above requirements into a set to be processed.
[0080] 2. Extract all constants in the set to be processed, including the first constant 5 and the second constant 3, and construct the constant relation 5>3 through the naturally existing connection relationship 5>3 between the constants.
[0081] 3. Derivation of connection conditions based on the constant relation 5>3. Specifically, step 3 includes:
[0082] (1) Combine the constant relation 5>3 and the constraints T1.a>5 and T2.a<3 in the query statement to construct a complete constraint relation T1.a>5>3>T2.a;
[0083] (2) Based on the transitivity rule, the constraint condition relation T1.a>5>3>T2.a is derived to form a new connection condition T1.a>T2.a.
[0084] 4. Determine whether the derived connection condition needs to be used based on the connection cost between T1 and T2. Specifically, step 4 includes:
[0085] (1) Determine whether there is a join condition between the two tables. If so, execute the following step (2). If not, execute the following step (3).
[0086] (2) The query statement composed of the original connection condition is submitted to the physical optimization module to generate a first execution plan, and then the query statement formed by the connection condition between the original two tables is replaced by the derived connection condition and submitted to the physical optimization module to generate a second execution plan. The first execution plan and the second execution plan are compared, and the connection condition corresponding to the execution plan with the lowest execution cost is taken as the connection condition for the final execution;
[0087] (3) The derived constraint conditions are taken as the join conditions of the two tables to generate an execution plan.
[0088] By using the above query optimization strategy, the query optimization method of the present invention realizes the derivation of connection conditions based on constant relationships under specific conditions, and executes query statements based on the derived connection conditions when the derived connection conditions are needed, thereby eliminating the Cartesian product operation performed when multiple tables are connected, making multiple tables non-equivalued connections, and improving the execution efficiency of query statements, thereby significantly improving query performance, avoiding the problems of long query time and low execution efficiency due to the use of Cartesian products for table connections, and further improving the performance of the database.
[0089] Figure 2 FIG. 1 is a flowchart of a query optimization method according to another embodiment of the present invention. Figure 2 As shown, the process of the query optimization method of the embodiment of the present invention may include the following steps:
[0090] Step S202, traverse the constraints in the query statement.
[0091] Step S204, determine whether the constraint condition has a binary expression, if so, execute step S206, if not, execute step S214.
[0092] Step S206, determining whether the constraint condition does not include a volatile function, if so, executing step S208, if not, executing step S214;
[0093] Step S208, determining whether the constraint condition does not contain a subquery, if so, executing step S210, if not, executing step S214.
[0094] Step S210, determining whether the constraint condition consists of column attributes, operators and constants, if so, executing step S212, if not, executing step S214.
[0095] Step S212, collecting the acquired constraint conditions into a to-be-processed set.
[0096] Step S214, determine whether the query statement traversal is complete, if so, execute step S216, if not, return to step S202.
[0097] Step S216, extracting constants from the set to be processed, and constructing a constant relational expression according to the connection relationship between the constants.
[0098] Step S218, deriving the connection conditions between the multiple tables involved in the query statement based on the constant relational expression.
[0099] Step S220, determining whether there is a connection condition between the multiple tables involved in the query statement, if so, executing step S222, if not, executing step S224.
[0100] Step S222: perform execution cost estimation, and select the connection condition with the minimum execution cost as the connection condition for final execution. At this point, this process ends.
[0101] Step S224, select the derived connection condition as the connection condition for final execution. At this point, this process ends.
[0102] After step S222 and step S224, there may be a step of continuing the remaining optimization work of the optimizer. When a query statement to be optimized is received again, the current process is restarted.
[0103] The query optimization method of the embodiment of the present invention, by traversing the constraints in the query statement, collects the constraints that meet the requirements of the connection condition derivation based on the constant relationship into the to-be-processed set, extracts the constants in the to-be-processed set, and constructs a constant relational expression according to the connection relationship between the constants, and derives the connection conditions between multiple tables involved in the query statement based on the constant relational expression, thereby realizing the derivation of the connection conditions based on the constant relationship under specific conditions. In addition, the query optimization method of the present invention determines whether it is necessary to use the derived connection conditions according to the connection costs between multiple tables, and executes the query statement based on the derived connection conditions when it is necessary to use the derived connection conditions, thereby eliminating the Cartesian product operation performed when multiple tables are connected, making multiple tables non-equivalent connections, improving the execution efficiency of the query statement, thereby significantly improving the query performance, avoiding the problem of long query time and low execution efficiency due to the use of Cartesian products for table connection, and thus improving the performance of the database.
[0104] Furthermore, the query optimization method of the embodiment of the present invention, by respectively obtaining multiple constants in multiple constraint conditions for different tables in the to-be-processed set, and constructing a constant relational expression composed of multiple constants and operators according to the size relationship between the multiple constants, realizes the construction of the constant relational expression according to the connection relationship between the constants, ensures the fast and accurate generation of the constant relational expression, thereby improving the execution accuracy of the subsequent connection condition derivation, and further ensuring the performance of the database.
[0105] This embodiment also provides a machine-readable storage medium and a computer device. Figure 3 is a schematic diagram of a machine-readable storage medium 10 according to an embodiment of the present invention, Figure 4 is a schematic diagram of a computer device 20 according to one embodiment of the present invention.
[0106] The machine-readable storage medium 10 stores a machine-executable program 11 , and when the machine-executable program 11 is executed by a processor, the query optimization method of any of the above embodiments can be implemented.
[0107] The computer device 20 may include a memory 220, a processor 210, and a machine executable program 11 stored in the memory 220 and running on the processor 210, and the processor 210 implements the query optimization method of any of the above embodiments when executing the machine executable program 11.
[0108] It should be noted that the logic and / or steps represented in the flowchart or described in other ways herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be specifically implemented in any machine-readable storage medium for use by an instruction execution system, device or equipment (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or equipment and execute instructions), or used in combination with these instruction execution systems, devices or equipment.
[0109] For the description of this embodiment, the machine-readable storage medium 10 can be any device that can contain, store, communicate, propagate or transmit a program for use with an instruction execution system, device or equipment or in conjunction with these instruction execution systems, devices or equipment. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection portion with one or more wirings (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and editable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the machine-readable storage medium 10 can even be paper or other suitable media on which the program can be printed, because the program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, interpreting or processing in other suitable ways as necessary, and then stored in a computer memory.
[0110] It should be understood that each part of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system.
[0111] The computer device 20 may be, for example, a server, a desktop computer, a notebook computer, a tablet computer, or a smart phone. In some examples, the computer device 20 may be a cloud computing node. The computer device 20 may be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, a program module may include routines, programs, target programs, components, logic, data structures, etc. that perform specific tasks or implement specific abstract data types. The computer device 20 may be implemented in a distributed cloud computing environment where remote processing devices linked through a communication network perform tasks. In a distributed cloud computing environment, program modules may be located on a local or remote computing system storage medium including a storage device.
[0112] The computer device 20 may include a processor 210 adapted to execute stored instructions, and a memory 220 providing temporary storage space for the operation of the instructions during operation. The processor 210 may be a single-core processor, a multi-core processor, a computing cluster, or any number of other configurations. The memory 220 may include a random access memory (RAM), a read-only memory, a flash memory, or any other suitable storage system.
[0113] The processor 210 may be connected to an I / O interface (input / output interface) suitable for connecting the computer device 20 to one or more I / O devices (input / output devices) through a system interconnect (e.g., PCI, PCI-Express, etc.). The I / O devices may include, for example, a keyboard and a pointing device, wherein the pointing device may include a touch pad or a touch screen, etc. The I / O devices may be built-in components of the computer device 20, or may be devices externally connected to the computing device.
[0114] The processor 210 may also be linked to a display interface suitable for connecting the computer device 20 to a display device through a system interconnect. The display device may include a display screen as a built-in component of the computer device 20. The display device may also include a computer monitor, a television, or a projector, etc., which are externally connected to the computer device 20. In addition, a network interface controller (NIC) may be suitable for connecting the computer device 20 to a network through a system interconnect. In some embodiments, the NIC may use any suitable interface or protocol (such as an Internet small computer system interface, etc.) to transmit data. The network may be a cellular network, a radio network, a wide area network (WAN), a local area network (LAN), or the Internet, etc. A remote device may be connected to the computing device through a network.
[0115] The flow chart provided by the present embodiment is not intended to indicate that the operation of the method will be performed in any particular order, or that all operations of the method are included in all every case. In addition, the method may include additional operations. Within the scope of the technical thinking provided by the present embodiment method, additional changes may be made to the above method.
[0116] At this point, those skilled in the art should recognize that, although multiple exemplary embodiments of the present invention have been shown and described in detail herein, many other variations or modifications that conform to the principles of the present invention can still be directly determined or derived based on the content disclosed in the present invention without departing from the spirit and scope of the present invention. Therefore, the scope of the present invention should be understood and recognized as covering all these other variations or modifications.
Claims
1. A query optimization method, include: Traverse the constraints in the query statement and collect the constraints that meet the requirements for connection condition derivation based on constant relations into the set to be processed; Extracting constants from the set to be processed, and constructing a constant relational expression according to the connection relationship between the constants; Deducing connection conditions between multiple tables involved in the query statement based on the constant relational expression; Determining whether it is necessary to use the derived connection condition according to the connection costs between the multiple tables; If necessary, the query statement is executed based on the derived connection condition.
2. The query optimization method according to claim 1, in, Extracting constants from the set to be processed and constructing a constant relational expression according to the connection relationship between the constants includes: Respectively obtaining multiple constants in multiple constraint conditions for different tables in the set to be processed; According to the size relationship between the multiple constants, the constant relationship expression consisting of the multiple constants and operators is constructed.
3. The query optimization method according to claim 2, in, Deducing the connection conditions between the multiple tables involved in the query statement based on the constant relational expression includes: Combining the constant relational expression with the constraint conditions where the multiple constants in the constant relational expression are located to construct a constraint condition relational expression; The constraint condition relational expression is derived based on the transitivity rule to form the connection condition.
4. The query optimization method according to claim 3, in, Deducing the connection conditions between the multiple tables involved in the query statement based on the constant relational expression includes: Combining the constant relational expression with the constraint conditions where the multiple constants in the constant relational expression are located to construct a constraint condition relational expression; The constraint condition relational expression is derived based on the transitivity rule to form the connection condition.
5. The query optimization method according to claim 1, in, Traverse the constraints in the query statement and collect the constraints that meet the requirements for connection condition derivation based on constant relations into the set to be processed, including: Obtaining constraints in the query statement one by one; According to the type and content of the constraint condition, judging whether the obtained constraint condition meets the requirement of deriving the connection condition based on the constant relationship; The constraint conditions that meet the requirements of deriving the connection conditions based on the constant relationship are stored in the to-be-processed set.
6. The query optimization method according to claim 5, in, Judging whether the acquired constraint condition meets the requirement of deriving the connection condition based on the constant relationship according to the type and content of the constraint condition includes: Determine whether the acquired constraint condition has a binary expression, does not contain a volatile function, does not contain a subquery, and is composed of column attributes, operators, and constants; When the above judgment results are all yes, it is determined that the acquired constraint condition meets the requirement of deriving the connection condition based on the constant relationship.
7. The query optimization method according to claim 1, in, Judging whether the derived connection condition needs to be used according to the connection costs between the multiple tables includes: Judging whether the derived connection condition needs to be used according to the connection costs between the multiple tables includes: Determine whether there is an original connection condition between the multiple tables in the query statement; If not, it is determined that the derived connection condition needs to be used; If yes, then perform an execution cost estimation, and determine whether the derived execution cost of the connection condition is less than the execution cost of the original connection condition; If it is less than, it is determined that the derived connection condition needs to be used; If it is not less than, it is determined that there is no need to use the derived connection condition.
8. The query optimization method according to claim 1, in, Executing the query statement based on the derived connection condition includes: An execution plan is generated by using the derived connection condition as the connection condition between the multiple tables.
9. A machine-readable storage medium having a machine-executable program stored thereon, wherein the machine-executable program, when executed by a processor, implements the query optimization method according to any one of claims 1 to 8.
10. A computer device comprising a memory, a processor, and a machine executable program stored in the memory and running on the processor, wherein the processor implements the query optimization method according to any one of claims 1 to 8 when executing the machine executable program.
