IP Library Granted Patent US 12670132
Granted Patent B2
US 12670132 · App. 18/900,853 · Granted Jun 30, 2026

Cardinality estimation method and apparatus

Inventors: Ji Sun (Beijing, CN); Shifu Li (Beijing, CN); Shujie Zhang (Shenzhen, CN); Guoliang Li (Shenzhen, CN)
Assignee: HUAWEI TECHNOLOGIES CO., LTD.
G06F16/211G06N5/022
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12670132
App. No.
18/900,853
Granted
Jun 30, 2026
Kind
B2
Abstract

A cardinality estimation method and an apparatus are provided. The method includes: selecting a target model type from a plurality of model types based on a target distribution feature and mapping relationship information, where the mapping relationship information includes a mapping relationship between the plurality of model types and a predicted distribution feature, and the target distribution feature is extracted from sample data collected based on an analysis instruction; and obtaining, based on the target model type, a target model corresponding to the target model type, where the target model is used to perform cardinality estimation on a query instruction of a database.

Claims (66)

1 . A method implemented by a cardinality estimation apparatus, comprising:

obtaining, based on an analysis instruction, sample data from a historical query record and from at least one data table in a database, wherein the historical query record includes a plurality of historical query instructions;

extracting a target distribution feature from the sample data;

selecting a target model type from a plurality of model types based on the target distribution feature and mapping relationship information, wherein the mapping relationship information comprises a mapping relationship between the plurality of model types and a predicted distribution feature;

obtaining, based on the target model type, a target model corresponding to the target model type;

generating a plurality of candidate execution plans according to a query instruction;

executing on each candidate execution plan among the plurality of candidate execution plans a cardinality estimation based on the target model;

selecting an optimal execution plan from the plurality of candidate execution plans based on a result of the cardinality estimation of each candidate execution plan; and

executing the query instruction based on the optimal execution plan.

2 . The method according to claim 1 , wherein the obtaining, based on the target model type, the target model corresponding to the target model type comprises:

obtaining, based on the target model type and the sample data, the target model corresponding to the target model type.

3 . The method according to claim 2 , wherein operators corresponding to the plurality of model types are set in the database, and the obtaining, based on the target model type and the sample data, the target model corresponding to the target model type comprises:

determining, from the operators corresponding to the plurality of model types, an operator corresponding to the target model type;

creating, by using the operator corresponding to the target model type, an untrained model corresponding to the target model type; and

training the untrained model based on the sample data, to obtain the target model corresponding to the target model type.

4 . The method according to claim 1 , wherein the sample data comprises a plurality of columns of data corresponding to the analysis instruction and historical query instruction data corresponding to the analysis instruction, and the target distribution feature comprises a first feature and a second feature, and wherein the first feature indicates a distribution characteristic of the plurality of columns of data, and the second feature indicates a distribution characteristic of columns in the historical query instruction data.

5 . The method according to claim 4 , wherein the first feature comprises at least one of the following features:

a quantity of different values in each of the plurality of columns of data,

information about correlation between columns in the plurality of columns of data, or

a quantity of columns in the plurality of columns of data.

6 . The method according to claim 4 , wherein the second feature comprises at least one of the following features:

column combination coverage information in the historical query instruction data;

column query range coverage information corresponding to each historical query instruction in the historical query instruction data, or information about a column query similarity between historical query instructions in the historical query instruction data.

7 . The method according to claim 1 , wherein when the analysis instruction comprises identifiers of a plurality of data tables in the database, a process of collecting the sample data is specifically as follows:

sending sampling indication information to at least one data node, wherein the sampling indication information indicates the at least one data node to collect, from the plurality of data tables based on a join relationship between the plurality of data tables, data in a column in the analysis instruction; and

collecting a historical query instruction that is in the historical query record and that is related to the column in the analysis instruction.

8 . The method according to claim 1 , wherein the mapping relationship information is obtained through training according to an artificial intelligence algorithm.

9 . The method according to claim 1 , wherein a model parameter of the target model is stored in a memory and/or a disk.

10 . The method according to claim 1 , wherein the plurality of model types comprises at least two of the following types:

a Bayesian network, autoregression, mixed uniform distribution, a Gaussian kernel function, a sum-product probability, or a neural network.

11 . A cardinality estimation apparatus, comprising:

a memory;

one or more processors coupled to the memory, wherein the one or more processors are configured to:

obtain, based on an analysis instruction, sample data from a historical query record and from at least one data table in a database, wherein the historical query record includes a plurality of historical query instructions;

extract a target distribution feature from the sample data;

select a target model type from a plurality of model types based on the target distribution feature and mapping relationship information, wherein the mapping relationship information comprises a mapping relationship between the plurality of model types and a predicted distribution feature;

obtain, based on the target model type, a target model corresponding to the target model type;

generate a plurality of candidate execution plans according to a query instruction;

execute on each candidate execution plan among the plurality of candidate execution plans a cardinality estimation based on the target model;

select an optimal execution plan from the plurality of candidate execution plans based on a result of the cardinality estimation of each candidate execution plan; and

execute the query instruction based on the optimal execution plan.

12 . The apparatus according to claim 11 , wherein the one or more processors are further configured to:

obtain, based on the target model type and the sample data, the target model corresponding to the target model type.

13 . The apparatus according to claim 11 , wherein operators corresponding to the plurality of model types are set in the database, and the model establishment unit is specifically configured to:

determine, from the operators corresponding to the plurality of model types, an operator corresponding to the target model type;

create, by using the operator corresponding to the target model type, an untrained model corresponding to the target model type; and

train the untrained model based on the sample data, to obtain the target model corresponding to the target model type.

14 . The apparatus according to claim 11 , wherein the sample data comprises a plurality of columns of data corresponding to the analysis instruction and historical query instruction data corresponding to the analysis instruction, and the target distribution feature comprises a first feature and a second feature, wherein the first feature indicates a distribution characteristic of the plurality of columns of data, and the second feature indicates a distribution characteristic of columns in the historical query instruction data.

15 . The apparatus according to claim 14 , wherein the first feature comprises at least one of the following features:

a quantity of different values in each of the plurality of columns of data,

information about correlation between columns in the plurality of columns of data, or

a quantity of columns in the plurality of columns of data.

16 . The apparatus according to claim 14 , wherein the second feature comprises at least one of the following features:

column combination coverage information in the historical query instruction data,

column query range coverage information corresponding to each historical query instruction in the historical query instruction data, or

information about a column query similarity between historical query instructions in the historical query instruction data.

17 . A non-transitory computer-readable storage medium storing program instructions, that when executed by one or more processors, cause the one or more processors to:

obtain, based on an analysis instruction, sample data from a historical query record and from at least one data table in a database, wherein the historical query record includes a plurality of historical query instructions;

extract a target distribution feature from the sample data;

select a target model type from a plurality of model types based on the target distribution feature and mapping relationship information, wherein the mapping relationship information comprises a mapping relationship between the plurality of model types and a predicted distribution feature;

obtain, based on the target model type, a target model corresponding to the target model type;

generate a plurality of candidate execution plans according to a query instruction;

execute on each candidate execution plan among the plurality of candidate execution plans a cardinality estimation based on the target model;

select an optimal execution plan from the plurality of candidate execution plans based on a result of the cardinality estimation of each candidate execution plan; and

execute the query instruction based on the optimal execution plan.

18 . The non-transitory computer-readable storage medium according to claim 17 , wherein the target model corresponding to the target model type is obtained based on the target model type and the sample data.