Database operation statement optimization method and related product
By obtaining and utilizing limit operation information in database operation statements, estimating target cardinality and determining execution plans, the problem of difficult information transmission during database optimization is solved, execution efficiency is improved and resource consumption is reduced.
Patent Information
- Application Number
- CN202410627715.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2024-08-23
AI Technical Summary
During the database optimization process, it is difficult to transmit interactive information between levels, making it difficult for upper-level nodes to give feedback after the lower-level nodes are generated, resulting in a waste of database resources.
By obtaining the limit operation information in the database operation statement, the target cardinality is estimated, and the execution plan is determined based on the target cardinality, thereby pushing the limit information down to the cardinality estimation step of the optimizer to generate a more reasonable execution plan.
It improves the execution efficiency of database operation statements, reduces the consumption of database resources, and generates an execution plan that is more in line with the actual situation.
Smart Images

Figure CN118535588A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database technology, and in particular to a method for optimizing database operation statements and related products. Background Art
[0002] When the optimizer generates a path, it generates the base table path from the bottom up, and then generates other operations such as the connection path, layer by layer. However, it is difficult to transmit interactive information between the layers. Often, after the lower-level nodes are generated, the upper-level nodes are generated based on the lower-level nodes. At this time, it is difficult for the upper-level nodes to give feedback information to the lower-level nodes to generate a more reasonable lower-level node, which will cause some unnecessary waste of database resources. Summary of the invention
[0003] In view of the above problems, the present invention proposes a method for optimizing database operation statements and related products that overcome the above problems or at least partially solve the above problems.
[0004] An object of the present invention is to improve the execution efficiency of database operation statements that meet the conditions.
[0005] A further object of the present invention is to generate an optimal execution plan based on constraint conditions.
[0006] A further object of the present invention is to reduce the consumption of database resources.
[0007] In particular, the present invention provides a method for optimizing database operation statements, which comprises:
[0008] Get database operation statements;
[0009] Determine whether there are restrictive operations in the database operation statement;
[0010] If yes, obtain the restriction information corresponding to the restriction operation;
[0011] Estimate the target cardinality of database operation statements after restriction based on restriction information;
[0012] Determine the execution plan of the database operation statement based on the target cardinality.
[0013] Optionally, the step of determining whether there is a restriction operation in the database operation statement includes:
[0014] Determine whether the target keyword exists in the database operation statement;
[0015] When the target keyword exists in the database operation statement, it is determined that a restriction operation exists in the database operation statement.
[0016] Optionally, the step of estimating a target cardinality of the database operation statement after restriction according to the restriction information includes:
[0017] Estimate the total amount of data in the data table corresponding to the database operation statement;
[0018] The total data volume is estimated based on the restriction information and the amount of data obtained after restriction is used as the target base.
[0019] Optionally, the step of determining an execution plan of a database operation statement according to the target cardinality includes:
[0020] Estimate the cost of different execution modes in different nodes of the execution plan based on the target cardinality;
[0021] The execution mode of the node is determined according to the cost, thereby generating an execution plan.
[0022] Optionally, the target keyword includes LIMIT;
[0023] The step of estimating the target cardinality of the database operation statement after restriction according to the restriction information includes: estimating the total data volume of the data table corresponding to the database operation statement; estimating the data volume obtained after restriction according to the restriction information corresponding to LIMIT as the target cardinality.
[0024] Optionally, the step of determining an execution plan of a database operation statement according to the target cardinality includes:
[0025] When the upper node of the database operation statement is the control node corresponding to LIMIT and the lower node is the scan node, the cost required for different scan methods in the scan node is estimated according to the target cardinality;
[0026] The scanning method is determined according to the cost to generate an execution plan.
[0027] Optionally, the target keyword also includes JOIN;
[0028] The step of determining the execution plan of the database operation statement according to the target cardinality includes: when the upper node of the database operation statement is a control node corresponding to LIMIT and the lower node is a connection node corresponding to JOIN, estimating the costs required for different connection modes in the connection node according to the target cardinality;
[0029] The connection method is determined based on the cost, thereby generating an execution plan.
[0030] According to another aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned methods for optimizing database operation statements are implemented.
[0031] According to another aspect of the present invention, a computer program product is provided, which includes a computer program, and when the computer program is executed by a processor, the steps of any one of the above-mentioned methods for optimizing database operation statements are implemented.
[0032] 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 the steps of any of the above-mentioned database operation statement optimization methods when executing the machine executable program.
[0033] The optimization method of database operation statements of the present invention first obtains database operation statements; determines whether there are restriction operations in the database operation statements; if so, obtains restriction information corresponding to the restriction operations; estimates the target cardinality of the database operation statements after restriction according to the restriction information; and determines the execution plan of the database operation statements according to the target cardinality. Through this method, the restriction information can be pushed down to the cardinality estimation step of the optimizer, so as to more accurately obtain the target cardinality of the database operation statements, and the optimizer can generate an execution plan that is more in line with the actual situation according to the target cardinality, thereby improving the execution efficiency of the database operation statements that meet the conditions and reducing the consumption of database resources.
[0034] 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
[0035] 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:
[0036] Figure 1 is a flowchart of a method for optimizing database operation statements according to an embodiment of the present invention;
[0037] Figure 2 is a flow chart of a method for optimizing database operation statements according to another embodiment of the present invention;
[0038] Figure 3 is a schematic diagram of a computer-readable storage medium according to a method for optimizing database operation statements according to an embodiment of the present invention;
[0039] Figure 4 is a schematic diagram of a computer program product according to a method for optimizing database operation statements according to an embodiment of the present invention; and
[0040] Figure 5The figure is a schematic diagram of a computer device for a method for optimizing database operation statements according to an embodiment of the present invention. DETAILED DESCRIPTION
[0041] It should be understood by those skilled in the art that the embodiments described below are only some embodiments of the present invention, rather than all embodiments of the present invention, and these embodiments are intended to explain the technical principles of the present invention, rather than to limit the protection scope of the present invention. Based on the embodiments provided by the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should still fall within the protection scope of the present invention.
[0042] Figure 1 1 is a flow chart of a method for optimizing database operation statements according to an embodiment of the present invention. In this embodiment, the flow chart generally includes:
[0043] Step S101, obtaining database operation statements. Database operation statements refer to statements for controlling and modifying a database. An optional example is SQL (Structured Query Language). SQL statements have multiple functions such as data manipulation and data definition. This language is interactive and can provide great convenience for users. In this step, the method for obtaining database operation statements can generally be active input or automatically generated by the database program according to the business. Those skilled in the art can decide on their own how to obtain database operation statements according to actual conditions.
[0044] Step S102, determining whether there is a restriction operation in the database operation statement. In this embodiment, the restriction operation generally refers to screening the data in the target table of the database operation statement through various conditions, thereby limiting the data finally obtained by the database operation statement.
[0045] The step of determining whether there is a restriction operation in the database operation statement may generally include: determining whether there is a target keyword in the database operation statement; and determining whether there is a restriction operation in the database operation statement when the target keyword exists in the database operation statement. The target keyword may generally include keywords such as LIMIT or ROWNUM. Those skilled in the art may determine the specific type of the target keyword according to actual conditions.
[0046] Step S103, if the result of step S102 is yes, obtain the restriction information corresponding to the restriction operation. The restriction information refers to the specific content restricted by the restriction operation. An optional SQL example is shown in formula (1):
[0047] Select*from t1 limit 1 formula (1)
[0048] In the SQL statement shown in formula (1), LIMIT indicates a restriction operation, and the specific restriction information is 10, which means taking the first row of data in table t1.
[0049] Step S104, estimating the target cardinality of the database operation statement after restriction according to the restriction information.
[0050] In some optional embodiments, the step of estimating the target cardinality of the database operation statement after restriction based on restriction information may generally include: estimating the total amount of data in the data table corresponding to the database operation statement; estimating the total amount of data after restriction based on the restriction information as the target cardinality.
[0051] Optionally, the target keyword may generally include LIMIT; the step of estimating the target cardinality of the database operation statement after restriction according to the restriction information may generally include: estimating the total data volume of the data table corresponding to the database operation statement; estimating the data volume obtained by restriction according to the restriction information corresponding to LIMIT as the target cardinality. Still taking formula (1) as an example, when the target keyword is LIMIT, the optimizer will first estimate the total data volume in the t1 table, for example, the total data volume is 1000 rows of data; then, according to the corresponding restriction information limit 1, it is estimated that the data volume of the t1 table after restriction is 1 row of data, that is, the target cardinality is 1.
[0052] Step S105, determining an execution plan for the database operation statement according to the target cardinality.
[0053] In some optional embodiments, the step of determining the execution plan of the database operation statement according to the target cardinality may generally include: estimating the cost required for different execution modes in different nodes in the execution plan according to the target cardinality; determining the execution mode of the node according to the cost, thereby generating the execution plan. The node refers to the execution plan or execution path that the database operation statement generally generates from the bottom to the top through the optimizer in the database, wherein each layer is called a node.
[0054] Optionally, the step of determining the execution plan of the database operation statement according to the target cardinality can generally include: when the upper node of the database operation statement is the control node corresponding to LIMIT and the lower node is a scan node, estimating the cost required for different scanning methods in the scan node according to the target cardinality; determining the scan method according to the cost, thereby generating an execution plan. In the current SQL statement, the LIMIT clause is used to limit the number of rows returned in the query result set. The LIMIT clause is usually used when you only want to obtain part of the result set instead of all the data, especially when processing large data sets or implementing paging queries. When the optimizer in the database faces an SQL statement with a LIMIT clause, it will generate a corresponding execution path from the bottom up, first generating the base table path, then generating other operations such as the connection path, and building it layer by layer.
[0055] Still taking the SQL statement shown in formula (1) as an example, the database will generate a scan node before generating the control node corresponding to LIMIT, and there are also multiple scanning methods to choose from during the scanning process of the scan node, such as sequential scanning or index scanning. Generally, the optimizer will select a specific scanning method according to the structure of the t1 table. In the case of the original 1000 data, the optimizer may consider whether there is an index in the t1 table, and thus select index scanning. However, in the present invention, by pushing down the restriction information corresponding to LIMIT to the cardinality estimation step, it is determined that the target cardinality is 1. At this time, the cardinality considered by the optimizer changes from 1000 to 1. When the target cardinality is 1, the optimizer will directly select sequential scanning to generate the corresponding execution plan. It can be seen from the above example that when the method of the present invention is not used for cardinality estimation, the optimizer may choose the scanning method of index scanning, but after using the method of the present invention, the sequential scanning is directly selected. At this time, the cost of index scanning is reduced in the scan node relative to the original execution plan, so that the optimizer can select a better execution plan according to a more accurate target cardinality.
[0056] In some other optional embodiments, the target keyword may generally include JOIN; the step of determining the execution plan of the database operation statement according to the target cardinality may generally include: when the upper node of the database operation statement is the control node corresponding to LIMIT, and the lower node is the connection node corresponding to JOIN, the cost required for different connection methods in the connection node is estimated according to the target cardinality; the connection method is determined according to the cost, thereby generating an execution plan. Through the method of the present invention, for the connection method of the connection node, the optimizer can select a more appropriate connection method based on a more accurate target cardinality. For example, if the data in the entire table is large, the optimizer may originally choose to use hash connection or sort connection, and then perform LIMIT screening. However, through the method of the present invention, after the LIMIT restriction, only one piece of data needs to be connected. At this time, the optimizer can select a nested loop connection according to the actual target cardinality, without using hash connection or sort connection. Thus, a better execution plan is generated.
[0057] Through this method, the restriction information can be pushed down to the cardinality estimation step of the optimizer, so as to obtain the target cardinality of the database operation statement more accurately. The optimizer can generate an execution plan that is more in line with the actual situation based on the target cardinality, thereby improving the execution efficiency of qualified database operation statements and reducing the consumption of database resources.
[0058] Figure 2 FIG. 1 is a flow chart of a method for optimizing database operation statements according to another embodiment of the present invention. In this embodiment, the flow chart generally includes:
[0059] Step S201, obtaining database operation statements. Database operation statements refer to statements for controlling and modifying a database. An optional example is SQL (Structured Query Language). SQL statements have multiple functions such as data manipulation and data definition. This language has interactive characteristics and can provide great convenience for users. In this step, the method for obtaining database operation statements can generally be active input or automatically generated by the database program according to the business. Those skilled in the art can decide on their own how to obtain database operation statements according to actual conditions.
[0060] Step S202, determine whether there is a restriction operation in the database operation statement. In this embodiment, the restriction operation generally refers to screening the data in the target table of the database operation statement through various conditions, thereby limiting the data finally obtained by the database operation statement.
[0061] The step of determining whether there is a restriction operation in the database operation statement may generally include: determining whether there is a target keyword in the database operation statement; and determining whether there is a restriction operation in the database operation statement when the target keyword exists in the database operation statement. The target keyword may generally include keywords such as LIMIT or ROWNUM. Those skilled in the art may determine the specific type of the target keyword according to actual conditions.
[0062] Step S203: If the result of step S202 is yes, obtain restriction information corresponding to the restriction operation, wherein the restriction information refers to the specific content restricted by the restriction operation.
[0063] Step S204, estimating the target cardinality of the database operation statement after restriction according to the restriction information. In some optional embodiments, the step of estimating the target cardinality of the database operation statement after restriction according to the restriction information may generally include: estimating the total data volume of the data table corresponding to the database operation statement; estimating the total data volume after restriction according to the restriction information as the target cardinality.
[0064] Optionally, the target keyword may generally include LIMIT; the step of estimating the target cardinality of the database operation statement after restriction according to the restriction information may generally include: estimating the total data volume of the data table corresponding to the database operation statement; estimating the data volume obtained by restriction according to the restriction information corresponding to LIMIT as the target cardinality. Still taking formula (1) as an example, when the target keyword is LIMIT, the optimizer will first estimate the total data volume in the t1 table, for example, the total data volume is 1000 rows of data; then, according to the corresponding restriction information limit 1, it is estimated that the data volume of the t1 table after restriction is 1 row of data, that is, the target cardinality is 1.
[0065] Step S205, obtaining node information corresponding to the database operation statement.
[0066] Step S206, determining whether the lower layer node is a connection node or a scanning node.
[0067] Step S207, when it is determined in step S206 that the node is a scan node, the costs required for different scanning modes in the scan node are estimated according to the target cardinality.
[0068] Step S208, determining a scanning method according to the cost, thereby generating an execution plan.
[0069] Still taking the SQL statement shown in formula (1) as an example, the database will generate a scan node before generating the control node corresponding to LIMIT, and there are also multiple scanning methods to choose from during the scanning process of the scan node, such as sequential scanning or index scanning. Generally, the optimizer will select a specific scanning method according to the structure of the t1 table. In the case of the original 1000 data, the optimizer may consider whether there is an index in the t1 table, and thus select index scanning. However, in the present invention, by pushing down the restriction information corresponding to LIMIT to the cardinality estimation step, it is determined that the target cardinality is 1. At this time, the cardinality considered by the optimizer changes from 1000 to 1. When the target cardinality is 1, the optimizer will directly select sequential scanning to generate the corresponding execution plan. It can be seen from the above example that when the method of the present invention is not used for cardinality estimation, the optimizer may choose the scanning method of index scanning, but after using the method of the present invention, the sequential scanning is directly selected. At this time, the cost of index scanning is reduced in the scan node relative to the original execution plan, so that the optimizer can select a better execution plan according to a more accurate target cardinality.
[0070] Step S209, when it is determined in step S206 that the node is a connection node, the cost required for different connection modes in the connection node is estimated according to the target cardinality.
[0071] In other optional embodiments, the target keyword may also generally include JOIN. Through the method of the present invention, for the connection method of connecting nodes, the optimizer can select a more appropriate connection method based on a more accurate target cardinality. For example, if the data in the entire table is large, the optimizer may originally choose to use hash connection or sort connection, and then perform LIMIT screening. However, through the method of the present invention, after the LIMIT restriction, only one piece of data needs to be connected. At this time, the optimizer can select a nested loop connection based on the actual target cardinality, without using hash connection or sort connection. Thus, a better execution plan is generated.
[0072] Step S210, determining a connection method according to the cost, thereby generating an execution plan.
[0073] Through this method, the restriction information can be pushed down to the cardinality estimation step of the optimizer, so as to obtain the target cardinality of the database operation statement more accurately. The optimizer can generate an execution plan that is more in line with the actual situation based on the target cardinality, thereby improving the execution efficiency of qualified database operation statements and reducing the consumption of database resources.
[0074] It should be understood that in some embodiments, each part 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.
[0075] This embodiment also provides a computer-readable storage medium 301 , a computer program product 401 , and a computer device 501 . Figure 3 is a schematic diagram of a computer-readable storage medium according to a method for optimizing database operation statements according to an embodiment of the present invention; Figure 4 is a schematic diagram of a computer program product according to a method for optimizing database operation statements according to an embodiment of the present invention; and Figure 5 Schematic diagram of a computer device for a method for optimizing database operation statements according to an embodiment of the present invention. A computer program product 401 includes a computer program 302, which implements the steps of any of the above-mentioned methods for optimizing database operation statements when executed by a processor 303. A computer-readable storage medium 301 stores the above-mentioned computer program 302, which implements the steps of any of the above-mentioned methods for optimizing database operation statements when executed by a processor 303. A computer device 501 may include a memory 502, a processor 303, and a computer program 302 stored in the memory 502 and running on the processor 303.
[0076] The computer program 302 for performing the operation of the present invention may be an assembly instruction, an instruction set architecture (ISA) instruction, a machine instruction, a machine-dependent instruction, a microcode, a firmware instruction, a state setting data, a configuration data of an integrated circuit, or a source code or an object code written in any combination of one or more programming languages and a procedural programming language. The computer program 302 may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the latter case, the remote computer may be connected to the user's computer via any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet Service Provider). In some embodiments, to perform aspects of the present invention, electronic circuits including, for example, programmable logic circuits, Field-Programmable Gate Arrays (FPGAs), or Programmable Logic Arrays (PLAs) may execute computer-readable program instructions by utilizing state information of the computer-readable program instructions to personalize the electronic circuits.
[0077] In the description of this embodiment, computer program product 401 is a related product including computer program 302 .
[0078] For the purposes of the description of this embodiment, computer readable storage medium 301 is a tangible device capable of retaining and storing computer program 302, which can be any device that can contain, store, communicate, propagate or transmit a program for use with or in conjunction with an instruction execution system, device or apparatus. More specific examples (a non-exhaustive list) of computer readable storage medium 301 include the following: 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), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, and any suitable combination of the foregoing.
[0079] Computer device 501 can be, for example, a server, a desktop computer, a notebook computer, a tablet computer, or a smart phone. In some examples, computer device 501 can be a cloud computing node. Computer device 501 can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, a program module can include routines, programs, target programs, components, logic, data structures, etc. that perform specific tasks or implement specific abstract data types. Computer device 501 can be implemented in a distributed cloud computing environment where remote processing devices linked by a communication network perform tasks. In a distributed cloud computing environment, program modules can be located on a local or remote computing system storage medium including a storage device.
[0080] The computer device 501 may include a processor 303 adapted to execute stored instructions, and a memory 502 that provides temporary storage space for the operation of the instructions during operation. The processor 303 may be a single-core processor, a multi-core processor, a computing cluster, or any number of other configurations. The memory 502 may include random access memory (RAM), read-only memory, flash memory, or any other suitable storage system.
[0081] The computer device 501 may also include a network adapter / interface and an input / output (I / O) interface. The I / O interface allows data to be input and output with external devices that may be connected to the computer device. The network adapter / interface may provide communication between the computer device and a network, which is typically shown as a communication network.
[0082] 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 method for optimizing database operation statements. include: Obtaining the database operation statement; Determine whether there is a restriction operation in the database operation statement; If yes, obtaining restriction information corresponding to the restriction operation; estimating a target cardinality of the database operation statement after restriction according to the restriction information; An execution plan for the database operation statement is determined according to the target cardinality.
2. The method for optimizing database operation statements according to claim 1, in, The step of determining whether there is a restriction operation in the database operation statement comprises: Determining whether a target keyword exists in the database operation statement; When the target keyword exists in the database operation statement, it is determined that the restriction operation exists in the database operation statement.
3. The method for optimizing database operation statements according to claim 2, in, The step of estimating the target cardinality of the database operation statement after restriction according to the restriction information comprises: Estimating the total amount of data in the data table corresponding to the database operation statement; The amount of data obtained by limiting the total data amount is estimated according to the limitation information as the target base.
4. The method for optimizing database operation statements according to claim 3, in, The step of determining the execution plan of the database operation statement according to the target cardinality includes: Estimate the costs required for different execution modes in different nodes in the execution plan according to the target cardinality; The execution mode of the node is determined according to the cost, thereby generating the execution plan.
5. The method for optimizing database operation statements according to claim 4, in, The target keywords include LIMIT; The step of estimating the target cardinality of the database operation statement after restriction according to the restriction information includes: estimating the total data volume of the data table corresponding to the database operation statement; estimating the data volume obtained after restriction of the total data volume as the target cardinality according to the restriction information corresponding to LIMIT.
6. The method for optimizing database operation statements according to claim 5, in, The step of determining the execution plan of the database operation statement according to the target cardinality includes: When the upper node of the database operation statement is a control node corresponding to LIMIT and the lower node is a scanning node, the cost required for different scanning modes in the scanning node is estimated according to the target cardinality; A scanning mode is determined according to the cost, thereby generating the execution plan.
7. The method for optimizing database operation statements according to claim 4, in, The target keyword also includes JOIN; The step of determining the execution plan of the database operation statement according to the target cardinality includes: when the upper node of the database operation statement is a control node corresponding to LIMIT and the lower node is a connection node corresponding to JOIN, estimating the cost required for different connection modes in the connection node according to the target cardinality; The connection mode is determined according to the cost, thereby generating the execution plan.
8. A computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the computer program implements the steps of the method for optimizing database operation statements according to any one of claims 1 to 7.
9. A computer program product, comprising a computer program, which, when executed by a processor, implements the steps of the method for optimizing database operation statements according to any one of claims 1 to 7.
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 steps of the method for optimizing database operation statements according to any one of claims 1 to 7 when executing the machine executable program.
