Method, device and equipment for SQL execution optimization based on trino public table expression

By optimizing the generation and segmentation of CTEs in the logical execution plan phase of Trino, the efficiency problem of public table expression references in Trino is avoided, thus achieving more efficient SQL query performance and resource utilization.

CN118132582BActive Publication Date: 2026-11-24BEIJING DIPU TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410147185.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-02-02
Publication Date
2026-11-24
Estimated Expiration
2044-02-02

AI Technical Summary

Technical Problem

Trino generates the same temporary table repeatedly when referencing common table expressions, which reduces SQL execution efficiency and increases memory and disk space consumption.

Method used

During Trino's logical execution plan phase, query plans are optimized to reuse common table expressions by generating CTEScanNode nodes and adding ExchangeNode nodes, avoiding the repeated generation of CTEs. In the segmentation phase, sub-segmentation plans are built only once, and production and consumption operation operators are used to control data flow, detect and resolve deadlocks and memory usage anomalies.

Benefits of technology

It improves the performance and readability of SQL queries, reduces memory and disk space consumption, and enhances query efficiency, especially for large and complex queries.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118132582B_ABST
    Figure CN118132582B_ABST
Patent Text Reader

Abstract

This application provides a method, apparatus, and device for SQL execution optimization based on Trino common table expressions. In this method, during the logical execution plan generation phase, multiple CTEScanNode nodes corresponding to the same common table use the same CTE reference ID; that is, these CTEScanNode nodes all reference the same CTE temporary table. Simultaneously, during the logical execution plan segmentation phase, for CTEs that have already had sub-segment plans built, duplicate sub-segment plans are not built subsequently. This setting avoids executing the same query multiple times, thus improving query performance and readability, and reducing the consumption of resources such as memory and disk space.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to a method, apparatus, and device for optimizing SQL execution based on Trino common table expressions, belonging to the field of big data and analytics engine technology. Background Technology

[0002] SQL (Structured Query Language) queries are commands written in the SQL language in relational databases used to retrieve and process data. SQL queries can be used to select specific data from tables, sort, group, and filter data based on conditions, and perform aggregation, calculation, and statistical analysis on the data.

[0003] The WITH syntax provides a way to write auxiliary statements used in a large query; these statements are often called Common Table Expressions (CTEs). A CTE can be viewed as defining a temporary table that exists only in one query but can be referenced multiple times within the same query, thus improving query performance and readability.

[0004] Trino is a distributed SQL query engine that supports CTEs (Concurrent Result Sets). However, since a CTE is a temporary intermediate result set (i.e., a temporary table), if it is referenced multiple times in an SQL statement, the current version of Trino will generate a temporary intermediate result set for each reference. This results in a large number of identical common tables being generated repeatedly when referenced multiple times, thereby reducing SQL execution efficiency and using more memory and disk space. Summary of the Invention

[0005] This application provides an SQL execution optimization method based on Trino common table expressions to solve the problem that current solutions generate a large number of identical common tables repeatedly when multiple queries are referenced, thereby reducing SQL execution efficiency and using more memory and disk space resources.

[0006] To achieve the above objectives, this application provides the following technical solution: In a first aspect, embodiments of this application provide an SQL execution optimization method based on Trino common table expressions, which includes: After receiving the query request, during the logical execution plan generation phase, the table node is accessed and it is checked whether the table node is a reference to a common table. If so, a CTEScanNode node is generated. Among them, repeated references to the same common table by different table nodes will generate multiple CTEScanNode nodes, but the reference ID of the CTE used by multiple CTEScanNode nodes corresponding to the same common table is the same. During the optimization phase of the logical execution plan, new optimization rules are added to the AddExchanges optimization operator, including adding an ExchangeNode node at the upper layer of each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution; During the segmentation phase of the logical execution plan, the ExchangeNode is accessed, and it is checked whether the upstream source node of the ExchangeNode is a CTEScanNode. If so, it is checked whether the reference ID of the CTE used by the corresponding CTEScanNode has been used to build a sub-segmentation plan. If not, a sub-segmentation plan of the CTE producer type is built for the CTE. If so, a sub-segmentation plan is not built for the CTE. Construct a CTE operation operator and execute a query based on the CTE operation operator.

