A database-based statement optimization method and apparatus

By optimizing the set of correlated subqueries and constructing the target subquery, the database query performance issues caused by correlated subqueries were resolved, and the efficiency of the execution plan was improved.

CN123178448APending Publication Date: 2026-11-27SHANGHAI DAMENG DATABASE
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611646058.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-23
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

In existing technologies, the execution of correlated subqueries leads to poor database query performance, especially when the correlated subquery repeatedly executes rows of the outer result set multiple times, generating a large number of calculation and comparison operations, which affects the performance of the execution plan.

Method used

Optimization is achieved by identifying the statement to be optimized, its corresponding first set, and the second set corresponding to the related subqueries. The target subquery is constructed and the statement to be optimized is rewritten to reduce the number of executions of the related subqueries and optimize the execution plan.

Benefits of technology

It simplifies related subqueries, reduces the amount of data processed during database queries, and improves the overall performance of the execution plan.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123178448A_ABST
    Figure CN123178448A_ABST
Patent Text Reader

Abstract

The application discloses a database-based statement optimization method and device. The specific implementation scheme comprises the following steps: determining a to-be-optimized statement and a first set corresponding to the to-be-optimized statement; determining a second set; and optimizing the to-be-optimized statement based on the first set and the second set to obtain an optimized statement. The to-be-optimized statement is optimized based on the first set and the second set to obtain the optimized statement, the related subquery is simplified, the execution times of the related subquery are reduced, the amount of data to be calculated, processed and compared when performing a query in the database is reduced, and the overall performance of the execution plan is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of database technology, and in particular to a database-based statement optimization method and apparatus. Background Technology

[0002] A correlated subquery is a subquery whose execution depends on values ​​provided by an outer query. The subquery references columns from the outer query, causing it to be re-executed for each row processed by the outer query. The key characteristics of a correlated subquery are: it references columns from the outer table, and it executes the outer result set rows multiple times. The correlated columns are the columns from the outer table referenced in the correlated subquery.

[0003] The following SQL statement creates three tables, T1, T2, and T3, each with 100 rows of data: CREATE TABLE T1(C1 INT, C2 INT); CREATE TABLE T2(D1 INT, D2 INT); CREATE TABLE T3(E1 INT, E2 INT); INSERT INTO T1 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; INSERT INTO T2 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; INSERT INTO T3 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; COMMIT; Execute the following SQL query: SELECT * FROM T1 JOIN T2 ON C1=(SELECT E2 FROM T3WHERE D1=E1); Analyzing this statement, D1 in (SELECT E2 FROM T3 WHERE D1=E1) references a column of T2 in the outer JOIN join. T2 is part of the outer query, so this clause can be determined to be a correlated subquery clause.

[0004] When the above statement is executed on the server, the execution plan generated according to the existing general strategy is as follows: PIPE2 PRJT2 NEST LOOP INNER JOIN2: join condition([rq] = T1.C1) CSCN2:(T1) CSCN2:(T2) SPL2 PRJT2 SLCT2:T3.E1 = var1 CSCN2:(T3) The above execution plan can be understood as follows: Tables T1 and T2 are first joined using a nested inner join, which generates an intermediate result—the Cartesian product of the T1 and T2 data. This intermediate result is then filtered using the join condition, where rq corresponds to the correlated subquery (SELECT E2 FROM T3 WHERE D1=E1). Obtaining rq involves taking each row in the intermediate result set, passing the value of column D1 as a temporary variable (var1) into the SPL2 plan, executing it once, and then comparing the output value E2 with the value C1 in the same row. If they are equal, the value is output; otherwise, it is not.

[0005] In the above execution plan, since T1 and T2 each contain 100 rows of data, the Cartesian product of the 100 rows of data in T1 and T2 will have 10,000 rows of data. If rq is calculated for each of these 10,000 rows of data, SLCT2+CSCN2 in T3 will have to process 1 million rows of data, resulting in poor performance of the execution plan. Summary of the Invention

[0006] This invention provides a database-based statement optimization method and apparatus to simplify related subqueries and improve the performance of execution plans.

