# US 2026/0064678 A1 · Generating high-performance queries using optimized subqueries

> Agents: this data is available as JSON from the patentgrep API and MCP server — see https://patentgrep.com/llms.txt

- Number: US 2026/0064678 A1
- Document ID: US20260064678A1
- Status: Published application
- Kind: A1
- Application number: 18/816,030
- Filed: 2024-08-27
- Published: 2026-03-05
- Earliest claimed: 2024-08-27
- Assignees: Snowflake Inc.
- Applicants: Snowflake Inc.
- Inventors: Mohammad Afroz Alam; Jianzhun Du; Yijun Xie
- Web page: https://patentgrep.com/patent/US20260064678A1

## CPC

- G06F 16/24535: {of sub-queries or views}
- G06F 16/24542: {Plan optimisation}
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

A client system of a computing environment including a data platform is provided that optimizes a database query. The client system creates a logical plan tree for a Structured Query Language (SQL) query, with the logical plan tree comprising a set of nodes. The client system identifies a set of duplicate nodes in the set of nodes of the logical plan tree and identifies a duplicate subtree in the logical plan tree by determining a set of root nodes of the duplicate subtree using parent-child relationships of the set of nodes. The client system generates an optimized query by replacing instances of subqueries represented by the duplicate subtree using a set of optimized subqueries. The client system communicates the optimized query to the data platform for execution.

## Claims

### Claim 1

A machine-implemented method, comprising:
creating a logical plan tree for a Structured Query Language (SQL) query, the logical plan tree comprising a set of nodes;
identifying a set of duplicate nodes in the set of nodes of the logical plan tree;
identifying a duplicate subtree in the logical plan tree by determining a set of root nodes of the duplicate subtree using parent-child relationships of the set of nodes, the duplicate subtree representing a duplicate subquery of the SQL query;
generating an optimized query by replacing a set of instances of the duplicate subquery by a set of instances of the duplicate subtree in the logical plan tree using a set of optimized subqueries; and
communicating the optimized query to a data platform for execution of the optimized query.

### Claim 2 (depends on 1)

The machine-implemented method of claim 1, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery including instructions to create a Common Table Expression (CTE) and one or more subsequent references to the CTE.

### Claim 3 (depends on 1)

The machine-implemented method of claim 1, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery and one or more subsequent references to a stored result of the first instance of the duplicate subquery.

### Claim 4 (depends on 3)

The machine-implemented method of claim 3, wherein the one or more subsequent references comprise references to a materialized result of the first instance of the duplicate subquery.

### Claim 5 (depends on 3)

The machine-implemented method of claim 3, wherein the one or more subsequent references comprise a query identification referencing the stored result of the first instance of the duplicate subquery.

### Claim 6 (depends on 1)

The machine-implemented method of claim 1, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining at least one parent node of the each duplicate node is not in the set of duplicate nodes; and
in response to determining the at least one parent node is not in the set of duplicate nodes, adding the each duplicate node to the set of root nodes.

### Claim 7 (depends on 1)

The machine-implemented method of claim 1, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining the each duplicate node has two or more different parent nodes; and
in response to determining the each duplicate node has two or more different parent nodes, adding the each duplicate node to the set of root nodes.

### Claim 8 (depends on 1)

The machine-implemented method of claim 1, wherein replacing a set of instances of the duplicate subquery comprises:
searching a query string of each duplicate node of the set of duplicate nodes iteratively in post order to locate an instance of the duplicate subtree in the SQL query; and
replacing an instance of a subquery represented by the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.

### Claim 9 (depends on 1)

The machine-implemented method of claim 1, wherein generating the optimized query comprises:
generating an additional query including unique placeholder for each child node of the logical plan tree of the SQL query; and
replacing the placeholder with an optimized subquery of the set of optimized subqueries during an evaluation of the SQL query.

### Claim 10 (depends on 1)

The machine-implemented method of claim 1, wherein replacing the set of instances of the duplicate subquery comprises:
identifying an instance of the duplicate subquery represented by the duplicate subtree when applying a binary operation during DataFrame creation; and
replacing the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.

### Claim 11

A system comprising:
at least one processor; and
at least one memory storing instructions that, when executed by the at least one processor, cause the system to perform operations comprising:
creating a logical plan tree for a Structured Query Language (SQL) query, the logical plan tree comprising a set of nodes;
identifying a set of duplicate nodes in the set of nodes of the logical plan tree;
identifying a duplicate subtree in the logical plan tree by determining a set of root nodes of the duplicate subtree using parent-child relationships of the set of nodes, the duplicate subtree representing a duplicate subquery of the SQL query;
generating an optimized query by replacing a set of instances of the duplicate subquery by a set of instances of the duplicate subtree in the logical plan tree using a set of optimized subqueries; and
communicating the optimized query to a data platform for execution of the optimized query.

### Claim 12 (depends on 11)