[0007] Based on the above methods, optionally, the following further methods may also be included: During the logical execution plan generation phase, if a table node is detected as not being a reference to a common table, a TableScanNode node is generated.

[0008] Based on the above method, optionally, the construction of the CTE operation operator includes: Construct a production-type operation operator CTEProducerOperator and a consumption-type operation operator CTEConsumerOperator; the production-type operation operator CTEProducerOperator is used to pull data from downstream nodes and add it to the queue, and the consumption-type operation operator CTEConsumerOperator is used to perform computation tasks, that is, to use the data in the queue to perform calculations and save the required data.

[0009] Based on the above methods, optionally, the following further methods may also be included: During the query execution process, periodically check for deadlocks. If so, the data from the thread that caused the deadlock is loaded using a file queue for use by other threads.

[0010] Based on the above methods, optionally, the following further methods may also be included: During the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then it is checked whether the currently used memory exceeds the user-configured upper limit. If so, an error message is output.

[0011] Based on the above methods, optionally, the following further methods may also be included: During the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then subsequent data will be written to the disk.

[0012] Secondly, embodiments of this application also provide an SQL execution optimization apparatus based on Trino common table expressions, which includes: The generation module is used to access table nodes and check whether the table node is a reference to a common table during the logical execution plan generation phase after obtaining the query request. If so, a CTEScanNode node is generated. Among them, repeated references to the same common table by different table nodes will generate multiple CTEScanNode nodes, but the reference ID of the CTE used by multiple CTEScanNode nodes corresponding to the same common table is the same. The optimization module is used to add new optimization rules to the AddExchanges optimization operator during the optimization phase of the logical execution plan, including adding ExchangeNode nodes above each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution; The segmentation module is used to access the ExchangeNode during the segmentation phase of the logical execution plan and check whether the upstream source node of the ExchangeNode is a CTEScanNode. If it is, it checks whether the reference ID of the CTE used by the corresponding CTEScanNode has been used to build a sub-segmentation plan. If not, it builds a sub-segmentation plan of the CTE producer type for the CTE. If it has been used, it does not build a sub-segmentation plan for the CTE. The query module is used to construct CTE operation operators and execute queries based on the CTE operation operators.

[0013] Thirdly, embodiments of this application also provide an electronic device, which includes a memory and a processor. The memory stores a computer program, and when the processor calls and executes the computer program, it implements the SQL execution optimization method based on Trino common table expressions as described in any one of the first aspects.

[0014] In the SQL execution optimization method, apparatus, and device based on Trino common table expressions provided in this application, during the logical execution plan generation phase, multiple CTEScanNode nodes corresponding to the same common table use the same CTE reference ID; that is, these CTEScanNode nodes all reference the same CTE temporary table. Simultaneously, during the logical execution plan segmentation phase, for CTEs that have already had sub-segment plans built, duplicate sub-segment plans are not subsequently built. This setting avoids executing the same query multiple times, thus improving query performance and readability, and reducing the consumption of resources such as memory and disk space. Attached Figure Description

[0015] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application. Furthermore, these drawings and textual descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concepts of this application to those skilled in the art through reference to specific embodiments.

[0016] Figure 1 A schematic diagram illustrating the overall process of executing SQL for Trino; Figure 2 A flowchart illustrating an embodiment of the SQL execution optimization method based on Trino common table expressions provided in this application; Figure 3 This is a schematic diagram of the SQL execution plan in a conventional method that does not employ the approach described in this embodiment; Figure 4 This is a schematic diagram of the SQL execution plan after applying the optimization method of this embodiment; Figure 5 A schematic diagram of the structure of an SQL execution optimization device based on Trino common table expressions provided in one embodiment of this application; Figure 6 This is a schematic diagram of the structure of an electronic device provided in one embodiment of this application. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the embodiments of this application. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application. Unless otherwise specified, the following embodiments and features can be combined with each other.

[0018] As an OLAP (Online Analytical Processing) analysis engine, Trino's SQL query return time is a major concern for users. When testing the TPCDS (Transaction Processing Performance Council Decision Support) dataset with Trino, we found that in SQL execution plans containing common table expressions, Trino resolves each reference to the common table into a separate execution plan, repeatedly generating the common table multiple times. This reduces SQL execution efficiency, increases user wait time, and consumes more memory and disk space.