[0007] According to one aspect of the present invention, a database-based statement optimization method is provided, comprising: Determine the statement to be optimized and the first set corresponding to the statement to be optimized. The statement to be optimized includes statements containing related subqueries. The statement to be optimized is used to perform data query in the database. The first set is related to the table corresponding to the statement to be optimized. Determine a second set, which includes the set corresponding to the related subquery, and the second set is related to the table corresponding to the related subquery; Based on the first set and the second set, the statement to be optimized is optimized to obtain the optimized statement.

[0008] According to another aspect of the present invention, a database-based statement optimization apparatus is provided, comprising: The first determining module is used to determine the statement to be optimized and the first set corresponding to the statement to be optimized. The statement to be optimized includes a statement containing related subqueries. The statement to be optimized is used to perform data query in the database. The first set is related to the table corresponding to the statement to be optimized. The second determining module is used to determine a second set, the second set including the set corresponding to the related subquery, and the second set being related to the table corresponding to the related subquery; The optimization module is used to optimize the statement to be optimized based on the first set and the second set to obtain the optimized statement.

[0009] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the database-based statement optimization method according to any embodiment of the present invention.

[0010] According to another aspect of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium storing computer instructions for causing a processor to execute and implement the database-based statement optimization method according to any embodiment of the present invention.

[0011] According to another aspect of the present invention, a computer program product is provided, the computer program product comprising a computer program that, when executed by a processor, implements the database-based statement optimization method described in any embodiment of the present invention.

[0012] The technical solution of this invention involves determining a statement to be optimized and a first set corresponding to the statement; determining a second set; and optimizing the statement to be optimized based on the first set and the second set to obtain an optimized statement. Optimizing the statement to be optimized using the first set and the second set simplifies related subqueries, reduces the number of executions of related subqueries, decreases the amount of data that needs to be calculated, processed, and compared when querying the database, and improves the overall performance of the execution plan.

[0013] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description

[0014] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0015] Figure 1 This is a flowchart of a database-based statement optimization method provided according to Embodiment 1 of the present invention; Figure 2 This is a flowchart of an optimized statement determination method provided according to Embodiment 2 of the present invention; Figure 3 This is a schematic diagram of the structure of a database-based statement optimization device according to Embodiment 3 of the present invention; Figure 4 This is a block diagram of an electronic device provided according to Embodiment 4 of the present invention. Detailed Implementation

[0016] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0017] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0018] Example 1 Figure 1This is a flowchart of a database-based statement optimization method according to Embodiment 1 of the present invention. This embodiment is applicable to the optimization of query statements. The method can be executed by a database-based statement optimization device, which can be implemented in hardware and / or software and can be configured in an electronic device. Figure 1 As shown, the method includes: S110. Determine the statement to be optimized and the first set corresponding to the statement to be optimized.

[0019] The statement to be optimized includes statements containing related subqueries. The statement to be optimized is used to query data in the database. The first set is related to the table corresponding to the statement to be optimized.

[0020] In this embodiment, the statement to be optimized can be understood as an SQL statement input by the user based on an existing relational database. The statement to be optimized can be a correct query statement after lexical, syntactic, and semantic analysis. The statement to be optimized can perform data queries in the database. The statement to be optimized is a query statement including correlated subqueries. A correlated subquery is a subquery in a query statement whose execution depends on values ​​provided by the outer query. The subquery references columns from the outer query, causing the subquery to be re-executed for each row processed by the outer query. If a correlated subquery references columns from an outer table, it will repeatedly execute rows from the outer result set. The first set is related to the table corresponding to the statement to be optimized, and this first set can be formed by the tables contained in the outer query of the statement to be optimized.

[0021] Specifically, if the user-input query is correctly analyzed using lexical, syntactic, and semantic methods based on an existing relational database, and the analyzed results (i.e., semantic analysis results) are successfully obtained, then the user-input query is identified as the query to be optimized. Next, the data source of the outer query of the query to be optimized can be determined, and the first set can be determined based on the determination result.

[0022] For example, the statement to be optimized could be: SELECT * FROM T1 JOIN T2 ON C1=(SELECT E2FROM T3 WHERE D1=E1). Here, in (SELECT E2 FROM T3 WHERE D1=E1), D1 references a column from table T2 in the outer JOIN join. Table T2 is part of the outer query, so this clause can be identified as a correlated subquery.

