Methods for optimizing the processing of unions in relational database management systems

By designing the megescan node merge subquery scan in the relational database management system, the problem of resource waste caused by multiple data table scans is solved, and the SQL execution efficiency and system processing capacity are improved.

CN114036188BActive Publication Date: 2024-08-23广州海量数据库技术有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111429533.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-29
Publication Date
2024-08-23
Estimated Expiration
2041-11-29

AI Technical Summary

Technical Problem

When existing relational database management systems process SQL that contains multiple subqueries, they need to scan the data table multiple times, resulting in a waste of resources and low efficiency.

Method used

Design a new operator (megescan node) to merge multiple subqueries connected by the union all operator, scan the basic table through the megescan node and output the results to the upper query node for projection or expression calculation, and finally form an optimized query plan, reducing The number of scans of the data table.

Benefits of technology

Significantly improves the efficiency of SQL execution, saves time, hardware and human resources, reduces operating costs, and improves system processing capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114036188B
    Figure CN114036188B
Patent Text Reader

Abstract

The present invention belongs to the technical field of database management and operating system, and in particular, to a method for optimizing the processing of unions in a relational database management system. The present invention proposes for the first time a new operator for the union operation union all in the optimization generation query plan stage of a relational database management system, by merging subqueries with the same access mode to the base table, the new operator will submit the access result of the base table to the upper-level subquery to perform a projection operation or expression calculation, and finally output the query result. The method can well overcome the defect of the existing technical solution that multiple data table scans are required for SQL containing multiple subquery plans, resulting in a large waste of resources, significantly reducing the operation cost of SQL, improving the system processing capacity and SQL execution efficiency, and saving a lot of time, hardware and human resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of database management and operating system, and in particular to a method for optimizing the processing of unions in a relational database management system and its application. Background Art

[0002] In a typical relational database management system such as OpenGauss, processing a query is usually divided into the following three stages:

[0003] (1) Lexical and grammatical analysis mainly converts the user's input text (SQL) into an internal data structure, generally called a grammar parse tree, and verifies the correctness of the grammar, ultimately obtaining a grammar parse tree representing SQL.

[0004] (2) Optimize and generate a query plan. The grammatical parsing tree obtained in the previous stage is optimized based on rules and physical costs to generate an optimal query plan.

[0005] (3) Execute the query plan generated in the previous stage (generally using an iterator), obtain the query results and return them to the user.