[0019] To address the aforementioned issues, this application provides an SQL execution optimization method based on Trino common table expressions. It proposes improving SQL execution efficiency by reusing common tables within Trino, while simultaneously reducing the consumption of resources such as memory and disk space. The following examples and embodiments illustrate the specific implementation scheme in a non-limiting manner.

[0020] This application provides an SQL execution optimization method based on Trino common table expressions in some embodiments. In specific implementations, this method can be executed by an electronic device, such as a computer or server. More specifically, the electronic device can run processing software that implements the corresponding method during runtime.

[0021] To facilitate a better understanding of the scheme in this application, a brief description of the overall process of Trino executing SQL is provided first. (Refer to...) Figure 1 , Figure 1 A schematic diagram illustrating the overall process of executing SQL for Trino. (Example) Figure 1 As shown, in the entire process, the user first inputs the query conditions and submits the query; then the query system performs lexical and syntactic analysis, as well as semantic analysis, in order to obtain query conditions and query parameters that can be recognized by subsequent programs.

[0022] Next comes the logical execution plan generation phase, which refers to the execution plan generated by Trino to guide Trino's operation steps when executing queries. The generation phase can generate a basic executable query plan.

[0023] The next stage is the logical execution plan optimization phase, which optimizes the generated basic query plan to improve query efficiency and parallel performance.

[0024] Next comes the segmentation phase of the logical execution plan. Segmentation refers to the process of breaking down the query into multiple tasks based on the query plan. Each task performs a portion of the computation (i.e., a segment) and passes the result to the next task. The segmented plan is a plan defined for each segment so that Trino can execute queries in parallel.

[0025] The next stage is the generation of physical execution fragments, which refers to Trino's process of converting the logical execution plan into a physical execution plan, further optimizing query execution efficiency. During the generation of physical execution fragments, operation operators are first generated, and Trino optimizes each operator in the logical execution plan, selecting the best execution plan and generating the corresponding physical execution fragment. The physical execution fragment is the operation step executed by Trino's execution engine.

[0026] The next stage is scheduling physical execution fragments, which refers to the process by which Trino executes the generated physical execution fragments. During scheduling, Trino allocates different tasks to available execution nodes for processing based on task dependencies and resource availability in the physical execution plan, achieving parallel execution. Trino also optimizes the execution order based on factors such as task priority and concurrency during scheduling, improving query efficiency.

[0027] Finally, the query task is executed, and the query results are summarized and returned to the user.

[0028] Based on the overall process described above, this application's embodiments optimize certain stages to avoid repeatedly generating a large number of identical CTEs and to avoid executing the same queries multiple times, thereby improving overall execution efficiency. (Refer to...) Figure 2 , Figure 2 This is a flowchart illustrating an embodiment of the SQL execution optimization method based on Trino common table expressions provided in this application. Figure 1 As shown, the SQL execution optimization method based on Trino common table expressions in this embodiment includes the following steps: Step S101: After obtaining the query request, during the logical execution plan generation phase, the table node is accessed and it is checked whether the table node is a reference to a common table. If so, a CTEScanNode node is generated. Among them, repeated references to the same common table by different table nodes will generate multiple CTEScanNode nodes, but the reference ID of the CTE used by multiple CTEScanNode nodes corresponding to the same common table is the same.

[0029] Specifically, during the generation phase of the logical execution plan, it is necessary to access the table node (i.e., the data source for the table query) and then generate subsequent operation nodes based on the data in that table node.

[0030] In practice, table nodes can be accessed through the public void visitTable method. The public void visitTable method is a method that iterates through all data source (Tables) nodes in the query plan builder. The method that accesses the TableHandle object will generate a scan node, which represents a data scan operation in the query plan, that is, scanning the specified table and returning the result.

[0031] If a table node is found to be a reference to a common table (CTE) when accessing a TableHandle object, a CTEScanNode is generated. A CTEScanNode is a scan node used to access the common table. In Trino, multiple table nodes referencing the same common table will generate multiple CTEScanNodes. However, in this embodiment, multiple CTEScanNodes corresponding to the same common table use the same CTE reference ID, meaning they reference the same CTE. This means that when a common table is referenced multiple times, Trino will generate multiple CTEScanNodes to scan the data, but these nodes will all reference the same temporary table (CTE), avoiding the duplicate generation of CTEs. This design improves query performance and readability while reducing resource consumption (memory, disk space, etc.).