[0023] For the above statement to be optimized, three tables T1, T2, and T3 were created, each with 100 rows of data: CREATE TABLE T1(C1 INT, C2 INT); CREATE TABLE T2(D1 INT, D2 INT); CREATE TABLE T3(E1 INT, E2 INT); INSERT INTO T1 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; INSERT INTO T2 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; INSERT INTO T3 SELECT LEVEL,LEVEL FROM DUAL CONNECT BY LEVEL<=100; COMMIT; Analyzing the statement to be optimized, in (SELECT E2 FROM T3 WHERE D1=E1), D1 references a column of T2 in the outer JOIN join. T2 is part of the outer query, so this clause can be determined to be a correlated subquery.

[0024] When executed on the server, the execution plan generated according to existing general strategies is as follows: PIPE2 PRJT2 NEST LOOP INNER JOIN2: join condition([rq] = T1.C1) CSCN2:(T1) CSCN2:(T2) SPL2 PRJT2 SLCT2:T3.E1 = var1 CSCN2:(T3) The above execution plan can be understood as follows: Tables T1 and T2 are first joined using a nested inner join, which generates an intermediate result—the Cartesian product of the data from T1 and T2. This intermediate result is then filtered using the join condition, where rq corresponds to the correlated subquery in the statement to be optimized (SELECT E2 FROM T3 WHERE D1=E1). Obtaining rq involves taking each row in the intermediate result set, passing the value of column D1 as a temporary variable (var1) into the SPL2 plan, executing it once, and then comparing the output value E2 with the value C1 in the same row. If they are equal, the value is output; otherwise, it is not.

[0025] S120, Determine the second set.

[0026] The second set includes the set corresponding to the correlated subquery, and the second set is related to the table corresponding to the correlated subquery.

[0027] In this embodiment, the second set can be understood as the set corresponding to the correlated subqueries in the statement to be optimized. The second set can be formed by the tables in the correlated subqueries that are related to the outer query.

[0028] Specifically, all relevant columns in the relevant subqueries corresponding to the statement to be optimized can be collected, and a second set can be obtained based on the tables corresponding to each relevant column. The relevant columns are the columns of the tables in the outer query referenced in the relevant subquery.

[0029] S130. Based on the first set and the second set, optimize the statement to be optimized to obtain the optimized statement.

[0030] In this embodiment, the optimized statement can be understood as the statement obtained after optimizing the statement to be optimized. The outer query of the optimized statement does not include the table corresponding to the relevant columns.

[0031] Specifically, the first and second sets can be evaluated. If the evaluation is successful, the statement to be optimized can be rewritten to obtain the optimized statement. The tables corresponding to the relevant columns in the statement to be optimized can be rewritten so that these tables are not included in the outer query of the optimized statement.

[0032] For example, the execution plan of the optimized statement differs from that of the statement to be optimized. In the optimized execution plan, not only are the number of times rq is executed reduced, but because of the reduced number of rq executions, the number of rows processed by SLCT2+CSCN2 in T3 is also reduced.

[0033] The technical solution of this invention involves determining a statement to be optimized and a first set corresponding to the statement; determining a second set; and optimizing the statement to be optimized based on the first set and the second set to obtain an optimized statement. Optimizing the statement to be optimized using the first set and the second set simplifies related subqueries, reduces the number of executions of related subqueries, decreases the amount of data that needs to be calculated, processed, and compared when querying the database, and improves the overall performance of the execution plan.

[0034] Based on the above embodiments, modified embodiments of the above embodiments are proposed. It should be noted that, in order to keep the description brief, only the differences from the above embodiments are described in the modified embodiments.

[0035] In one embodiment, determining the statement to be optimized and the first set corresponding to the statement to be optimized includes: Obtain the statement to be optimized and the data source item corresponding to the statement to be optimized, wherein the data source item includes the item contained in the statement to be optimized for specifying the data source of the statement to be optimized; When the data source item is a multi-table join, the set of tables included in the data source item is determined as the first set corresponding to the statement to be optimized.

[0036] In this embodiment, the data source item can be understood as an item included in the statement to be optimized that specifies the data source of the statement to be optimized. The data source item can indicate the data source of the outer query of the statement to be optimized. A multi-table join can be understood as an operation in which two or more tables, views or subqueries are involved in the data source item of a statement to be optimized, and they are combined into a result set through join conditions.

