# US 2022/0035813 A1 · Query optimization method and apparatus

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

- Number: US 2022/0035813 A1
- Document ID: US20220035813A1
- Status: Published application
- Kind: A1
- Application number: 17/354,465
- Filed: 2021-06-22
- Published: 2022-02-03
- Earliest claimed: 2020-08-03
- Assignees: Alipay (Hangzhou) Information Technology Co., Ltd.
- Applicants: Alipay (Hangzhou) Information Technology Co., Ltd.
- Inventors: Jian NIU
- Web page: https://patentgrep.com/patent/US20220035813A1

## CPC

- G06F 16/24535: {of sub-queries or views}
- G06F 16/24544: {Join order optimisation}
- G06F 11/3419: {by assessing time}
- G06F 16/24542: {Plan optimisation}
- G06F: ELECTRIC DIGITAL DATA PROCESSING (computer systems based on specific computational models G06N)

## Abstract

A query optimization method includes: obtaining a plurality of query tasks to be optimized, each query task including at least one subtask, and when a same query task includes at least two subtasks, a query dependence relationship existing between the at least two subtasks; combining the plurality of query tasks based on the query dependence relationship to obtain a combined task; and merging at least two subtasks in different query tasks of the plurality of query tasks based on the combined task to obtain a first merged subtask, the first merged subtask being executed to obtain query results of the at least two subtasks in the different query tasks.

## Claims

### Claim 1

A query optimization method, comprising:
obtaining a plurality of query tasks to be optimized, each query task including at least one subtask, and when a same query task includes at least two subtasks, a query dependence relationship existing between the at least two subtasks;
combining the plurality of query tasks based on the query dependence relationship to obtain a combined task; and
merging at least two subtasks in different query tasks of the plurality of query tasks based on the combined task to obtain a first merged subtask, the first merged subtask being executed to obtain query results of the at least two subtasks in the different query tasks.

### Claim 2 (depends on 1)

The method according to claim 1, wherein the merging the at least two subtasks in the different query tasks to obtain the first merged subtask includes:
determining a non-dependent subtask in each query task of the plurality of query tasks based on the query dependence relationship to form a first task set of the combined task; and
merging at least two subtasks in the first task set to obtain the first merged subtask.

### Claim 3 (depends on 2)

The method according to claim 2, wherein each subtask includes a query condition and a query data source; and the merging the at least two subtasks in the first task set to obtain the first merged subtask includes:
merging at least two subtasks in the first task set that have a same query condition and query data source, to obtain the first merged subtask.

### Claim 4 (depends on 2)

The method according to claim 2, wherein each subtask includes a query data source; and the merging the at least two subtasks in the first task set to obtain the first merged subtask includes:
determining a plurality of subtasks with a same query data source from the first task set; and
merging at least two subtasks whose query paths have a same part in the plurality of subtasks with the same query data source, to obtain the first merged subtask.

### Claim 5 (depends on 2)

The method according to claim 2, after the forming the first task set of the combined task, further comprising:
determining, from the combined task based on the query dependence relationship, subtasks that depend on each subtask in the first task set, to form a second task set of the combined task;
executing the subtasks in the first task set, and storing query results; and
if the second task set and the first task set have subtasks with a same query condition and query data source, using a query result of the subtask in the first task set as a query result of the same subtask in the second task set.

### Claim 6 (depends on 1)

The method according to claim 1, wherein each subtask includes a query data source, and the query data source includes at least two data sources of different physical architectures.

### Claim 7 (depends on 2)

The method according to claim 2, after the forming the first task set of the combined task, further comprising:
concurrently executing the subtasks in the first task set; and
placing, into a cache, a query result of a subtask whose query duration meets a predetermined condition.

### Claim 8 (depends on 1)

The method according to claim 1, further comprising:
before the combining the plurality of query tasks to obtain the combined task, determining a first query logic from a query task based on a predetermined determining condition, the first query logic including at least one subtask in the same query task; and storing a query result of the first query logic; and
after the combining the plurality of query tasks to obtain the combined task, in response to determining that a query logic that is the same as the first query logic exists in the combined task, using the query result of the first query logic as a query result of the query logic that is the same as the first query logic.

### Claim 9

A query optimization apparatus, comprising:
a processor; and
a memory storing instructions executable by the processor,
wherein the processor is configured to:
obtain a plurality of query tasks to be optimized, each query task including at least one subtask, and when a same query task includes at least two subtasks, a query dependence relationship existing between the at least two subtasks;
combine the plurality of query tasks based on the query dependence relationship to obtain a combined task; and
merge at least two subtasks in different query tasks of the plurality of query tasks based on the combined task to obtain a first merged subtask, the first merged subtask being executed to obtain query results of the at least two subtasks in the different query tasks.

### Claim 10 (depends on 9)

The apparatus according to claim 9, wherein the processor is further configured to:
determine a non-dependent subtask in each query task of the plurality of query tasks based on the query dependence relationship to form a first task set of the combined task; and
merge at least two subtasks in the first task set to obtain the first merged subtask.

### Claim 11 (depends on 10)

The apparatus according to claim 10, wherein each subtask includes a query condition and a query data source; and the processor is further configured to:
merge at least two subtasks in the first task set that have a same query condition and query data source, to obtain the first merged subtask.

### Claim 12 (depends on 10)

The apparatus according to claim 10, wherein each subtask includes a query data source; and the processor is further configured to:
determine a plurality of subtasks with a same query data source from the first task set; and
merge at least two subtasks whose query paths have a same part in the plurality of subtasks with the same query data source, to obtain the first merged subtask.

### Claim 13 (depends on 10)

The apparatus according to claim 10, wherein the processor is further configured to:
determine, from the combined task based on the query dependence relationship, subtasks that depend on each subtask in the first task set, to form a second task set of the combined task;
execute the subtasks in the first task set, and store query results; and
if the second task set and the first task set have subtasks with a same query condition and query data source, obtain a query result of the subtask in the first task set as a query result of the same subtask in the second task set.

### Claim 14 (depends on 9)

The apparatus according to claim 9, wherein each subtask includes a query data source, and the query data source includes at least two data sources of different physical architectures.

### Claim 15 (depends on 10)

The apparatus according to claim 10, wherein the processor is further configured to:
concurrently execute the subtasks in the first task set; and
place, into a cache, a query result of a subtask whose query duration meets a predetermined condition.

### Claim 16 (depends on 9)

The apparatus according to claim 9, wherein the processor is further configured to:
determine a first query logic from a query task based on a predetermined determining condition, the first query logic including at least one subtask in the same query task;
store a query result of the first query logic; and
in response to determining that a query logic that is the same as the first query logic exists in the combined task, obtain the query result of the first query logic as a query result of the query logic that is the same as the first query logic.

### Claim 17

A non-transitory computer readable storage medium having stored therein instructions that, when executed by a processor of a device, cause the device to perform a query optimization method, the method comprising:
obtaining a plurality of query tasks to be optimized, each query task including at least one subtask, and when a same query task includes at least two subtasks, a query dependence relationship existing between the at least two subtasks;
combining the plurality of query tasks based on the query dependence relationship to obtain a combined task; and
merging at least two subtasks in different query tasks of the plurality of query tasks based on the combined task to obtain a first merged subtask, the first merged subtask being executed to obtain query results of the at least two subtasks in the different query tasks.