[0032] In addition, in some embodiments, the above method may also include: during the generation phase of the logical execution plan, if it is detected that a table node is not a reference to a public table, then a TableScanNode node is generated.

[0033] That is, if a table node is not a reference to a common table (CTE) when accessing a TableHandle object, a TableScanNode node is generated. TableScanNode is also a logical execution plan node, similar to CTEScanNode, but unlike CTEScanNode, it cannot be used as a scan node to access a common table, but is used for other nodes.

[0034] Step S102: In the optimization phase of the logical execution plan, add new optimization rules to the AddExchanges optimization operator, including adding ExchangeNode nodes on the upper layer of each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution.

[0035] Specifically, as mentioned above, in Trino, the CTEScanNode node is used to access the scan node of the Common Table (CTE), and it typically appears in the middle stage of the query execution plan.

[0036] AddExchanges is an optimization operator in Trino used to add Exchange operators to the execution plan, enabling data distribution and aggregation, thereby improving query parallelism and execution efficiency. In the AddExchanges optimization operator, Trino uses ExchangeNode nodes to achieve parallel processing, distributing computational tasks across different nodes for execution, thus improving the overall parallelism and performance of the query.

[0037] To ensure that operators related to CTEScanNode can be executed in different segment stages, a new optimization rule needs to be added to the AddExchanges optimization operator, namely, adding an ExchangeNode node above the CTEScanNode node. The reason for this is that Trino's query plan generator typically places the execution plan for the common table at the beginning, and subsequent query plans directly reference the results of the common table. Without adding an ExchangeNode node, each node would perform a full scan of the common table, which could lead to excessive network traffic and computational resource consumption, degrading the overall query performance.

[0038] Therefore, after adding an ExchangeNode, this ExchangeNode will receive the input data (such as filter conditions) needed to scan the common table and distribute the computation tasks to multiple child nodes for execution. This ensures that Trino can distribute the common table scan tasks across different nodes when executing the query plan, avoiding duplicate scans. This improves query performance, especially for large and complex queries.

[0039] In addition, ExchangeNode nodes can control the parallelism of query computation, that is, dynamically adjust the number of tasks and node load according to the available computing resources in the cluster, thereby improving the overall query performance.

[0040] Step S103: In the segmentation phase of the logical execution plan, access the ExchangeNode and check whether the upstream source node of the ExchangeNode is a CTEScanNode. If it is, check whether the reference ID of the CTE used by the corresponding CTEScanNode has been used to build a sub-segmentation plan. If not, build a sub-segmentation plan of the CTE producer type for the CTE. If it is, do not build a sub-segmentation plan for the CTE.

[0041] Specifically, in Trino's execution plan, segmentation refers to the process of breaking down a query into multiple tasks for execution based on the query plan. Each task performs a portion of the computation and passes the result to the next task. The segmentation plan is a plan defined for each segment so that Trino can execute queries in parallel. Therefore, the segmentation plan is the basic building block that Trino uses to execute queries.

[0042] In practice, ExchangeNode nodes can be accessed via the visitExchange method. When accessing an ExchangeNode, Trino checks whether its upstream source node type is CTEScanNode. If it is CTEScanNode, Trino checks whether the CTE reference ID used by this CTEScanNode node has already built a sub-Plan.

[0043] If the CTE reference ID used by this CTEScanNode has not been used to build a sub-segmentation plan, Trino will build a CTE producer-type sub-segmentation plan to perform a scan of the common tables and return the results to the ExchangeNode. A producer-type sub-segmentation plan is also a sub-segmentation plan used to produce data.