[0037] Specifically, after obtaining the statement to be optimized, the first step is to determine whether the data source items in the outer query of the statement to be optimized are multi-table joins. If so, all tables included in the data source items are collected, and the set formed by the above tables is denoted as the first set.

[0038] For example, to determine whether the data source item (i.e., the FROM item) of the outer query in the statement to be optimized is a multi-table join, in the above statement to be optimized, the FROM item of the outer query is FROM T1 JOIN T2… Therefore, the above FROM item is a multi-table join, which is implemented through T1 JOIN T2… Collect all tables T1 and T2 contained in the FROM item, and the resulting first set A includes tables T1 and T2.

[0039] In one embodiment, determining the second set includes: Determine the join conditions, which include the join conditions between tables contained in the data source item corresponding to the statement to be optimized; Based on the connection conditions, a second set is determined.

[0040] In this embodiment, the join condition can be understood as the join condition between the tables contained in the data source item corresponding to the statement to be optimized. The join condition can indicate the conditions or matching methods such as comma connection between the tables contained in the data source item.

[0041] Specifically, by identifying the join conditions contained in the statement to be optimized, rows from the tables containing the data source items that meet the conditions can be combined to form intermediate or final results usable by the outer query. It can be determined whether the join conditions meet a preset form, and a second set can be determined based on the determination result.

[0042] For example, in the above statement to be optimized, the join condition can be the join condition of T1 and T2, and the join condition can include: ON C1=(SELECT E2 FROM T3 WHERE D1=E1).

[0043] Optionally, determining the second set based on the connection conditions includes: When the connection conditions meet the preset form, collect the relevant columns contained in the relevant subquery, and the relevant columns include the columns contained in the relevant subquery that are related to the data source item; The set of tables to which the relevant columns belong is determined as the second set.

[0044] Specifically, it checks whether the join condition meets the preset pattern. If so, it collects all relevant columns from the correlated subquery. Then, it denotes the set formed by the tables to which the relevant columns belong as the second set.

[0045] For example, the preset format can be set to "column ops correlated subquery". The preset format can also be set to other formats, allowing for expansion and support. Next, it is determined whether the join condition satisfies the preset format. If so, all correlated columns in the correlated subquery are collected. For the above statement to be optimized, the correlated column in the correlated subquery is D1. If the table to which the correlated column D1 belongs is determined to be T2, then the second set B includes table T2.

[0046] Example 2 Figure 2 This is a flowchart of a method for determining optimized statements according to Embodiment 2 of the present invention. This embodiment focuses on the method for determining optimized statements in the above embodiments. Figure 2 As shown, the method includes: S210. Determine the statement to be optimized and the first set corresponding to the statement to be optimized.

[0047] S220, Determine the second set.

[0048] S230. When the second set is a subset of the first set, construct a target subquery.

[0049] The data source corresponding to the target subquery is the second set, and the query item corresponding to the target subquery is related to the related subquery.

[0050] In this embodiment, the target subquery can be understood as a newly constructed subquery. The data source of the target subquery is the second set, and the query items of the target subquery can be determined based on the related subqueries.

[0051] Specifically, after determining the first set and the second set, we first determine whether the second set is a subset of the first set. If so, we construct a new subquery as the target subquery.

[0052] For example, if the first set A includes tables T1 and T2, and the second set B includes table T2, then the second set B is a subset of the first set A. Therefore, a new subquery is constructed as the target subquery. The data source of this target subquery (i.e., the FROM clause of the target subquery) is the second set B, and the query terms of the target subquery are determined based on the related subqueries in the statement to be optimized. For example, the constructed target subquery is SELECT (SELECT E2 FROM T3 WHERE D1=E1)F1 FROM T2 (here, (SELECT E2 FROM T3 WHERE D1=E1) is aliased as F1).

[0053] S240. Based on the target subquery, optimize the statement to be optimized to obtain the optimized statement.

[0054] Specifically, based on the target subquery, the statement to be optimized is rewritten. This involves rewriting the tables corresponding to the relevant columns in the statement to be optimized, resulting in the optimized statement.