[0006] According to the conventional processing strategy in the current relational database management system, in the above step (2) of optimizing and generating query plans, for the SQL to be processed, such as SQL: select t.stid,'english'as cousename,t.english as score from t union all select t.stid,'chinese'as cousename,t.chinese as score from t, the structure of table t is the primary key stid (student's student ID), which is of type int, and english (student's English score) and chinese (student's Chinese score) are also int types. This SQL obtains the student's student ID and English score. This type of SQL contains multiple subqueries, which obtain output columns from the same single or multiple tables, and finally perform a union all operation on the results. In the current operation, for this type of SQL, when generating a query plan, a subquery plan will be generated for each subquery, and finally the multiple subquery plans will be organized using the append operator (see Figure 1). For the above SQL example of obtaining the English language scores of students, an append operator will be generated, which contains two subquery plans, representing select t.stid,'english'as cousename,t.english as score from t and select t.stid,'chinese'as cousename,t.chinese as score from t.

[0007] Therefore, in the next step (3) of query plan execution, the query plan needs to be executed one by one from bottom to top, that is, first perform a sequential scan (seqscan) operation on t at the bottom level, then execute the append operator, and obtain the final result (result). However, the biggest drawback of the above process is that in the process of executing the plan, table t needs to be scanned repeatedly (at least twice), resulting in a waste of resources. In other words, if SQL contains N subquery plans, to do a union of N subqueries, table t needs to be scanned N times. Obviously, the more subquery plans SQL contains and the larger N is, the more resources will be wasted, and the time, hardware and labor costs will increase significantly. Summary of the invention

[0008] In order to overcome the defect that the existing technical solution needs to perform multiple data table scans for SQL containing multiple subquery plans, thereby causing a large waste of resources, the present invention provides a solution. This solution designs an operator to merge the scans in union all and all the lower-level subqueries (for the scan of a single table or the link scan of multiple tables), directly performs projection operations or expression calculations according to the requirements of each subquery, and finally outputs the query results, which can greatly improve the efficiency of SQL execution and save a lot of time, hardware and human resources.

[0009] For example, for the SQL described above:

[0010] select t.stid,'english'as cousename,t.english as score from t unionall select t.stid,'chinese'as cousename,t.chinese as score from t.

[0011] To address this situation, we handle multiple subqueries connected by union all (record merging) as follows: 1. Extract each subquery and group them. The subqueries in the same group must meet the following conditions: no sorting and the same access method to the underlying table (for a single table, the same underlying table is required; for links to multiple tables, the underlying tables must be the same and the linking method must also be the same); 2. For multiple subqueries in each group, design a new operator to represent the access to the underlying table. The operator scans each row and outputs the query results based on each subquery; 3. Use union all to connect each group and combine them with the append operator.

[0012] As attached Figure 2 As shown, the bottom layer is a megescan operator (node), and the query1 operator and query2 operator on the upper layer are on the same layer. Above the query1 and query2 operators is the result operator on the top layer.

[0013] The execution process is also executed from bottom to top: first execute the megescan node to scan table t, and output the scan results to query1 and query2 one by one; query1 and query2 perform necessary calculations based on the results output by megescan, such as projection operations or expression calculations, and then submit the results to the top-level result node; and the result node returns the query results to the user.

[0014] Specifically, the present invention provides a method for optimizing the processing of unions in a relational database management system. In the query plan generation stage of optimizing the relational database, the method provides a method for optimizing the processing of unions. An operator is designed to merge the scans in the union all and all the subqueries in the lower layer, and projection operations or expression calculations are directly performed according to the requirements of each subquery, and finally the query results are output, thereby improving the efficiency of SQL execution and saving time, hardware and human resources.

[0015] The method for optimizing the processing of the union includes the following steps:

[0016] (1) In the query plan generation phase of optimization, multiple subqueries connected by the union all operator are grouped;

[0017] (2) After completing the grouping, when generating a query plan, for the subqueries in the same group, the first subquery is processed first, and a two-layer query plan is generated for it. The lower layer is the megescan node, which represents the scanning base table or link operation of the subquery, and the upper layer is the query node, which represents the projection and calculation operations of the subquery. For the remaining subqueries in the same group, the lower megescan node of the first subquery is used as the lower plan, and the upper plan is the projection and calculation operations corresponding to the subquery. After the above processing, a total of 1 megescan node and N upper query nodes corresponding to N subqueries are generated, and the lower nodes of the N upper query nodes are all the same megescan node. After processing the subqueries in each group in the same way, all groups are connected using the append method to form the final execution plan.

[0018] (3) In the query plan execution phase, the above nodes are initialized one by one from bottom to top. For the megescan node and its upper N query nodes in the same group, the megescan node as the lower node is initialized first, and then the N query nodes above it are initialized one by one.

[0019] (4) In the query plan execution phase, the entire query plan is executed in an iterator manner; for the 1 lower-level megescan node and N upper-level query nodes generated by the N subqueries in the same group, the megescan node is first executed to obtain a record, and the obtained record is submitted to the N upper-level query nodes. The N upper-level query nodes execute the record and return the execution results one by one to the result node at the top level. Finally, the result node returns the query result to the user;

[0020] (5) After the query plan is executed, when the execution nodes are destroyed to reclaim resources, these nodes are also destroyed one by one in a bottom-up manner. For one lower-level megescan node and N upper-level query nodes in the same group, the megescan node is destroyed first to release the resources, and then the N query nodes are destroyed one by one to release their resources.

[0021] Furthermore, the scanning in the subquery in the present method includes scanning of a single table and link scanning of multiple tables.

[0022] Furthermore, in step (1) of the method, the multiple subqueries connected by the union all operator are grouped, and the subqueries grouped into the same group meet the following conditions: there is no aggregation or sorting operation, and the basic table access method is the same.

[0023] Furthermore, the above-mentioned same basic table access method means that when the basic table accessed is a single table, the same basic table is required; when the basic table accessed is a link of multiple tables, the basic tables are required to be the same and the linking methods are also the same.

[0024] Furthermore, in step (2) of the method, when there is only one group, the append node is no longer set, and each upper-level query node in the group is directly used as a lower-level node of the result.

[0025] Furthermore, in step (4) of the method, when the megescan node does not obtain the record to be processed, it directly returns and submits the blank to the N upper query nodes. At this time, the N upper query nodes directly return the blank to the result node at the top layer, and finally the result node returns the blank query result to the user;

[0026] In step (4), executing the megescan node refers to scanning the base table or linking operation; and executing the record by the upper query node refers to the upper query node performing a projection operation or expression calculation on the record.

[0027] In addition, the present invention also relates to the use of the above-mentioned method for optimizing the processing of unions in a relational database management system in a relational database management or operating system.

[0028] In summary, the present invention proposes for the first time a new operator (node) for the union operation union all in the optimization query plan generation stage of a relational database management system. By merging subqueries with the same access method to the base table, the new operator submits the access result of the base table to the upper-level subquery to perform a projection operation or expression calculation, and finally outputs the query result. The method can well overcome the defect of the prior art solution that multiple data table scans are required for SQL containing multiple subquery plans, resulting in a large waste of resources, significantly reducing the operation cost of SQL, improving the system processing capability and SQL execution efficiency, and saving a lot of time, hardware and human resources. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions of the prior art and the embodiments of the present invention, the drawings required for use in the prior art and the embodiments of the present invention are briefly introduced below. Obviously, the following drawings are only some embodiments recorded in the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without paying creative work.

[0030] Figure 1A query plan strategy diagram is generated for optimizing SQL statements containing multiple subqueries using the current method.

[0031] Figure 2 The present invention is a schematic diagram of a query plan strategy generated by the method for optimizing SQL containing multiple sub-queries. DETAILED DESCRIPTION

[0032] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. The present invention can also be implemented or applied through other different specific implementation methods. The details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present invention.

[0033] At the same time, it should be understood that the protection scope of the present invention is not limited to the specific embodiments described below; it should also be understood that the terms used in the embodiments of the present invention are for describing specific embodiments rather than for limiting the protection scope of the present invention.

[0034] Embodiment 1: A method for optimizing the processing of unions in a relational database management system. In the optimization query plan generation stage of the relational database management system, the method designs a new operator (node) for the union operation union all, merges subqueries with the same access mode to the base table (for scanning a single table or link scanning of multiple tables), and the new operator submits the access result of the base table to the upper-level subquery to perform a projection operation or expression calculation, and finally outputs the query result. The method specifically includes the following steps:

[0035] (1) During the query plan generation phase, multiple subqueries connected by the union all operator (including single table scans and linked scans of multiple tables) are grouped. The subqueries grouped into the same group meet the following conditions: there is no aggregation or sorting operation, and the access method of the base table is the same (when the accessed base table is a single table, the same base table is required; when the accessed base table is a link of multiple tables, the base tables are required to be the same and the link method is also the same).

[0036] (2) After completing the grouping, when generating a query plan, for the subqueries within the same group, the first subquery is processed first, and a two-layer query plan is generated for it. The lower layer is the megescan node, which represents the scanning base table or link operation of the subquery, and the upper layer is the query node, which represents the projection and calculation operations of the subquery. For the remaining subqueries within the same group, the lower layer megescan node of the first subquery is used as the lower layer plan, and the upper layer plan is the projection and calculation operations corresponding to the subquery. After the above processing, a total of 1 megescan node and N upper layer query nodes corresponding to N subqueries are generated, and the lower layer nodes of the N upper layer query nodes are all the same megescan node. After processing the subqueries in each group in the same way, all groups are connected using the append method to form the final execution plan. When there is only one group, the append node is no longer set, and each upper layer query node in the group is directly used as the lower layer node of the result.

[0037] (3) In the query plan execution phase, the above nodes are initialized one by one from bottom to top. For the megescan node and its upper N query nodes in the same group, the megescan node as the lower node is initialized first, and then the upper N query nodes are initialized one by one.

[0038] (4) In the query plan execution phase, the entire query plan is executed in an iterator manner; for the 1 lower-level megescan node and N upper-level query nodes generated by the N subqueries in the same group mentioned above, the megescan node is first executed (scanning the basic table or linking operation) to obtain a record, and the obtained record is submitted to the N upper-level query nodes. The N upper-level query nodes execute the record (projecting the record or calculating the expression), and return the execution results one by one to the result node at the top level, and finally the result node returns the query result to the user; when the megescan node does not obtain the record to be processed, it returns directly and submits a blank to the N upper-level query nodes. At this time, the N upper-level query nodes directly return a blank to the result node at the top level, and finally the result node returns the blank query result to the user.

[0039] (5) After the query plan is executed, when the execution nodes are destroyed to reclaim resources, these nodes are also destroyed one by one in a bottom-up manner. For one lower-level megescan node and N upper-level query nodes in the same group, the megescan node is destroyed first to release the resources, and then the N query nodes are destroyed one by one to release their resources.

[0040] Embodiment 2: A method for optimizing the processing of unions in a relational database management system (see Figure 2 ),

[0041] SQL: select t.stid,'english'as cousename,t.english as score from tunion all select t.stid,'chinese'as cousename,t.chinese as score from t, where the structure of table t is the primary key stid (student's student ID), which is of int type, and english (student's English score) and chinese (student's Chinese score) are also of int type. This SQL contains multiple subqueries, which obtain the student's student ID and English score. These subqueries obtain output columns from the same single or multiple tables, and finally perform a union all operation on the results.

[0042] As attached Figure 2 As shown, the bottom layer is a megescan operator (node), and the query1 operator and query2 operator on the upper layer are on the same layer. Above the query1 and query2 operators is the result operator on the top layer.

[0043] The execution process is also executed from bottom to top: first execute the megescan node to scan table t, and output the scan results to query1 and query2 one by one; query1 and query2 perform necessary calculations based on the results output by megescan, such as projection operations or expression calculations, and then submit the results to the top-level result node; and the result node returns the query results to the user.

[0044] The various embodiments of the present invention are described in a progressive manner, and the same or similar parts between the various embodiments can be referred to in detail.

[0045] The above description is only an embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, replacement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the claims of the present invention.

Claims

1. A method for optimizing the processing of unions in a relational database management system, characterized in that: This method provides a method for optimizing the processing of unions in the query plan generation stage of a relational database. By designing an operator, the scans in the union all and all the subqueries in the lower layer are merged, and the projection operation or expression calculation is directly performed according to the requirements of each subquery, and finally the query result is output; The method for optimizing the processing of the union includes the following steps: (1) During the query plan generation phase, multiple subqueries connected by the union all operator are grouped. The subqueries grouped into the same group meet the following conditions: there is no aggregation or sorting operation, and the basic table access method is the same; (2) After completing the grouping, when generating a query plan, for the subqueries within the same group, the first subquery is processed first, and a two-layer query plan is generated for it. The lower layer is the megescan node, which represents the scanning base table or link operation of the subquery, and the upper layer is the query node, which represents the projection and calculation operations of the subquery. For the remaining subqueries within the same group, the lower layer megescan node of the first subquery is used as the lower layer plan, and the upper layer plan is the projection and calculation operations corresponding to the subquery. After the above processing, a total of 1 megescan node and N upper layer query nodes corresponding to N subqueries are generated, and the lower layer nodes of the N upper layer query nodes are all the same megescan node. After processing the subqueries in each group in the same way, all groups are connected using the append method to form the final execution plan. (3) In the query plan execution phase, the above nodes are initialized one by one from bottom to top. For the megescan node and its upper N query nodes in the same group, the megescan node as the lower node is initialized first, and then the N query nodes above it are initialized one by one. (4) In the query plan execution phase, the entire query plan is executed using an iterator. For the 1 lower-level megescan node and N upper-level query nodes generated by the N subqueries in the same group, the megescan node is first executed to obtain a record, and the obtained record is submitted to the N upper-level query nodes. The N upper-level query nodes execute the record and return the execution results one by one to the result node at the top level. Finally, the result node returns the query result to the user. (5) After the query plan is executed, when the execution nodes are destroyed to reclaim resources, these nodes are also destroyed one by one in a bottom-up manner. For one lower-level megescan node and N upper-level query nodes in the same group, the megescan node is destroyed first to release the resources, and then the N query nodes are destroyed one by one to release their resources.

2. The method for optimizing the processing of a union according to claim 1, characterized in that: The scan in the subquery includes a scan of a single table and a link scan of multiple tables.

3. The method for optimizing the processing of a union according to claim 1, characterized in that: The same accessed basic table mode means that when the accessed basic table is a single table, the same basic table is required; when the accessed basic table is a link of multiple tables, the basic tables are required to be the same and the linking modes are also the same.

4. The method for optimizing the processing of a union according to claim 1, characterized in that: In step (2), when there is only one group, the append node is no longer set, and each upper-level query node in the group is directly used as the lower-level node of the result.

5. The method for optimizing the processing of a union according to claim 1, characterized in that: In step (4), when the megescan node does not obtain the record to be processed, it returns directly and submits the blank to the N upper query nodes. At this time, the N upper query nodes directly return the blank to the result node at the top layer, and finally the result node returns the blank query result to the user; In step (4), executing the megescan node refers to scanning the base table or linking operation; and executing the record by the upper query node refers to the upper query node performing a projection operation or expression calculation on the record.

6. Use of the method for optimizing the processing of unions in a relational database management system as claimed in any one of claims 1 to 5 in a relational database management or operating system.