[0044] If the CTE reference ID used by this CTEScanNode has already been used to build a sub-segmentation plan, Trino will not build a sub-segmentation plan for that CTE. Instead, it will only build a new segmentation plan for the new CTE (a CTE with an ID different from the one already built for a sub-segmentation plan). Furthermore, grandchild segments will not be generated repeatedly. A "grandchild segment" refers to a sub-segmentation plan (a plan containing multiple tasks), where each task contains a sub-segmentation plan. Because Trino supports nested queries and subqueries, grandchild segmentation plans may exist—that is, a query plan where a task contains a sub-segmentation plan. In this case, if it is detected that the CTE reference ID used by the CTEScanNode has already been used to build a sub-segmentation plan and a possible grandchild segmentation plan, Trino will not generate sub-segmentation plans and grandchild segmentation plans again. Instead, it will only generate a new sub-segmentation plan (which may include grandchild segmentation plans).

[0045] This means that the build task corresponding to a certain CTE only needs to be executed once, avoiding repeated calculations and data scanning, and improving query performance and resource utilization.

[0046] Step S104: Construct a CTE operation operator and execute a query based on the CTE operation operator.

[0047] Specifically, this step is for Figure 1 This section summarizes the generation of physical execution fragments (PEFs) and subsequent stages. The CTE operator is used to execute query tasks.

[0048] Furthermore, in some embodiments, constructing the CTE operation operator may specifically include: Construct a production-type operation operator CTEProducerOperator and a consumption-type operation operator CTEConsumerOperator. The production-type operation operator CTEProducerOperator is used to pull data from downstream nodes and add it to the queue (i.e., production), while the consumption-type operation operator CTEConsumerOperator is used to perform computation tasks, that is, to use the data in the queue to perform computations and save the required data (i.e., consumption).

[0049] The CTE operator uses a queue to store data, with a default queue length of 1024. Once the queue is full, the CTEProducerOperator will stop fetching data and wait for the CTEConsumerOperator to process the data in the queue.

[0050] This producer-consumer model effectively controls the concurrency of data flow and computational tasks, reducing data transmission and synchronization issues during multi-node parallel computing. Furthermore, using queues can improve query performance.

[0051] Based on the solutions in the above embodiments, during the logical execution plan generation phase, multiple CTEScanNode nodes corresponding to the same public table use the same CTE reference ID; that is, these CTEScanNode nodes all reference the same CTE temporary table. Simultaneously, during the logical execution plan segmentation phase, for CTEs that have already had sub-segment plans built, duplicate sub-segment plans will not be built subsequently. This configuration avoids executing the same query multiple times, thus improving query performance and readability, and reducing the consumption of resources such as memory and disk space.

[0052] Figure 3 A schematic diagram of the SQL execution plan in a conventional method not employing this embodiment is shown. Figure 4 A schematic diagram of the SQL execution plan after applying the optimization method of this embodiment is shown. Wherein, Figure 4 The CTE NODE in the text is the CTEScanNode, and it is accessed through... Figure 3 and Figure 4 As can be seen from the comparison, the optimization method of the present application embodiment reduces the number of repeated CTEs.

[0053] In addition, comparative tests were conducted on SQL queries with common table expressions in TPCDS. One group used the method of this application, i.e., CTE was enabled, and the other group used the conventional method, i.e., CTE was not enabled. The results showed that enabling CTE resulted in an average query performance improvement of about 30%.

[0054] In addition, in some embodiments, the above method may further include: During query execution, a deadlock is periodically checked; if so, data from the thread causing the deadlock is loaded using a file queue for use by other threads.

[0055] Specifically, in practice, CTE operators are reused multiple times, which can lead to deadlock in certain complex scenarios. Deadlock occurs when two or more threads are blocked during execution due to resource contention or communication issues, preventing them from continuing to work. Therefore, this embodiment establishes a deadlock detection mechanism to periodically check for deadlocks. If a deadlock occurs, a file queue is used to load data, thus addressing the deadlock problem specifically.

[0056] In practice, a dedicated thread can be used to detect deadlocks. This thread periodically scans other executing threads to check if they are blocked. If a thread is found to be blocked for a period exceeding a certain threshold, a deadlock is considered to have occurred, and a deadlock resolution mechanism needs to be initiated. When initiating the deadlock resolution mechanism, a file queue can be used to load data. A file queue is a data structure that can be shared among different threads to store data that needs to be accessed. When a deadlock occurs, the data that needs to be accessed can be placed in the file queue, and then other threads can retrieve and process the data from the queue.

[0057] Thus, by establishing a deadlock detection mechanism and using a file queue to load data, the deadlock problem that may occur in CTE operations can be specifically addressed.