[0055] Optionally, optimizing the statement to be optimized based on the target subquery to obtain the optimized statement includes: Determine the second set of elements and the original query terms contained in the statement to be optimized; Based on the related subqueries contained in the statement to be optimized, determine the optimized query items; The table contained in the second set in the statement to be optimized is replaced with the target subquery, and the original query item in the statement to be optimized is replaced with the optimized query item to obtain the optimized statement.

[0056] In this embodiment, the original query term can be understood as the query term corresponding to the statement to be optimized. The optimized query term can be understood as the output column of the target subquery.

[0057] Specifically, identify the data source items and original query items contained in the statement to be optimized. When optimizing the statement, the tables in the second set of data source items can be replaced with the target subquery, and the original query items can be replaced with the optimized query items.

[0058] For example, replace the second set B in the FROM field (data source field) of the statement to be optimized with the target subquery, and replace the original query terms with the optimized query terms. For ease of writing, table T2 in the second set B can be aliased as V. In the statement to be optimized, the original query terms are output T1.* and T2.*. When rewriting the statement to be optimized, replace V in the second set B with the target subquery SELECT (SELECT E2 FROM T3 WHERE D1=E1)F1 FROM T2, and replace the original query terms T1.* and T2.* with the optimized query terms T1.*, V.D1, and V.D2.

[0059] Therefore, the optimized statement is: SELECT T1.* ,V.D1,V.D2 FROM T1 JOIN (SELECT (SELECTE2 FROM T3 WHERE D1=E1) F1 ,D1 ,D2 FROM T2) V ON C1=F1. The execution plan corresponding to the optimized statement is as follows: PIPE2 PRJT2 HASH INNER JOIN2: join condition(V.F1 = T1.C1) CSCN2:(T1) PRJT2(rq => V.F1) CSCN2:(T2) SPL2 PRJT2 SLCT2:T3.E1 = var1 CSCN2:(T3).

[0060] Optionally, determining the optimized query item based on the related subqueries contained in the statement to be optimized includes: Determine the connection column corresponding to the connection condition; If the table to which the join column belongs belongs to the second set, then the join column and the related subquery are determined as the optimized query items; Otherwise, the relevant subquery will be determined as the optimized query item.

[0061] In this embodiment, the join column can be understood as the outer column corresponding to the join condition, and the join column can be used as the outer comparison column in the join condition.

[0062] For example, the join column C1 corresponding to the join condition is determined. Join column C1 is used to determine whether the current row of table T1 matches the result of the correlated subquery driven by T2. It is then determined whether table T1, to which join column C1 belongs, belongs to the second set B. If so, join column C1 and the correlated subquery are determined as the optimized query terms. Otherwise, only the correlated subquery is retained in the optimized query terms. In the above statement to be optimized, table T1, to which join column C1 belongs, does not belong to the second set B; therefore, the optimized query terms only contain the correlated subquery.

[0063] The technical solution of this invention involves constructing a target subquery when the second set is a subset of the first set; and optimizing the statement to be optimized based on the target subquery to obtain an optimized statement. By constructing a target subquery to optimize the statement to be optimized, the optimized statement is obtained, thus optimizing the multi-table join method from the original nested join to a hash join, reducing the number of executions of related subqueries, and improving the overall performance of the execution plan.

[0064] Example 3 Figure 3 This is a schematic diagram of the structure of a database-based statement optimization device according to Embodiment 3 of the present invention. Figure 3 As shown, the device includes: The first determining module 310 is used to determine the statement to be optimized and the first set corresponding to the statement to be optimized. The statement to be optimized includes a statement containing a related subquery. The statement to be optimized is used to perform data query in the database. The first set is related to the table corresponding to the statement to be optimized. The second determining module 320 is used to determine a second set, the second set including the set corresponding to the related subquery, and the second set being related to the table corresponding to the related subquery; The optimization module 330 is used to optimize the statement to be optimized based on the first set and the second set to obtain the optimized statement.