The system of claim 11, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery including instructions to create a Common Table Expression (CTE) and one or more subsequent references to the CTE.

### Claim 13 (depends on 11)

The system of claim 11, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery and one or more subsequent references to a stored result of the first instance of the duplicate subquery.

### Claim 14 (depends on 13)

The system of claim 13, wherein the one or more subsequent references comprise references to a materialized result of the first instance of the duplicate subquery.

### Claim 15 (depends on 13)

The system of claim 13, wherein the one or more subsequent references comprise a query identification referencing the stored result of the first instance of the duplicate subquery.

### Claim 16 (depends on 11)

The system of claim 11, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining at least one parent node of the each duplicate node is not in the set of duplicate nodes; and
in response to determining the at least one parent node is not in the set of duplicate nodes, adding the each duplicate node to the set of root nodes.

### Claim 17 (depends on 11)

The system of claim 11, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining the each duplicate node has two or more different parent nodes; and
in response to determining the each duplicate node has two or more different parent nodes, adding the each duplicate node to the set of root nodes.

### Claim 18 (depends on 11)

The system of claim 11, wherein replacing a set of instances of the duplicate subquery comprises:
searching a query string of each duplicate node of the set of duplicate nodes iteratively in post order to locate an instance of the duplicate subtree in the SQL query; and
replacing an instance of a subquery represented by the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.

### Claim 19 (depends on 11)

The system of claim 11, wherein generating the optimized query comprises:
generating an additional query including unique placeholder for each child node of the logical plan tree of the SQL query; and
replacing the placeholder with an optimized subquery of the set of optimized subqueries during an evaluation of the SQL query.

### Claim 20 (depends on 11)

The system of claim 11, wherein replacing the set of instances of the duplicate subquery comprises:
identifying an instance of the duplicate subquery represented by the duplicate subtree when applying a binary operation during DataFrame creation; and
replacing the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.

### Claim 21

A machine-storage medium storing instructions that, when executed by one or more processors of a system, cause the system to perform operations comprising:
creating a logical plan tree for a Structured Query Language (SQL) query, the logical plan tree comprising a set of nodes;
identifying a set of duplicate nodes in the set of nodes of the logical plan tree;
identifying a duplicate subtree in the logical plan tree by determining a set of root nodes of the duplicate subtree using parent-child relationships of the set of nodes, the duplicate subtree representing a duplicate subquery of the SQL query;
generating an optimized query by replacing a set of instances of the duplicate subquery by a set of instances of the duplicate subtree in the logical plan tree using a set of optimized subqueries; and
communicating the optimized query to a data platform for execution of the optimized query.

### Claim 22 (depends on 21)

The machine-storage medium of claim 21, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery including instructions to create a Common Table Expression (CTE) and one or more subsequent references to the CTE.

### Claim 23 (depends on 21)

The machine-storage medium of claim 21, wherein the set of optimized subqueries comprises a first instance of the duplicate subquery and one or more subsequent references to a stored result of the first instance of the duplicate subquery.

### Claim 24 (depends on 23)

The machine-storage medium of claim 23, wherein the one or more subsequent references comprise references to a materialized result of the first instance of the duplicate subquery.

### Claim 25 (depends on 23)

The machine-storage medium of claim 23, wherein the one or more subsequent references comprise a query identification referencing the stored result of the first instance of the duplicate subquery.

### Claim 26 (depends on 21)

The machine-storage medium of claim 21, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining at least one parent node of the each duplicate node is not in the set of duplicate nodes; and
in response to determining the at least one parent node is not in the set of duplicate nodes, adding the each duplicate node to the set of root nodes.

### Claim 27 (depends on 21)

The machine-storage medium of claim 21, wherein determining the set of root nodes of the set of instances of the duplicate subtree comprises:
for each duplicate node of the set of duplicate nodes, performing operations comprising:
determining the each duplicate node has two or more different parent nodes; and
in response to determining the each duplicate node has two or more different parent nodes, adding the each duplicate node to the set of root nodes.

### Claim 28 (depends on 21)

The machine-storage medium of claim 21, wherein replacing a set of instances of the duplicate subquery comprises:
searching a query string of each duplicate node of the set of duplicate nodes iteratively in post order to locate an instance of the duplicate subtree in the SQL query; and
replacing an instance of a subquery represented by the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.

### Claim 29 (depends on 21)

The machine-storage medium of claim 21, wherein generating the optimized query comprises:
generating an additional query including unique placeholder for each child node of the logical plan tree of the SQL query; and
replacing the placeholder with an optimized subquery of the set of optimized subqueries during an evaluation of the SQL query.

### Claim 30 (depends on 21)

The machine-storage medium of claim 21, wherein replacing the set of instances of the duplicate subquery comprises:
identifying an instance of the duplicate subquery represented by the duplicate subtree when applying a binary operation during DataFrame creation; and
replacing the instance of the duplicate subtree with an optimized subquery of the set of optimized subqueries.