[0058] In addition, in some embodiments, the above method may also include: during the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then it is detected whether the currently used memory exceeds the upper limit configured by the user, and if so, an error message is output.

[0059] Specifically, after producing data, the CTE (Consumer-Oriented) operator stores it in a queue. Since CTE operators operate in a producer-consumer mode, there may be situations where multiple consumers lack sufficient processing capacity, impacting the efficiency of the CTE operator. To address this, CTE operators typically have a reserved buffer space. When the queue is full, the produced data is temporarily cached in the buffer space, and loaded into the queue when there is available space. However, in practice, even with reserved buffer space, the buffer may become full due to inconsistent consumption rates among multiple consumers (CTE operators), leading to congestion.

[0060] To address this issue, this embodiment introduces a user configuration option, allowing users to specify the amount of memory available for loading public table data—essentially, configuring the maximum memory limit. Then, during query execution, if the remaining buffer space of the CTE operation operator is detected to be less than a set threshold, the system further checks whether the currently used memory exceeds the user-configured limit. If so, an error message is output to prompt the user to intervene promptly and prevent congestion.

[0061] In addition, in some other embodiments, the above method may also include: during the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then subsequent data is written to the disk.

[0062] Specifically, in this embodiment, to address the blocking issue caused by a full buffer, a file storage function is introduced. That is, when the remaining space in the buffer of the CTE operation operator is detected to be less than a set threshold, subsequent data is no longer written to memory but instead written to disk. When this data is needed, it can be read from disk through the file system. The disadvantage of this approach is that it may affect query performance because reading data from disk is slower than reading data from memory. However, compared to the blocking problem, this approach is more stable and reliable.

[0063] Furthermore, embodiments of this application provide an SQL execution optimization device based on Trino common table expressions, referring to... Figure 5 The device includes: The generation module 51 is used to access the table node and check whether the table node is a reference to the public table during the logical execution plan generation stage after obtaining the query request. If so, a CTEScanNode node is generated. Among them, repeated references to the same public table by different table nodes will generate multiple CTEScanNode nodes, but the reference ID of the CTE used by multiple CTEScanNode nodes corresponding to the same public table is the same. The optimization module 52 is used to add new optimization rules to the AddExchanges optimization operator during the optimization phase of the logical execution plan, including adding ExchangeNode nodes on the upper layer of each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution. Segmentation module 53 is used to access the ExchangeNode node during the segmentation phase of the logical execution plan and detect whether the upstream source node of the ExchangeNode node is a CTEScanNode node. If it is, it detects whether the reference ID of the CTE used by the corresponding CTEScanNode node has been used to build a sub-segmentation plan. If not, it builds a sub-segmentation plan of the CTE producer type for the CTE. If it is, it does not build a sub-segmentation plan for the CTE. The query module 54 is used to construct CTE operation operators and execute queries based on the CTE operation operators.

[0064] The specific implementation methods of each module of the SQL execution optimization device based on Trino common table expressions can be found in the corresponding content of the aforementioned method embodiments, and will not be repeated here.

[0065] Furthermore, embodiments of this application provide an electronic device, such as... Figure 6 As shown, the electronic device includes a memory 61 and a processor 62; wherein, the memory 61 stores a computer program, and when the processor 62 calls and executes the computer program, it implements the SQL execution optimization method based on Trino common table expressions in any of the above embodiments.

[0066] The electronic device can be a computer or a server, etc.

[0067] It is understood that the same or similar parts in the above embodiments can be referred to each other, and the contents not described in detail in some embodiments can be referred to the same or similar contents in other embodiments.

[0068] It should be noted that, in the description of this invention, unless otherwise stated, "a plurality of" means at least two.

[0069] In the description of this specification, references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., indicate that a specific feature, structure, material, or characteristic described in connection with that embodiment or example is included in at least one embodiment or example of the invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.

[0070] Although embodiments of the present invention have been shown and described above, it is understood that the above embodiments are exemplary and should not be construed as limiting the present invention. Those skilled in the art can make changes, modifications, substitutions and variations to the above embodiments within the scope of the present invention.

Claims