[0065] The database-based statement optimization device provided in this embodiment of the invention determines the statement to be optimized and a first set corresponding to the statement to be optimized through a first determining module; determines a second set through a second determining module; and optimizes the statement to be optimized based on the first set and the second set through an optimization module to obtain an optimized statement. Through the cooperation between the modules, and by optimizing the statement to be optimized using the first set and the second set to obtain the optimized statement, the device simplifies related subqueries, reduces the number of executions of related subqueries, reduces the amount of data that needs to be calculated, processed, and compared when querying the database, and improves the overall performance of the execution plan.

[0066] In one embodiment, the first determining module 310 is specifically used for: Obtain the statement to be optimized and the data source item corresponding to the statement to be optimized, wherein the data source item includes the item contained in the statement to be optimized for specifying the data source of the statement to be optimized; When the data source item is a multi-table join, the set of tables included in the data source item is determined as the first set corresponding to the statement to be optimized.

[0067] In one embodiment, the second determining module 320 includes: The first determining unit is used to determine the join conditions, which include the join conditions between tables contained in the data source item corresponding to the statement to be optimized. The second determining unit is used to determine the second set based on the connection conditions.

[0068] In one embodiment, the second determining unit is specifically used for: When the connection conditions meet the preset form, collect the relevant columns contained in the relevant subquery, and the relevant columns include the columns contained in the relevant subquery that are related to the data source item; The set of tables to which the relevant columns belong is determined as the second set.

[0069] In one embodiment, the optimization module 330 includes: A construction unit is used to construct a target subquery when the second set is a subset of the first set, wherein the data source corresponding to the target subquery is the second set, and the query item corresponding to the target subquery is related to the related subquery; The optimization unit is used to optimize the statement to be optimized based on the target subquery to obtain the optimized statement.

[0070] In one embodiment, the optimization unit includes: The first determining subunit is used to determine the data source items and original query items contained in the statement to be optimized; The second determining subunit is used to determine the optimized query item based on the related subqueries contained in the statement to be optimized; The replacement subunit is used to replace the table contained in the second set in the statement to be optimized with the target subquery, and to replace the original query item in the statement to be optimized with the optimized query item, so as to obtain the optimized statement.

[0071] In one embodiment, the second determining subunit is specifically used for: Determine the connection column corresponding to the connection condition; If the table to which the join column belongs belongs to the second set, then the join column and the related subquery are determined as the optimized query items; Otherwise, the relevant subquery will be determined as the optimized query item.

[0072] The database-based statement optimization device provided in this embodiment of the invention can execute the database-based statement optimization method provided in any embodiment of the invention. Through the cooperation and coordination between various modules, the statement optimization is completed, and it has the corresponding functional modules and beneficial effects of the execution method.

[0073] Example 4 According to embodiments of the present invention, the present invention also provides an electronic device, a computer-readable storage medium, and a computer program product.

[0074] Figure 4 This is a block diagram of an electronic device according to Embodiment 4 of the present invention, which implements the database-based statement optimization method described in the embodiments of the present invention. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices (such as helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.

[0075] like Figure 4 As shown, the electronic device 410 includes at least one processor 411 and a memory, such as a read-only memory (ROM) 412 or a random access memory (RAM), communicatively connected to the at least one processor 411. The memory stores computer programs executable by the at least one processor. The processor 411 can perform various appropriate actions and processes based on the computer program stored in the ROM 412 or loaded from storage unit 418 into the RAM 413. The RAM 413 can also store various programs and data required for the operation of the electronic device 410. The processor 411, ROM 412, and RAM 413 are interconnected via a bus 414. An input / output (I / O) interface 415 is also connected to the bus 414.

[0076] Multiple components in the electronic device are connected to the I / O interface 415, including: an input unit 416, such as a keyboard, mouse, etc.; an output unit 417, such as various types of displays, speakers, etc.; a storage unit 418, such as a disk, optical disk, etc.; and a communication unit 419, such as a network card, modem, wireless transceiver, etc. The communication unit 419 allows the electronic device to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0077] Processor 411 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 411 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 411 performs the various methods and processes described above, such as database-based statement optimization methods.

[0078] In some embodiments, the database-based statement optimization method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 418. In some embodiments, part or all of the computer program may be loaded and / or installed on electronic device 410 via ROM 412 and / or communication unit 419. When the computer program is loaded into RAM 413 and executed by processor 411, one or more steps of the database-based statement optimization method described above may be performed. Alternatively, in other embodiments, processor 411 may be configured to perform the database-based statement optimization method by any other suitable means (e.g., by means of firmware).

