# US 11,120,021 B2 · Systems and methods for optimizing queries

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

- Number: US 11,120,021 B2
- Document ID: US11120021B2
- Status: Granted
- Kind: B2
- Application number: 15/404,118
- Filed: 2017-01-11
- Published: 2018-07-12
- Granted: 2021-09-14
- Earliest claimed: 2017-01-11
- Assignees: Facebook, Inc.
- Applicants: Facebook, Inc.
- Inventors: Kevin Alan Schaeffer; Wojciech Galuba
- Web page: https://patentgrep.com/patent/US11120021B2

## CPC

- G06F 16/24539: {using cached or materialised query results}
- G06F 16/24542: {Plan optimisation}
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

Systems, methods, and non-transitory computer-readable media can receive a query that includes at least one operation for aggregating data stored in one or more database tables. A determination is made that at least a portion of the data being aggregated by the operation has been pre-computed. The query is optimized so that the operation uses the pre-computed data when executed.

## Claims

### Claim 1

A computer-implemented method comprising:
receiving, by a computing system, a query that includes at least one operation for aggregating data stored in one or more database tables;
determining, by the computing system, that at least a portion of the data being aggregated by the operation has been pre-aggregated,
wherein the data was pre-aggregated in response to a number of previous queries that aggregated the data, the number of previous queries that aggregated the data satisfying a threshold number; and
optimizing, by the computing system, the query so that the operation uses the pre-aggregated data when executed.

### Claim 2 (depends on 1)

The computer-implemented method of claim 1, wherein the operation for aggregating data corresponds to an average function, a count function, a maximum function, a minimum function, a median function, a sum function, or a union function.

### Claim 3 (depends on 1)

The computer-implemented method of claim 1, wherein optimizing the query so that the operation uses the pre-aggregated data when executed further comprises:
generating, by the computing system, an intermediate representation of the query; and
modifying, by the computing system, the intermediate representation of the query to reference the pre-aggregated data.

### Claim 4 (depends on 3)

The computer-implemented method of claim 3, wherein the intermediate representation of the query corresponds to an abstract syntax tree that describes the query, and wherein one or more nodes in the abstract syntax tree are modified to reference the pre-aggregated data.

### Claim 5 (depends on 1)

The computer-implemented method of claim 1, the method further comprising:
generating, by the computing system, a translated version of the query for execution by a given SQL query engine.

### Claim 6 (depends on 5)

The computer-implemented method of claim 5, wherein the translated version of the query corresponds to an Apache Hive query or a Presto query.

### Claim 7 (depends on 1)

The computer-implemented method of claim 1, wherein the data was pre-aggregated in response to the data being aggregated by one or more scheduled queries.

### Claim 8 (depends on 1)

The computer-implemented method of claim 1, the method further comprising:
determining, by the computing system, that a cost estimate for running the query satisfies a threshold value; and
preventing, by the computing system, the query from being executed.

### Claim 9

A system comprising:
at least one processor; and
a memory storing instructions that, when executed by the at least one processor, cause the system to perform:
receiving a query that includes at least one operation for aggregating data stored in one or more database tables;
determining that at least a portion of the data being aggregated by the operation has been pre-aggregated, wherein the data was pre-aggregated in response to a number of previous queries that aggregated the data, the number of previous queries that aggregated the data satisfying a threshold number; and
optimizing the query so that the operation uses the pre-aggregated data when executed.

### Claim 10 (depends on 9)

The system of claim 9, wherein the operation for aggregating data corresponds to an average function, a count function, a maximum function, a minimum function, a median function, a sum function, or a union function.

### Claim 11 (depends on 9)

The system of claim 9, wherein optimizing the query so that the operation uses the pre-aggregated data when executed further comprises:
generating an intermediate representation of the query; and
modifying the intermediate representation of the query to reference the pre-aggregated data.

### Claim 12 (depends on 11)

The system of claim 11, wherein the intermediate representation of the query corresponds to an abstract syntax tree that describes the query, and wherein one or more nodes in the abstract syntax tree are modified to reference the pre-aggregated data.

### Claim 13 (depends on 9)

The system of claim 9, the method further comprising:
generating a translated version of the query for execution by a given SQL query engine.

### Claim 14

A non-transitory computer-readable storage medium including instructions that, when executed by at least one processor of a computing system, cause the computing system to perform a method comprising:
receiving a query that includes at least one operation for aggregating data stored in one or more database tables;
determining that at least a portion of the data being aggregated by the operation has been pre-aggregated, wherein the data was pre-aggregated in response to a number of previous queries that aggregated the data, the number of previous queries that aggregated the data satisfying a threshold number; and
optimizing the query so that the operation uses the pre-aggregated data when executed.

### Claim 15 (depends on 14)

The non-transitory computer-readable storage medium of claim 14, wherein the operation for aggregating data corresponds to an average function, a count function, a maximum function, a minimum function, a median function, a sum function, or a union function.

### Claim 16 (depends on 14)

The non-transitory computer-readable storage medium of claim 14, wherein optimizing the query so that the operation uses the pre-aggregated data when executed further comprises:
generating an intermediate representation of the query; and
modifying the intermediate representation of the query to reference the pre-aggregated data.

### Claim 17 (depends on 16)

The non-transitory computer-readable storage medium of claim 16, wherein the intermediate representation of the query corresponds to an abstract syntax tree that describes the query, and wherein one or more nodes in the abstract syntax tree are modified to reference the pre-aggregated data.

### Claim 18 (depends on 14)

The non-transitory computer-readable storage medium of claim 14, the method further comprising:
generating a translated version of the query for execution by a given SQL query engine.

### Claim 19 (depends on 1)

The computer-implemented method of claim 1, wherein the data was pre-aggregated in response to at least one previous query that required an amount of time to execute, the amount of time to execute satisfying a threshold amount of time.