1. A method for SQL execution optimization based on Trino public table expression, characterized in that, include: After receiving the query request, during the logical execution plan generation phase, the table node is accessed, and it is checked whether the table node is a reference to a common table. If so, a CTEScanNode node is generated. Multiple references to the same common table by different table nodes will generate multiple CTEScanNode nodes, but the reference ID of the CTE used by multiple CTEScanNode nodes corresponding to the same common table is the same. If the table node is not a reference to a common table, a TableScanNode node is generated. The TableScanNode node cannot be used as a scan node to access the common table, but is used for other nodes. During the optimization phase of the logical execution plan, new optimization rules are added to the AddExchanges optimization operator, including adding an ExchangeNode node at the upper layer of each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution; During the segmentation phase of the logical execution plan, the ExchangeNode is accessed, and it is checked whether the upstream source node of the ExchangeNode is a CTEScanNode. If so, it is checked whether the reference ID of the CTE used by the corresponding CTEScanNode has been used to build a sub-segmentation plan. If not, a sub-segmentation plan of the CTE producer type is built for the CTE. If so, a sub-segmentation plan is not built for the CTE. Constructing CTE operation operators and executing queries based on the CTE operation operators; wherein, constructing CTE operation operators includes: constructing a production operation operator CTEProducerOperator and a consumer operation operator CTEConsumerOperator; the production operation operator CTEProducerOperator is used to pull data from downstream nodes and add it to a queue, and the consumer operation operator CTEConsumerOperator is used to perform computation tasks, that is, to use the data in the queue to perform computations and save the required data.

2. The method of claim 1, wherein, Also includes: During the query execution process, periodically check for deadlocks. If so, the data from the thread that caused the deadlock is loaded using a file queue for use by other threads.

3. The method of claim 1, wherein, Also includes: During the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then it is checked whether the currently used memory exceeds the user-configured upper limit. If so, an error message is output.

4. The method of claim 1, wherein, Also includes: During the query execution based on the CTE operation operator, if it is detected that the remaining space of the buffer space of the CTE operation operator is less than a set threshold, then subsequent data will be written to the disk.

5. A device for SQL execution optimization based on Trino public table expression, characterized in that, include: The generation module, upon receiving a query request, accesses table nodes during the logical execution plan generation phase and checks whether the table node references a common table. If so, it generates a CTEScanNode node. Multiple references to the same common table by different table nodes will generate multiple CTEScanNode nodes, but the reference IDs of multiple CTEScanNode nodes corresponding to the same common table are identical for the CTE. Furthermore, during the logical execution plan generation phase, if a table node does not reference a common table, it generates a TableScanNode node. This TableScanNode node cannot be used as a scan node to access the common table but is used for other nodes. The optimization module is used to add new optimization rules to the AddExchanges optimization operator during the optimization phase of the logical execution plan, including adding ExchangeNode nodes above each CTEScanNode node; wherein, the ExchangeNode node is used to receive the input data required for scanning the common table and distribute the calculation tasks to multiple child nodes for execution; The segmentation module is used to access the ExchangeNode during the segmentation phase of the logical execution plan and check whether the upstream source node of the ExchangeNode is a CTEScanNode. If it is, it checks whether the reference ID of the CTE used by the corresponding CTEScanNode has been used to build a sub-segmentation plan. If not, it builds a sub-segmentation plan of the CTE producer type for the CTE. If it has been used, it does not build a sub-segmentation plan for the CTE. The query module is used to construct CTE operation operators and execute queries based on the CTE operation operators. The construction of CTE operation operators includes constructing a production operation operator (CTEProducerOperator) and a consumer operation operator (CTEConsumerOperator). The production operation operator (CTEProducerOperator) is used to pull data from downstream nodes and add it to a queue, while the consumer operation operator (CTEConsumerOperator) is used to perform computation tasks, i.e., to use the data in the queue to perform computations and save the required data.

6. An electronic device, comprising: It includes a memory and a processor, wherein the memory stores a computer program, and the processor calls and executes the computer program to implement the SQL execution optimization method based on Trino common table expressions as described in any one of claims 1 to 4.

Citation Information

Patent Citations

  • Query optimization method and device and storage medium

    CN116501761A

  • Method, device and equipment for detecting data query scheduling deadlock

    CN117312346A