[0079] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0080] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0081] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0082] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0083] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or middleware components (e.g., application servers), or frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.

[0084] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.

[0085] In some embodiments, the computer program product includes a computer program that, when executed by a processor, implements the database-based statement optimization method provided in the embodiments of the present invention.

[0086] The technical solution of this invention provides a database-based statement optimization method and apparatus. The method involves determining the statement to be optimized and a first set corresponding to the statement; determining a second set; and optimizing the statement to be optimized based on the first and second sets to obtain an optimized statement. Optimizing the statement to be optimized using the first and second sets simplifies related subqueries, reduces the number of executions of related subqueries, decreases the amount of data that needs to be calculated, processed, and compared during database queries, and improves the overall performance of the execution plan.

[0087] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.

[0088] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A database-based statement optimization method, characterized in that, include: Determine the statement to be optimized and the first set corresponding to the statement to be optimized. The statement to be optimized includes statements containing related subqueries. The statement to be optimized is used to perform data query in the database. The first set is related to the table corresponding to the statement to be optimized. Determine a second set, which includes the set corresponding to the related subquery, and the second set is related to the table corresponding to the related subquery; Based on the first set and the second set, the statement to be optimized is optimized to obtain the optimized statement.

2. The method according to claim 1, characterized in that, The determination of the statement to be optimized and the first set corresponding to the statement to be optimized includes: Obtain the statement to be optimized and the data source item corresponding to the statement to be optimized, wherein the data source item includes the item contained in the statement to be optimized for specifying the data source of the statement to be optimized; When the data source item is a multi-table join, the set of tables included in the data source item is determined as the first set corresponding to the statement to be optimized.

3. The method according to claim 1, characterized in that, Determining the second set includes: Determine the join conditions, which include the join conditions between tables contained in the data source item corresponding to the statement to be optimized; Based on the connection conditions, a second set is determined.

4. The method according to claim 3, characterized in that, Determining the second set based on the connection conditions includes: When the connection conditions meet the preset form, collect the relevant columns contained in the relevant subquery, and the relevant columns include the columns contained in the relevant subquery that are related to the data source item; The set of tables to which the relevant columns belong is determined as the second set.

5. The method according to claim 1, characterized in that, The optimization of the statement to be optimized based on the first set and the second set to obtain the optimized statement includes: When the second set is a subset of the first set, a target subquery is constructed, wherein the data source corresponding to the target subquery is the second set, and the query item corresponding to the target subquery is related to the related subquery; Based on the target subquery, the statement to be optimized is optimized to obtain the optimized statement.

6. The method according to claim 5, characterized in that, The optimization of the statement to be optimized based on the target subquery to obtain the optimized statement includes: Determine the data source items and original query items contained in the statement to be optimized; Based on the related subqueries contained in the statement to be optimized, determine the optimized query items; The table contained in the second set in the statement to be optimized is replaced with the target subquery, and the original query item in the statement to be optimized is replaced with the optimized query item to obtain the optimized statement.

7. The method according to claim 6, characterized in that, The step of determining the optimized query item based on the related subqueries contained in the statement to be optimized includes: Determine the connection column corresponding to the connection condition; If the table to which the join column belongs belongs to the second set, then the join column and the related subquery are determined as the optimized query items; Otherwise, the relevant subquery will be determined as the optimized query item.

8. A database-based statement optimization device, characterized in that, include: The first determining module is used to determine the statement to be optimized and the first set corresponding to the statement to be optimized. The statement to be optimized includes a statement containing related subqueries. The statement to be optimized is used to perform data query in the database. The first set is related to the table corresponding to the statement to be optimized. The second determining module is used to determine a second set, the second set including the set corresponding to the related subquery, and the second set being related to the table corresponding to the related subquery; The optimization module is used to optimize the statement to be optimized based on the first set and the second set to obtain the optimized statement.

9. An electronic device, characterized in that, The electronic device includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the database-based statement optimization method according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that cause a processor to execute the database-based statement optimization method according to any one of claims 1-7.

11. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the database-based statement optimization method according to any one of claims 1-7.